skip to Main Content

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

3

Answers


  1. 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);
    }
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search