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

NoomForPhotoshop
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.
https://drive.google.com/drive/folders/ ... sp=sharing

Thank you folks, please help me out

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

Re: Photoshop Script failed at looping through layers

Post by txuku »

Bonjour

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

User avatar
jaydoubleyou80
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
takeSnapshot()
var myDoc=app.activeDocument
var myColor = new SolidColor
    myColor.rgb.red = 255
    myColor.rgb.green = 0
    myColor.rgb.blue = 0
var fileName=myDoc.name;
var shortName=fileName.split(".")[0]
pngSaveOptions = new PNGSaveOptions();

for(a=0;a<myDoc.layers.length;a++){
    myDoc.layers[a].visible=false;
}

for(b=0;b<myDoc.layers.length;b++){
    myDoc.layers[b].visible=true;
    var layerName=myDoc.layers[b].name;
    var saveFolder=Folder(myDoc.path + "/" + layerName);
    var saveName=(shortName+","+layerName)
    if(!saveFolder.exists){
        saveFolder.create();
        }
    var layers=myDoc.artLayers;
    var textLayer=layers.add();
        textLayer.kind=LayerKind.TEXT;
    var textItem=textLayer.textItem;
        textItem.kind=TextType.POINTTEXT;
        textItem.color=myColor;
        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);
    textLayer.remove()
    myDoc.layers[b].visible=false;
    }

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

   var sref = new ActionReference();  // Snapshot
   sref.putClass(cTID("SnpS"));
   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');      
    }
revertToFirstSnapshot()
app.preferences.rulerUnits = originalUnit

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

Re: Photoshop Script failed at looping through layers

Post by txuku »

Bonjour

Thanks to you !

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