Skip to main content
Mohamed Hameed21513110
Inspiring
September 27, 2022
Answered

increasing the number only

  • September 27, 2022
  • 1 reply
  • 1372 views

Greetings to all
I have a project with more than 200 names and these names contain a number
One of them is in the beginning of a line, in the middle of a line, or at the end of a line

 

 

i use this code

I use this code, but it works on a number only
I want it to work on text and number while preserving the text and increasing the number only

doc = app.activeDocument;
    // the front document

var txtLayer = activeDocument.layers[0];
// obviously, the text layer if found

if (txtLayer) {
    var num = txtLayer.textItem.contents.match(/\d+/)[0],
    // find the numeric part
        len = num.length,
    // find the length of the numeric part
        num = (parseInt(num,10)+1).toString();
    // add 1 to that number
    while (num.length < len) num = '0' + num;
    // and adjust length if necessary so that e.g.
    // 032 will not become 33 but it will become 033
    //txtLayer.textItem.contents = '#' + num;
    txtLayer.textItem.contents =  num;
      };

 

This topic has been closed for replies.
Correct answer Charu Rajput

@Charu Rajput  Thank you for your interest and cooperation

Exactly what I really want

I want to repeat the process based on the number entered

Changes the number and then saves it based on the text layer content name

The change is repeated and saved until the last number is reached
Then the process stopped


You can try the following version

var _value = prompt('Enter range : ', '');
if (_value && _value.indexOf('-') != -1) {
    doc = app.activeDocument;
    var _start = Number(_value.split('-')[0]);
    var _end = Number(_value.split('-')[1]);
    for (var i = _start; i <= _end; i++) {
        var txtLayer = doc.layers[0];
        if (txtLayer) {
            var _contents = txtLayer.textItem.contents;
            var _oldNum = txtLayer.textItem.contents.match(/\d+/)[0];
            var len = _oldNum.length;
            var _newNum = i.toString();
            while (_newNum.length < len) {
                _newNum = '0' + _newNum;
            }
            txtLayer.textItem.contents = _contents.replace(_oldNum, _newNum);
            var _file = '~/Desktop/' + i + '.psd';
            app.activeDocument.saveAs(new File(_file));
        };
    }
}

 

Please note there is no validation in above, code I mean if you do not enter the range in format 1-50, the code not work, if you enter some text in the prompt, code will crash. So you can add the validation for these cases as required at your end.

1 reply

Charu Rajput
Community Expert
Community Expert
September 27, 2022

Hello,

Few things are not much clear in your question, but what I understand is you want to just update the number in the line by keeping the text as it is, if that is the case. Please try the follwoing version

 

doc = app.activeDocument;
// the front document

var txtLayer = activeDocument.layers[0];
// obviously, the text layer if found

if (txtLayer) {
    var _contents = txtLayer.textItem.contents;
    var _oldNum = txtLayer.textItem.contents.match(/\d+/)[0],
        // find the numeric part
        len = _oldNum.length,
        // find the length of the numeric part
        _newNum = (parseInt(_oldNum, 10) + 1).toString();
    // add 1 to that number
    while (_newNum.length < len) _newNum = '0' + _newNum;
    // and adjust length if necessary so that e.g.
    // 032 will not become 33 but it will become 033
    //txtLayer.textItem.contents = '#' + num;
    txtLayer.textItem.contents = _contents.replace(_oldNum, _newNum);

    //Only use when you want replace all numbers in the line
    // var _regEx = new RegExp(_oldNum, "g");
    // txtLayer.textItem.contents = _contents.replace(_regEx, _newNum);
};

 

 

Also, see last two lines of code which are commented, if you want to replace all 1 in the line with 2, then you to use global replace.

 

 var _regEx = new RegExp(_oldNum, "g");
 txtLayer.textItem.contents = _contents.replace(_regEx, _newNum);

 

 

The above will replace all 2 with 3. So for an example, if line is like, "Stage 2 Class 2" -> It will be "Stage 3 Class 3", if you use above two lines.

 

Best regards
Mohamed Hameed21513110
Inspiring
September 27, 2022

@Charu Rajput 

Yes, this code works very well, thank you

- but is it possible to make a continuation loop and implement it through a value prompt, for example I want the numbering operation from 1 for example to 50 or from 100 to 500

So that a continuum loop is made to change the number from 1 to 50, for example, and I'll include a repeat of this process, and save the file

I hope I got the idea if that was possible

Charu Rajput
Community Expert
Community Expert
September 27, 2022

So, you want,

You enter value 1-50 in the prompt.
1. After that, it will change number(whatever exists) to 1 and save

2. Again change number to 2 and save

3. It will go so on and forth till 50 and save.

 

Am I understanding correctly?

 

Best regards