Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
Javascript Auto Increment Code not working

I need to generate 1000 images each with different numbers. Doing this is found a script online which works fine, but it doesn't work with 00 in front of the increments.

I can't just add 00 in front of every number because when it hits 10 it's doing 0010 instead of 010 like I want it to.

That means I need to change the code a bit. And it's probably REALLY basic, but I just can't figure it out. There is no log sadly, because I am running the script in Photoshop.

Here is the code I have trouble with. And underneath is the result:

  for (var i=1; i <= numImages; i++) {
    
    if(layer.textItem.contents < 10) {
      layer.textItem.contents = "00" + i.toString();
    } else if(layer.textItem.contents >= 10) {
     layer.textItem.contents = "0" + i.toString();
    } else {
      layer.textItem.contents = i.toString();
    }
    SavePNG(directory + imageName + '_'+ i +'.png');
  };

Any assistance is highly appreciated! I don't need to be fed by a spoon! I need to learn from my mistakes!

Image of the script result, 10 is not generated properly. As well as 1

Here is the entire code in the script (Forgot to add this, edited afterwards)

var imageName = 'Oppmoteskjema';
var numImages = 15;

function SavePNG(saveFile){
  var pngOpts = new ExportOptionsSaveForWeb; 
  pngOpts.format = SaveDocumentType.PNG
  pngOpts.PNG8 = false; 
  pngOpts.transparency = false; 
  pngOpts.interlaced = false; 
  pngOpts.quality = 10;
  activeDocument.exportDocument(new File(saveFile),ExportType.SAVEFORWEB,pngOpts); 
}

var layer = activeDocument.layers[0];
if (layer.kind == 'LayerKind.TEXT') {
    for (var i=1; i <= numImages; i++) {
      layer.textItem.contents = i.toString();
      var str = "" + i;
      var pad = "000";
      var ans = pad.substring(0, pad.length - str.length) + str;
      SavePNG(directory + imageName + '_'+ ans +'.png');
    }
};```
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could try it this way:

for (var i=1; i <= numImages; i++) {
    var str = "" + i;
    var pad = "000";
    var ans = pad.substring(0, pad.length - str.length) + str;
    layer.textItem.contents = ans;
    SavePNG(directory + imageName + '_'+ ans +'.png');
  };

You can change pad template as you wish. The output of this will be:

1 -> 001

97 -> 097

999 -> 999

1234 -> 1234

about 4 years ago · Juan Pablo Isaza Report

0

If you're just trying to manipulate a variable based off the index of the loop then the below would suffice.

for (var i = 1; i <= 1000; i++) {

    // Result will be '00' | '0' | ''
    const result = i < 10 ? '00' : i < 100 ? '0' : '';

    // The result is prepended to the current index
    // Aka (001, 010, 100) 
    const contents = result + i;

    // Set the text item object contents to value of contents 
    layer.textItem.contents = contents;

    // Saves the Image
    SavePNG(directory + imageName + '_' + contents + '.png');
}

This

i < 10 ? '00' : i < 100 ? '0' : ''

Is a ternary operator btw, it's essentially a shorthand if statement.

about 4 years ago · Juan Pablo Isaza Report

0

You can add the number to a string of zeroes, and then slice the resulting string based on desired length:

var template = "0000";
var num = 0;
var targetNumber = 1000;


for(let i = 0; i <= targetNumber; i++) {
    // add i to the template. Javascript will automatically to this type-coerceon since we're adding string + number
  let numStr = template + i;
  // strip leading zeroes to match the template length
  numStr = numStr.slice(numStr.length - template.length);
  console.log(numStr);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!