Skip to main content
Mohamed Hameed21513110
Inspiring
October 13, 2022
Answered

automatic numbering of a part of the text

  • October 13, 2022
  • 1 reply
  • 445 views

Welcome everyone
All thanks and appreciation to Charu Rajput
Because he helped me with this code

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);
};

But I faced a problem when I worked on a file with text that contains numbers and text as well as numbers
I want to number a certain part of the text while preserving the rest of the text and numbers
I have inserted a picture showing the shaded part that I want to number sequentially
I also included the code and a file to explain the whole idea

 

 

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

HI @Mohamed Hameed21513110 ,

I am assuming you just need to update the last numbers after - So try the following script.

 

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 _tempArray = _contents.split('-');
            var _oldNum = _tempArray[_tempArray.length - 1];
            var len = _oldNum.length;
            var _newNum = i.toString();
            while (_newNum.length < len) {
                _newNum = '0' + _newNum;
            }
            txtLayer.textItem.contents = _contents.replace(new RegExp(_oldNum + '$'), _newNum);
            var _file = '~/Desktop/' + i + '.psd';
            app.activeDocument.saveAs(new File(_file));
        };
    }
}

I hope it helps.

 

1 reply

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
October 13, 2022

HI @Mohamed Hameed21513110 ,

I am assuming you just need to update the last numbers after - So try the following script.

 

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 _tempArray = _contents.split('-');
            var _oldNum = _tempArray[_tempArray.length - 1];
            var len = _oldNum.length;
            var _newNum = i.toString();
            while (_newNum.length < len) {
                _newNum = '0' + _newNum;
            }
            txtLayer.textItem.contents = _contents.replace(new RegExp(_oldNum + '$'), _newNum);
            var _file = '~/Desktop/' + i + '.psd';
            app.activeDocument.saveAs(new File(_file));
        };
    }
}

I hope it helps.

 

Best regards
Mohamed Hameed21513110
Inspiring
October 13, 2022

@Charu Rajput 

very great your work
Your effort is very great
Thank you for your interest