PS Javascript script help: How to automate file save with layer names in file name?
- September 30, 2022
- 4 replies
- 1200 views
Disclaimer: I am not a programmer and is my first time working with anything related to Javascript. I do know some coding/programing terms but my knowledge is admittedly rather limited.
-
Hello, I would like to ask for help regarding a Javascript script. So I'm working on a personal creative project involving randomly generated/gachapon-style collectibles. My plan is to place the art assets into folders in Photoshop and use this free script I found to automatically generate images using layers that are randomly selected from each folder. (Link goes to video explaining how to use script. My project is not NFT-related, by the way, but the script happens to suit my needs for this project.)
I did not create this script (credit goes to benlaorg on Youtube), but it is free to download and use, so I will share it here:
function Visible(){var Grps=app.activeDocument.layerSets;for(var i=0;i<Grps.length;i++){var tmp=app.activeDocument.layerSets[i].layers.length;app.activeDocument.layerSets[i].visible=true;var groupChildArr=app.activeDocument.layerSets[i].layers;var randLays=Math.floor(Math.random()*tmp);groupChildArr[randLays].visible=true;Save();}
Revert();}
function Save(){var outFolder=app.activeDocument;var outPath=outFolder.path;var fName="PNG";var f=new Folder(outPath+"/"+fName);if(!f.exists){f.create()}
var saveFile=new File(outPath+"/"+fName+"/"+"image-"+num+".png");pngSaveOptions=new PNGSaveOptions();pngSaveOptions.interlaced=false;app.activeDocument.saveAs(saveFile,pngSaveOptions,true,Extension.LOWERCASE);}
function Revert(){var idRvrt=charIDToTypeID("Rvrt");executeAction(idRvrt,undefined,DialogModes.NO);}
var count=prompt("How many versions you want","");for(var x=0;x<count;x++){var num=x+1;Visible();}
The image generation itself works as intended. It's the automated file naming for each generated image that I wish to change. By default, the images are saved as image-num.png, with "num" being a variable (as you can see in the code). I would like the file name to be something like image-num_layer1name_layer2name_layer3name.png. So for example, a generated image using the layers B01, P03, E02, and A03 would be named: image-248_B01_P03_E02_A03.png.
For context, each layer in my .psd has an certain name/code that corresponds to a different trait. It's like a DNA code that will be used to track and sort these collectibles according to the traits they have. As such, it's important that these trait codes are present in the file names. Here's a section of my .psd folder setup for example.
Is that even possible? I'm guessing I need to make variables that change depending on name of the layers that are randomly selected from each folder. But I have no idea how to even begin doing that. I haven't had any luck finding resources relating to this, hence why I'm asking here.
I'm not sure this is something I can ask here or if this even makes sense. I figured I'd give it a shot anyway. If anyone is willing to lend their expertise, I would greatly and truly appreciate it. Thank you in advance for your time.
(and in case it's needed, I made a quick dummy file set up similarly to mine for reference and testing purposes if anyone wants to test the script themselves. I'll attach it to this post.)
I'm also using Photoshop CS6 on MacOS if that matters.

