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!
CodePudding user response:
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);
}
CodePudding user response:
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;
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