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

Select all layers with the same name

Guest
Dec 03, 2013 Dec 03, 2013

Copy link to clipboard

Copied

I'm setting up a bunch of actions in PS Cs5 but I've hit a wall with the way photoshop selects layers by name.

If I name two layers "example" and have an action that selects "example" it will only select the bottom layer.

Is there a workaround using actions? And if no, is there a way to do this by script?

(Word of warning, I have never tried anything with photoshop scripting so assume I know nothing about it.)

TOPICS
Actions and scripting

Views

4.0K

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

Community Expert , Dec 06, 2013 Dec 06, 2013

This should select all Layers that have the same name as the activeLayer.

Not sure it this is what you were after, though, pixelpuncher.

// based on code by paul riggott and mike hale;

// 2013, use it at your own risk;

if (app.documents.length > 0) {

activeDocument.suspendHistory('stuff', 'main()');

function main(){

if(!documents.length) return;

var myDoc = app.activeDocument;

var theName = myDoc.activeLayer.name;

var theResults = new Array;

selectAllLayers();

var selectedLayers = getSelect

...

Likes

Translate

Translate
Guru ,
Dec 03, 2013 Dec 03, 2013

Copy link to clipboard

Copied

It may be not what you want to hear but the best workaround is to have unique layer names.

Otherwise you have to loop thoroug all the layers in the document and collect matching names. That is often slow, sometimes very slow.

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
Community Expert ,
Dec 03, 2013 Dec 03, 2013

Copy link to clipboard

Copied

It’s no Scripting solution but couldn’t the Layer Filter in the Layers Panel be used manually?

Otherwise you have to loop thoroug all the layers in the document and collect matching names. That is often slow, sometimes very slow.

Well, the Action Manager code approaches that (if I remeber correctly) you and Paul have presented seem fairly quick in contrast to DOM-approaches.

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
Guest
Dec 04, 2013 Dec 04, 2013

Copy link to clipboard

Copied

Unique layer names will be a pain and prone to usererror, I was hoping to make an action that would just always work without having to train people to name the layers in very specific ways.

Still, if no other option exists I guess I'll have to.

As for the slow script, how would I go about doing that? I'd like to at least test it. Also, is it possible to narrow the search to just group names? I'd guess that would speed up the search. Actions don't make the distinction between groups and normal layers, but maybe it's possible with scripting?

I'm not sure what you mean by Layer Filter, are you talking about the new feature in cs6? I'm still stuck with cs5.

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
Community Expert ,
Dec 04, 2013 Dec 04, 2013

Copy link to clipboard

Copied

I'm not sure what you mean by Layer Filter, are you talking about the new feature in cs6? I

It was introduced in CS6.

Edit: Sorry, I had paid no attention to the reference to CS5 in the original post, but the Layer Filter seems not to be Action-able anyways …

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
Guru ,
Dec 04, 2013 Dec 04, 2013

Copy link to clipboard

Copied

c.pfaffenbichler wrote:

Well, the Action Manager code approaches that (if I remeber correctly) you and Paul have presented seem fairly quick in contrast to DOM-approaches.

Yes Action Manager can be faster and there are plenty examples of searching the layer stack using either the DOM or Action Manager.

But even then you need some way to know which layer named "example" should be used.

Is it really that hard to name the layers "top example" and "bottom example" or "example1" and "example2"?

If the layer structure is always the same you could refer to the layers by index( position in the layer stack ) and not worry about how they are named.

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
Community Expert ,
Dec 06, 2013 Dec 06, 2013

Copy link to clipboard

Copied

This should select all Layers that have the same name as the activeLayer.

Not sure it this is what you were after, though, pixelpuncher.

// based on code by paul riggott and mike hale;

// 2013, use it at your own risk;

if (app.documents.length > 0) {

activeDocument.suspendHistory('stuff', 'main()');

function main(){

if(!documents.length) return;

var myDoc = app.activeDocument;

var theName = myDoc.activeLayer.name;

var theResults = new Array;

selectAllLayers();

var selectedLayers = getSelectedLayersIdx();

for(var a = 0; a < selectedLayers.length; a++){

var thisName = layerName(Number(selectedLayers[a]));

if (thisName == theName) {

          theResults.push(Number(selectedLayers[a]))

          };

};

selectLayerByIndex(theResults[0], false);

for (var m = 1; m < theResults.length; m++) {

selectLayerByIndex(theResults[m], true);

};

}

};

function getSelectedLayersIdx(){

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

                     }

           }

           return selectedLayers;

};

