Select all layers with same name script

Explorer ,
May 21, 2021 May 21, 2021

Copy link to clipboard

Copied

Hi,

 

I have found the code from the following link https://community.adobe.com/t5/photoshop/select-layer-by-similar-name-script/m-p/8784041 that selects all layers with the same name:

 

cTID = function(s) {return app.charIDToTypeID(s);};
sTID = function(s) {return app.stringIDToTypeID(s);};      
if(app.documents.length>0){
    var docRef = app.activeDocument;
    docRef.activeLayer=docRef.backgroundLayer;
    var myLyr = docRef.artLayers;
        for (var i = 0; i < myLyr.length; i++) {
            if(myLyr.name.indexOf("Layer")!=-1){
                addtoSelection(myLyr.name);
            }
        }
}
function addtoSelection(lyName){
    var desc71 = new ActionDescriptor();
    var ref65 = new ActionReference();
    ref65.putName( cTID( "Lyr " ), lyName );
    desc71.putReference( cTID( "null" ), ref65 );
    desc71.putEnumerated( sTID( "selectionModifier" ), sTID( "selectionModifierType" ), sTID( "addToSelection" ));
    desc71.putBoolean( cTID( "MkVs" ), false );
    executeAction( cTID( "slct" ), desc71, DialogModes.NO );
}

 

The code is stopping at line 8 and throwing the following error:

Error 1302: No such element

Line 8:

->      if(myLyr.name.indexOf("Layer")!=-1){

 

Why is this error showing and how to fix it?

My simple question is how to search and select all layers with the same name by a script.

 

Thank you.

TOPICS
Actions and scripting, How to, Make It, Windows

Views

292

Likes

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

Adobe Community Professional , May 21, 2021 May 21, 2021
Replace in two places myLyr.name with myLyr[i].name upd. >>>My simple question is how to search and select all layers with the same name by a script. But it is not clear what exactly and how you want

Likes

Translate

Translate
Adobe Community Professional ,
May 21, 2021 May 21, 2021

Copy link to clipboard

Copied

Replace in two places

 

myLyr.name

 

with

 

myLyr[i].name

 

upd.

>>>My simple question is how to search and select all layers with the same name by a script.

But it is not clear what exactly and how you want

Likes

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
Explorer ,
May 21, 2021 May 21, 2021

Copy link to clipboard

Copied

LATEST

Thank you r-bin, this solves the problem. I will use this code to achieve certain selections of many layers with the same name and then will apply certain filter on them.

Likes

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