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

Script for selecting layers with the same name

Explorer ,
Oct 18, 2019 Oct 18, 2019

Copy link to clipboard

Copied

Hello!

 

Here is my situation, I'd like to be able to launch a script for selecting all the layers with the name A

within my layer stack:

 

Layer Selection.JPG

I've already seen several posts speaking about the same question, but for some reason I'm not able to use the scripts who have been shared.  I'm using ExtendScript Toolkit CS5 with Photoshop CC 2019, could it be the reason?

 

Thanks!

TOPICS
Actions and scripting

Views

4.0K

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 2 Correct answers

Community Expert , Oct 18, 2019 Oct 18, 2019

Try this:

#target photoshop
var doc = activeDocument;
var curLayer = doc.activeLayer;
var layName = curLayer.name;
var selcList = new Array();

var layerSets = 0
try{doc.backgroundLayer}
catch(e){layerSets=1}

var fullList = getLayerSetsData ();

for(var i=0;i<fullList.length;i++){
    if(fullList[i].name.toLowerCase() == layName.toLowerCase()){
        selcList.push(fullList[i].id)
        }
    }

if(selcList.length>1){multiSelectByIDs (selcList)}
else{alert('There are no duplicate layer names
...

Votes

Translate

Translate
Community Expert , Oct 19, 2019 Oct 19, 2019
This would work also on with multiple selected layers: 
 
// select layers of same name as active layers’;
// 2019, use it at your own risk;
#target photoshop-130.064
if (app.documents.length > 0) {
app.activeDocument.suspendHistory("selectSameNames", "main ()");
};
////////////////////////////////////
function main () {
var myDocument = app.activeDocument;
// the file;
var theSelectedLayers = getSelectedLayersIdxAndName ();
//var theLayerName = myDocument.activeLayer.name;
// get number of layers;
var ref = n
...

Votes

Translate

Translate
Adobe
Community Expert ,
Oct 18, 2019 Oct 18, 2019

Copy link to clipboard

Copied

Since it looks like your file has several different layer names that are duplicates, how to you want to select just one set of them - by using the current selected layer?

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
Explorer ,
Oct 18, 2019 Oct 18, 2019

Copy link to clipboard

Copied

Well, the idea would be to be able to select all the layers with the same name, so that I can move them into the same folder at the same time (the file I'm working on currently contains hundreds of layers, and it's impossible to select rapidly all the layers with the same name).

 

Are you saying it sounds impossible to do that script (sorry I'm not a programer)?

I tried to create a script manually on Photoshop but it selects systematically the layer A at the bottom of the stack and only this one.

 

Thanks for your help!

 

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 Expert ,
Oct 18, 2019 Oct 18, 2019

Copy link to clipboard

Copied

No, it's possible to get all layers with duplicate name, just wondering what exactly you want.

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
Explorer ,
Oct 18, 2019 Oct 18, 2019

Copy link to clipboard

Copied

Ok! Let me explain step by step:

 

Whatever layer is selected in the layer stack, I like to be able to select all the layers with the same name in the layer stack, thanks to a script button, and that's all.

 

1:

01.JPG

2: I press the script (than I'll name "Select A") and it selects all the other layers named A:

 

02.JPG

 

And then, this shouldn't be included in the script because I'm using a timeline in Photoshop,

putting all those layers in the folder at the bottom:

 

03.JPG

 

And I will do the same thing with an other script but for B, and then for C and so forth.

 

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 Expert ,
Oct 18, 2019 Oct 18, 2019

Copy link to clipboard

Copied

Okay, that's what I thought would make more sense than selecting any layers that have the same name, like all the A's and B's in your example. It would be easier for just one set at a time.

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 Expert ,
Oct 18, 2019 Oct 18, 2019

Copy link to clipboard

Copied

Try this:

#target photoshop
var doc = activeDocument;
var curLayer = doc.activeLayer;
var layName = curLayer.name;
var selcList = new Array();

var layerSets = 0
try{doc.backgroundLayer}
catch(e){layerSets=1}

var fullList = getLayerSetsData ();

for(var i=0;i<fullList.length;i++){
    if(fullList[i].name.toLowerCase() == layName.toLowerCase()){
        selcList.push(fullList[i].id)
        }
    }

if(selcList.length>1){multiSelectByIDs (selcList)}
else{alert('There are no duplicate layer names')};

function doesIdExists( id ){// function to check if the id exists
   var res = true;
   var ref = new ActionReference();
   ref.putIdentifier(charIDToTypeID('Lyr '), id);
    try{var desc = executeActionGet(ref)}catch(err){res = false};
    return res;
}

function multiSelectByIDs(ids) {
  if( ids.constructor != Array ) ids = [ ids ];
    var layers = new Array();
    var id54 = charIDToTypeID( "slct" );
    var desc12 = new ActionDescriptor();
    var id55 = charIDToTypeID( "null" );
    var ref9 = new ActionReference();
    for (var i = 0; i < ids.length; i++) {
       if(doesIdExists(ids[i]) == true){// a check to see if the id stil exists
           layers[i] = charIDToTypeID( "Lyr " );
           ref9.putIdentifier(layers[i], ids[i]);
       }
    }
    desc12.putReference( id55, ref9 );
    var id58 = charIDToTypeID( "MkVs" );
    desc12.putBoolean( id58, false );
    executeAction( id54, desc12, DialogModes.NO );
}

function getLayerSetsData()
{
    //var count = 0;//set counter for multi-dimensional array
    var lyrSets = [];

    while (true)
    {
        ref = new ActionReference();
        ref.putIndex(charIDToTypeID('Lyr '), layerSets);
        try
            {var d1 = executeActionGet(ref)}
        catch (err){
            break;
            };

        var c2t = function (s){return app.charIDToTypeID(s);};
        var s2t = function (s){return app.stringIDToTypeID(s);};
        var lyrSet = {};

        lyrSet.type = d1.getInteger(s2t("layerKind"));
        lyrSet.name = d1.getString(c2t("Nm  "));
        lyrSet.id = d1.getInteger(s2t("layerID"));
        
        lyrSets.push(lyrSet);
        layerSets++;
    }; 
    return lyrSets;
};

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
Explorer ,
Oct 18, 2019 Oct 18, 2019

Copy link to clipboard

Copied

Thank you very much Chuck!

 

I just tried it and I get the message "there are no duplicate layer names"

Did I make something wrong?

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
Explorer ,
Oct 18, 2019 Oct 18, 2019

Copy link to clipboard

Copied

Oh, actually it works fine!!! I haven't selected any of the layers duplicated (like A or B) but

I was on C (which was the only layer with that name - the others were copy C, copy2 C, etc).

 

THANKS SO MUCH CHUCK!

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 Expert ,
Oct 18, 2019 Oct 18, 2019

Copy link to clipboard

Copied

Glad it's working for you.

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 ,
Jun 01, 2022 Jun 01, 2022

Copy link to clipboard

Copied

I know this is an old post, any chance we can do it REGEX. Pluging works really good on same layer names. But you normaly make a copy of the layer to control PSB and creates a copy example from "Logo Smart" to "Logo Smart copy 31"

 

I tried to read your code but didn't understand how its working. So if we can have 1 that gets all the layers with Same Name, having any other text on layers. a type of REGEX thing, would appreciate.

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 Expert ,
Jun 01, 2022 Jun 01, 2022

Copy link to clipboard

Copied

Wouldn’t changing 

if(fullList[i].name.toLowerCase() == layName.toLowerCase()){

to 

if(fullList[i].name.toLowerCase().match(layName.toLowerCase()) != -1){

suffice? 

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 ,
Jun 01, 2022 Jun 01, 2022

Copy link to clipboard

Copied

@c.pfaffenbichler 

Thanks for the quick replay and taking your time to help.

Your code is selecting all layers strangely. 😞

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 Expert ,
Jun 01, 2022 Jun 01, 2022

Copy link to clipboard

Copied

What code exactly? 

I was referring to a line in @Chuck Uebele ’s Script. 

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 Expert ,
Jun 01, 2022 Jun 01, 2022

Copy link to clipboard

Copied

One can avoid the adding of »copy x« to duplicated Layers’ names by unchecking »Add "copy" to Copied Layers and Groups«. 

Screenshot 2022-06-01 at 16.45.54.png

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 ,
Jun 01, 2022 Jun 01, 2022

Copy link to clipboard

Copied

LATEST

Thanks this helps alot for future PS works. TY.

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 Expert ,
Oct 19, 2019 Oct 19, 2019

Copy link to clipboard

Copied

This would work also on with multiple selected layers: 
 
// select layers of same name as active layers’;
// 2019, use it at your own risk;
#target photoshop-130.064
if (app.documents.length > 0) {
app.activeDocument.suspendHistory("selectSameNames", "main ()");
};
////////////////////////////////////
function main () {
var myDocument = app.activeDocument;
// the file;
var theSelectedLayers = getSelectedLayersIdxAndName ();
//var theLayerName = myDocument.activeLayer.name;
// get number of layers;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
theLayers.push([theName, theID])
};
}
catch (e) {};
};
////////////////////////////////////
for (var o = 0; o < theLayers.length; o++) {
var thisLayer = theLayers[o];
for (var p = 0; p < theSelectedLayers.length; p++) {
var theLayerName = theSelectedLayers[p][1];
// select layer is name matches;
if (thisLayer[0] == theLayerName) {
selectLayerByID(thisLayer[1], true)
}
}
};
};
// based on code by mike hale, via paul riggott;
function selectLayerByID(id,add){ 
add = undefined ? add = false:add 
var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID("Lyr "), id);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref );
       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
      desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
   try{
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message); 
}
};
 
////// get array of arrays of smart objects witrh index, center and half-dimensions //////
function getSelectedLayersIdxAndName(){
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count;
var selectedLayers = new Array();
for(var i=0;i<c;i++){
try{ 
activeDocument.backgroundLayer;
selectedLayers.push(  desc.getReference( i ).getIndex() );
}catch(e){
selectedLayers.push(  desc.getReference( i ).getIndex()+1 );
};
}
 }else{
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" )); 
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
try{ 
activeDocument.backgroundLayer;
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
}catch(e){
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
};
};
////////////////////////////////////
var theArray = new Array;
var theIDs = new Array;
for (var m = 0; m < selectedLayers.length; m++) {
var thisIndex = selectedLayers[m];
var ref = new ActionReference();
ref.putIndex( charIDToTypeID("Lyr "), thisIndex); 
var layerDesc = executeActionGet(ref);
var thisID = layerDesc.getInteger(stringIDToTypeID("layerID"));
var theName = layerDesc.getString(stringIDToTypeID("name"));
var theVisibility = layerDesc.getInteger(stringIDToTypeID("visible"));
// is visible;
if (theVisibility == true) {
theIDs.push ([thisID, theName])
}
};
////////////////////////////////////
//return;
return theIDs
////////////////////////////////////
};

 

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
Explorer ,
Oct 19, 2019 Oct 19, 2019

Copy link to clipboard

Copied

Hi Pfaffenbichler! 

 

Your script also worked super well, thanks!!

It upgrades the Chuck's one, that's great!                   

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