function selectAllLayers() {

var desc29 = new ActionDescriptor();

          var ref23 = new ActionReference();

          ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

desc29.putReference( charIDToTypeID('null'), ref23 );

executeAction( stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO );

};

function layerName(idx){

var ref = new ActionReference();

ref.putIndex( charIDToTypeID( "Lyr " ), idx);

var desc = executeActionGet(ref);

return desc.getString(stringIDToTypeID("name"));

};

function selectLayerByIndex(index,add){

add = undefined ? add = false:add

var ref = new ActionReference();

    ref.putIndex(charIDToTypeID("Lyr "), index);

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

}

};

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
Guest
Dec 10, 2013 Dec 10, 2013

Copy link to clipboard

Copied

Wow, that looks more complicated then I expected. I'll have to test it, but if it does what you say it does, it's exactly what I need. Thanks!

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
Participant ,
Nov 18, 2022 Nov 18, 2022

Copy link to clipboard

Copied

Hi,

I tried to run this code on Illustrator 2023. It ends up with this:

Error 24: activeDocument.suspendHistory is not a function.

Line: 7

--> activeDocument.suspendHistory("stuff", "main()");

could you check, where might be the problem?

Thanks
(My mistake, its for Photoshop and not for Illustrator and I need the script for Illustrator)

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
Community Expert ,
Nov 18, 2022 Nov 18, 2022

Copy link to clipboard

Copied

Have you checked on the Illustrator Forum for a corresponding Script? 

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
Participant ,
Nov 18, 2022 Nov 18, 2022

Copy link to clipboard

Copied

LATEST

I just did, thanks

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
Participant ,
Jul 07, 2021 Jul 07, 2021

Copy link to clipboard

Copied

Hi!

I am making an action to create my Start file that I use on projects.

I have duplicated a group multiple times with layers, and each group has an unique mask, but similar layers. I want to select all layers with the same name that searches all layers sets.

Capture4.JPG

 

I found this script, but I get this error:

Captur3e.JPG

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
Participant ,
Jul 07, 2021 Jul 07, 2021

Copy link to clipboard

Copied

URL to thread I found Script: Select all layers with the same name

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
LEGEND ,
Jul 07, 2021 Jul 07, 2021

Copy link to clipboard

Copied

It's fixed. Try again.

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
Valorous Hero ,
Jul 07, 2021 Jul 07, 2021

Copy link to clipboard

Copied

var r_idx = new ActionReference();
var match_cnt = 0;

var text = prompt("Layer name:", "", "Input");

if (text != null)
    {
    var d = new ActionDescriptor();
    var r = new ActionReference();
    r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
    d.putReference(stringIDToTypeID("null"), r);
    executeAction(stringIDToTypeID("selectNoLayers"), d, DialogModes.NO);

    var r = new ActionReference();
    r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("count"));
    r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
    var count = executeActionGet(r).getInteger(stringIDToTypeID("count"));

    var n = 0; 
    try { app.activeDocument.backgroundLayer; } catch(e) { n = 1; ++count; }

    var r_idx = new ActionReference();
    var do_it = false;

    for (var i = count-1; i >= n; i--)
        {
        var r = new ActionReference();
        r.putIndex(stringIDToTypeID("layer"), i);
        var d = executeActionGet(r);

        if (d.getInteger(stringIDToTypeID("layerKind")) != 1) continue; // olny pixel layers

        if (d.getString(stringIDToTypeID("name")).indexOf(text) >= 0) { r_idx.putIndex(stringIDToTypeID("layer"), i); ++match_cnt; } 
        }
    
    if (match_cnt)
        {
        var d = new ActionDescriptor();
        d.putReference(stringIDToTypeID("null"), r_idx);
        executeAction(stringIDToTypeID("select"), d, DialogModes.NO);
        }

    alert(match_cnt + " \"" + text + "\" layers were selected");
    }

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