0

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');
    }
};```
Marius
  • 73
  • 1
  • 10
  • Does this answer your question? [Is there a JavaScript function that can pad a string to get to a determined length?](https://stackoverflow.com/questions/2686855/is-there-a-javascript-function-that-can-pad-a-string-to-get-to-a-determined-leng) – phuzi Sep 24 '21 at 09:14
  • 1
    Are you wanting the contents of the text layer to change or the resulting filename? – LoveDev Sep 24 '21 at 09:15
  • looks like what you want is just to have the numbers in a specific format. I think something like this would be helpful for you https://stackoverflow.com/questions/56186513/leading-and-trailing-zeros-in-numbers – stranded Sep 24 '21 at 09:17
  • Hi, you are adding "00" and "0" to `layer.textItems.content` but return `i` in your `SavePng` for the file name. Moreover i don't really understand the purpose of your `else` , if you check your `if` and `else if` condition you already cover all possibilities. Your will never entre the else statement – Guix Sep 24 '21 at 09:17
  • I am by no means expert at JS, I just thought else and else if runs whenever the value is greater than or less than, but this didn't work. @stranded I am attempting to change the contents of the image itself, I run the script in Photoshop so it will save the file with the huge number incrementing, but I need 00 when it's below 10 and 0 when it's above 10 and no zeroes when it's above 100. – Marius Sep 24 '21 at 10:10

3 Answers3

1

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

abellay
  • 368
  • 2
  • 11
  • So far this has worked to an extent, but because I am changing an actual image, I need to add `layer.textItem.contents = i.toString();` somewhere in the for statement. I added this above `var str = "" + i;` and it works, but it doesn't add the zeroes in front. It just saves it as 1-2-3-4-5 and not 001-002-003-004-005 now. So the code works, but it doesn't seem to run through the substring. – Marius Sep 24 '21 at 10:05
  • 1
    @Marius I don't know if I understand correctly but it seems like you need to only replace ```i.toString()``` with ```ans``` and place it before SavePNG. ```layer.textItem.contents = ans``` – abellay Sep 24 '21 at 11:27
1

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.

LoveDev
  • 257
  • 1
  • 9
  • I am attempting to edit the actual image, and this works the same as the code given below, it saves the image with 1-2-3-4-5 but it doesn't add 00 in front of the numbers. I need to add `layer.textItem.contents = i.toString();` but it doesn't add the zeroes I need infront. Any suggestions? – Marius Sep 24 '21 at 10:07
  • I've edited my answer above for elaboration. If you're only calling `i.toString` to set the value of `layer.textItem.contents` then it won't change. `i` is the index of your current loop iteration (1-1000 for you) so it'll always be a "basic" number. You need to set another variable based off your index and then set that as the value of the `layer.textItem.contents` property. – LoveDev Sep 24 '21 at 10:26
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);
}
Mikkel Christensen
  • 2,502
  • 1
  • 13
  • 22