Photoshop Script failed at looping through layers

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Kukurykus, Tom, xbytor

Posts: 6
Joined: Thu Sep 03, 2020 6:06 pm

Photoshop Script failed at looping through layers

Post by NoomForPhotoshop »

Hello, I have a script that does the following:

loops through layers,
saves each layer in a separate folder (name of folder same as layer name)
saved layer images have names "nameofthedocument, nameofthelayer.png"

Now I wanted to add text item on top of those layers while they are saving as separate pngs so that for gods sake you don't have to always look at the file name but instead there would be text in the PNG image rasterized and says "nameofthelayer" variable.

So for each layer of course different text should be inserted in the image.

i encountered weird problems once I tried to do that what seamigly looked easy.

HEre is the link with a video and an explanation of the code as well as what I did and why it messed up the script. ... sp=sharing

Thank you folks, please help me out

User avatar
Posts: 134
Joined: Thu Jan 01, 1970 12:00 am

Re: Photoshop Script failed at looping through layers

Post by txuku »


A written script is easier to consider than a video for a foreigner ??? :)

User avatar
Posts: 20
Joined: Mon Oct 17, 2016 1:41 pm
Location: USA

Re: Photoshop Script failed at looping through layers

Post by jaydoubleyou80 »

I had a hard time modifying your script, so I just rewrote it. The below doesn't need you to turn the layers off, and will revert to the original state after completing the saves.

It's also not recursive to look into layer groups, but that could be added. It cycles through turning off all layers visibility, then cycles through turning on one layer, creating the text for it, saving a png, deleting the text layer, and turning off the layer visibility again. It uses the source file location to create the folders, the source file name to prepend the layer name, and uses a comma to separate.

I think that's all the stuff you were looking for, let me know if you run into any problems.

Code: Select all

#target Photoshop
var originalUnit = preferences.rulerUnits
preferences.rulerUnits = Units.PIXELS
var myDoc=app.activeDocument
var myColor = new SolidColor = 255 = 0 = 0
var shortName=fileName.split(".")[0]
pngSaveOptions = new PNGSaveOptions();


    var layerName=myDoc.layers[b].name;
    var saveFolder=Folder(myDoc.path + "/" + layerName);
    var saveName=(shortName+","+layerName)
    var layers=myDoc.artLayers;
    var textLayer=layers.add();
    var textItem=textLayer.textItem;
        textItem.position = [0,20]
        textItem.justification = Justification.LEFT
        textItem.size = 12
        textItem.contents = shortName + ", " + layerName;
    myDoc.saveAs (new File (saveFolder+"/"+saveName+".png"),pngSaveOptions,true, Extension.LOWERCASE);

function cTID(s) { return app.charIDToTypeID(s); }
function takeSnapshot () {
   var desc = new ActionDescriptor();  // Make

   var sref = new ActionReference();  // Snapshot
   desc.putReference(cTID("null"), sref);

   var fref = new ActionReference();  // From current history state
   fref.putProperty(cTID("HstS"), cTID("CrnH"));
   desc.putReference(cTID("From"), fref );

   executeAction(cTID("Mk  "), desc, DialogModes.NO );
function revertToFirstSnapshot() {
    myDoc.activeHistoryState = myDoc.historyStates.getByName('Snapshot 1');      
app.preferences.rulerUnits = originalUnit

User avatar
Posts: 134
Joined: Thu Jan 01, 1970 12:00 am

Re: Photoshop Script failed at looping through layers

Post by txuku »


Thanks to you !

It's clearer but I still have no idea !!! :?