• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

automatic numbering of a part of the text

Engaged ,
Oct 13, 2022 Oct 13, 2022

Copy link to clipboard

Copied

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

Sample Attach.jpg

 

 

TOPICS
Actions and scripting , SDK

Views

49

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 13, 2022 Oct 13, 2022

HI @Mohamed Hameed ,

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 
...

Likes

Translate

Translate
Community Expert ,
Oct 13, 2022 Oct 13, 2022

Copy link to clipboard

Copied

HI @Mohamed Hameed ,

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 13, 2022 Oct 13, 2022

Copy link to clipboard

Copied

LATEST

@Charu Rajput 

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines