Any JavaScript experts here? or Photoshop

Graphic46

Active member
Oct 2, 2018
401
88
28
Hi all !
I need a script that will write all the letters one by one on the blank background and save it. I searched the internet and found this below code. it numbers one by one to any number. but I need A to Z letter.

how to change code for to sort alphabetically ?
so this code export images = #1 #2 #3 #4 #5 ...
but i need A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

can someone help me? thanks.

JavaScript:
function main() {

//this just checks if you have a text layer selected
try {
    var textLayer = activeDocument.activeLayer.textItem
} catch (e) {
    alert("active layer isn't a text layer");
    return
};

// PNG save options to use below, using defaults
var pngOpts = new PNGSaveOptions();
pngOpts.compression = 1;
pngOpts.interlaced = false;

var loops = 5,
    outputFolder = Folder.selectDialog('', Folder.desktop); //this will ask for an output folder

for (var i = 0; i < loops; i++) {
    var myNum = i + 1;
    textLayer.contents = "#" + myNum; //this will change layer contents to number only. if you need some text here, write it in quotes like textLayer.contents = "my text" + myNum;
    activeDocument.saveAs(new File(outputFolder + "/#" + myNum + ".png"), pngOpts, true, Extension.LOWERCASE);
}

}
app.activeDocument.suspendHistory("temp", "main()");
 

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me
I think the following might help you.

The charcode for letter capital A = 65, B = 66, C = 67 and so on. So, you change the base number from 0 to 64 and then convert the incrementing number to the letter with String.fromCharCode which results in this code (only the 'for' section from your example):

JavaScript:
for (var i = 64; i < loops; i++) {
    var myNum = i + 1;
    var myLetter = String.fromCharCode(myNum);
    textLayer.contents = "#" + myLetter; //this will change layer contents to LETTER only. if you need some text here, write it in quotes like textLayer.contents = "my text" + myLetter;
    activeDocument.saveAs(new File(outputFolder + "/#" + myLetter + ".png"), pngOpts, true, Extension.LOWERCASE);
}

Hope this helps, please let me know.

EDIT: this of course results in #A, #B a.s.o. You probably don't want that so just leave that hash character out or replace with anything you want there.
 
Last edited:

About us

  • Our community has been around for many years and pride ourselves on offering unbiased, critical discussion among people of all different backgrounds. We are working every day to make sure our community is one of the best.

Quick Navigation

User Menu