Skip to main content
Known Participant
June 9, 2022
Answered

Find and select layer by an specific word in its name script

  • June 9, 2022
  • 3 replies
  • 784 views

Hi everyone! I searched here in the community but couldn't find what I need or something I could change to what I need.

I would be grateful if anyone can help


I have some files, with several layers, but I need to select a specific layer in each of these files.

The names vary, but the ones I need have the final _final in the names.

 

Ex: Layer 123456_final, another layer, in another file, 5678_final, another abcd_final and so on. the names vary, but the _final is always the same within the files.

 

Is there any script that recognizes the _final and selects that layer regardless of the name that comes before it?

 

Thanks!

This topic has been closed for replies.
Correct answer jazz-y

more letters, but faster:

 

#target photoshop
s2t = stringIDToTypeID;
(ref = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
ref.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var len = executeActionGet(ref).getInteger(p);
for (var i = 1; i <= len; i++) {
    (ref = new ActionReference()).putProperty(s2t('property'), p = s2t('name'));
    ref.putIndex(s2t('layer'), i);
    if ((/_final$/i).test(executeActionGet(ref).getString(p))) {
        (ref = new ActionReference()).putProperty(s2t('property'), p = s2t('layerID'));
        ref.putIndex(s2t('layer'), i);
        (r = new ActionReference()).putIdentifier(s2t("layer"), executeActionGet(ref).getInteger(p));
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        executeAction(s2t("select"), d, DialogModes.NO);
        break;
    }
}

 

or less letters, but slower:

 

lrs=[].slice.call((ad=activeDocument).layers)
while (l=lrs.shift()) if(/_final$/i.test(l.name)) {ad.activeLayer=l; break;}

 

 

3 replies

Stephen Marsh
Community Expert
Community Expert
June 9, 2022

@rrprec  – The first question that comes to mind is: what will you do with the selected layer?

 

The next question is – will there only be 1 layer ending in "_final" – or if there were multiple layers ending in "_final" do you need all of them to be selected? 

rrprecAuthor
Known Participant
June 11, 2022

Hi Stephen. Thanks for answering. There Will be lots of layers with that name. I will select one and run an action that changes the name. So i can loop the script to all layers um the file. 

Thanks!

Stephen Marsh
Community Expert
Community Expert
June 11, 2022

Thanks, I was wondering "what was next"... Please share your final code.

jazz-yCorrect answer
Legend
June 9, 2022

more letters, but faster:

 

#target photoshop
s2t = stringIDToTypeID;
(ref = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
ref.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var len = executeActionGet(ref).getInteger(p);
for (var i = 1; i <= len; i++) {
    (ref = new ActionReference()).putProperty(s2t('property'), p = s2t('name'));
    ref.putIndex(s2t('layer'), i);
    if ((/_final$/i).test(executeActionGet(ref).getString(p))) {
        (ref = new ActionReference()).putProperty(s2t('property'), p = s2t('layerID'));
        ref.putIndex(s2t('layer'), i);
        (r = new ActionReference()).putIdentifier(s2t("layer"), executeActionGet(ref).getInteger(p));
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        executeAction(s2t("select"), d, DialogModes.NO);
        break;
    }
}

 

or less letters, but slower:

 

lrs=[].slice.call((ad=activeDocument).layers)
while (l=lrs.shift()) if(/_final$/i.test(l.name)) {ad.activeLayer=l; break;}

 

 

Zesty_wanderlust15A7
Known Participant
June 9, 2022

Just a hint... I asked something related in this thread, and the solution seems to be the 'split', which seems followed by regex...
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-delete-the-last-recent-layer-comps-specify-amount-to-a-func/td-p/12066626/
I had asked to remove only Layer Comps starting with TMP_ (or some specific character, like – )

Bojan Živković11378569
Community Expert
Community Expert
June 9, 2022

It seems @Kukurykus and @r-bin were most active on above mentioned thread so I will mentioned them to draw their attention. Let me add and @c.pfaffenbichler @Stephen Marsh