Skip to main content
thomasm96501510
Participating Frequently
February 14, 2017
Answered

Javascript Code to select by layer names in Photoshop File!! (using the Jam engine or otherwise)

  • February 14, 2017
  • 2 replies
  • 5093 views

I have a series of *.jsx files that run on the jam engine, to automatically create a series of image results from template files containing smart objects.

My question is!!!

Is there a way to select a specific layer/smart object, other than calling out the layer name?!! I ask because sometimes layernames are incorrectly renamed or deleted, and I would prefer a more failsafe method than depending on layer names to render files correctly.

Is it possible to select layers by color? Size? Position? Meta Data?

Or alternatively, is there a way in the code that I can lock the names of layers so it is impossible to change them?

A apologize for the vagueness of the question, I cannot find any options in the Photoshop API reference PDF

With much appreciation,

Thomas Murphy

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

Your question would be better suited in the "Photoshop Scripting" forum, and I will try to move it.

To answer your question: You can iterate over all layers in a document and get the layer name:

var layers = app.activeDocument.layers;

for (var i =0; i<layers.length; i++) {

  alert(layers.name);

}

2 replies

Jarda Bereza
Inspiring
February 15, 2017

You can select layer by:

1) index - which is position layers stack and it changes when you change layers order

2) ID - which is unique number for each layer and it doesn't change (shouldn't change)

3) name - not unique, not persistent

You put one from this into your code and you get layers instantly.

Otherwise you must go through all layers and read their data. This is much more time consuming, but it can be quite fast when this is written with AM approach. JAM engine is also fast if your descriptor is asking for only one property. Recursive DOM calling can be pretty slow if you have 500+ layers.

thomasm96501510
Participating Frequently
February 16, 2017

YES you are totally right and thank you. I was looking for the nomenclature for selecting a unique unchanging ID!

This should fit the bill then!

  1. function getActiveLayerId() {  
  2.     var ref = new ActionReference();  
  3.     ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "LyrI" ));  
  4.     ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
  5.     return executeActionGet(ref).getInteger( stringIDToTypeID( "layerID" ) ); 
Karl Heinz  Kremer
Community Expert
Karl Heinz KremerCommunity ExpertCorrect answer
Community Expert
February 14, 2017

Your question would be better suited in the "Photoshop Scripting" forum, and I will try to move it.

To answer your question: You can iterate over all layers in a document and get the layer name:

var layers = app.activeDocument.layers;

for (var i =0; i<layers.length; i++) {

  alert(layers.name);

}

thomasm96501510
Participating Frequently
February 15, 2017

Hi Karl,

Yes you are correct that would work to get all the layer names and then I could work from there.

What about this though?

I have a script that does the following:

1) looks for a series of specifically named layers with smart objects in one *.psd file.

2) so that in can export those smart objects to temporary folder (let's call it "textures")

3) NOW an entire other set of 50 images (that share the same smart objects with specific layer names) can be updated.

So you see these 50 images are expecting to share the same layer names of the templates, and if they don't, for example... if one of the layers in my template file are mis-named, then the "render" will fail.

As far as I can see it, Photoshop doesn't allow for "fuzzy name selection", where I can select a layer name that contains the word "foo" or "bar". That would help me the most.