Skip to main content
Participant
February 5, 2021
Answered

How to select sublayer group by name?

  • February 5, 2021
  • 2 replies
  • 988 views

Hi community!

How can I select all the items in the sublayer groups called "color" and "shadow"

My main group name in this example called "education-props" is different across all files. Only "color" and "shadow" is consistent and I need the items in these subgroups.

I'm completely new with scripting. Your help is much appreciated!

 

 



[ attachment inserted as inline image by moderator ]

This topic has been closed for replies.
Correct answer ellewong

Oh i think i figured it!

var docRef = app.activeDocument;
var layers = docRef.layers;

docRef.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.
for(var a=0;a<docRef.groupItems.length;a++){
if (docRef.groupItems[a].name == "shadow"){
docRef.groupItems[a].selected = true;
}
if (docRef.groupItems[a].name == "color"){
docRef.groupItems[a].selected = true;
}
}

2 replies

pixxxelschubser
Community Expert
Community Expert
February 5, 2021

Good finding.

😉

 

Another variant (will be helpful if you have dozens of hundreds of groups)

var aDoc = app.activeDocument;
aDoc.selection = null;
// make sure the layer exists!  or also use a try catch clause
var aLay = aDoc.layers.getByName("education-props");

var grp = null;
try {grp = aLay.groupItems.getByName("shadow");
    grp.selected = true;
    }
catch (e) {alert ("""there is no "shadow" group""")}
try {grp = aLay.groupItems.getByName("color");
    grp.selected = true;
    }
catch (e) {alert ("""there is no "color" group""")}
Participating Frequently
April 29, 2022

Danke dir, @pixxxelschubser deine Zeilen haben mir auch schon ein wenig weitergeholfen. Da ich leider überhaupt keinen Plan vom scripten habe kommt hier meine Frage: 

Wie wähle ich eine Ebene aus, die noch eine Stufe tiefer sitzt?

In meinem Fall die Ebene "Background". 

 

Danke schon mal im Voraus!

 

Just saw this was an english thread: 

I want to select the sublayer "Background" in the image above. 

Sergey Osokin
Inspiring
May 2, 2022

What should the script do if the document contains many layers of the same name?

ellewongAuthorCorrect answer
Participant
February 5, 2021

Oh i think i figured it!

var docRef = app.activeDocument;
var layers = docRef.layers;

docRef.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.
for(var a=0;a<docRef.groupItems.length;a++){
if (docRef.groupItems[a].name == "shadow"){
docRef.groupItems[a].selected = true;
}
if (docRef.groupItems[a].name == "color"){
docRef.groupItems[a].selected = true;
}
}

Inspiring
October 12, 2022

This worked for me as well, been hours looking for something like this, thanks!