• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Feb 14, 2017 Feb 14, 2017

Copy link to clipboard

Copied

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

TOPICS
Actions and scripting

Views

4.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 14, 2017 Feb 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);

}

Votes

Translate

Translate
Adobe
Community Expert ,
Feb 14, 2017 Feb 14, 2017

Copy link to clipboard

Copied

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);

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 15, 2017 Feb 15, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 15, 2017 Feb 15, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 16, 2017 Feb 16, 2017

Copy link to clipboard

Copied

LATEST

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" ) ); 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines