Highlighted

Question regarding the object model, specifically regarding [Layers] vs [GroupItems]

Engaged ,
Jun 13, 2020

Copy link to clipboard

Copied

The guide states that "the layer and group item classes can contain nested objects of the same class which can, in turn, contain additional nested objects".

 

Untitled666.png

 

So I understand why I use recursion to iterate over nested layers.

 

But I can iterate over all groupItems, including nested groupItems, directly without recursion.  (I presume that "child" groupItems are added to the "parent" groupItems collection.)

 

My question is: Why the difference?

 

Thanks in advance.

 

Untitled667.png

 

var theLayers = activeDocument.layers;
var theList = "";
for (var i = 0; i < theLayers.length; i++) {
  theList = theList + theLayers[i].name + "\r";
}
alert(theList);//Layer 1 Layer 2 (i.e. parent layers only)
var theItems = activeDocument.groupItems;
var theList = "";
for (var i = 0; i < theItems.length; i++) {
  theList = theList + theItems[i].name + "\r";
}
alert(theList);//A1 A2 A2a A2a1 (i.e. parent and child groupItems)

 

Adobe Community Professional
Correct answer by CarlosCanto | Adobe Community Professional

Hi, I don't know the reason why it works the way you described, but I'm glad it does. The more options we have the better for us. The currrent behaviour comes in very handy at times.

 

If you only want to target top levels groups (excluding nested groups) loop through layers, then through groups in each layer.

 

At the document level, every groupItem is available wheter is nested or not. It's the same behaviour other pageItems have. But I hear you, layers/sublayers have their own logic.

TOPICS
Scripting

Views

155

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

Question regarding the object model, specifically regarding [Layers] vs [GroupItems]

Engaged ,
Jun 13, 2020

Copy link to clipboard

Copied

The guide states that "the layer and group item classes can contain nested objects of the same class which can, in turn, contain additional nested objects".

 

Untitled666.png

 

So I understand why I use recursion to iterate over nested layers.

 

But I can iterate over all groupItems, including nested groupItems, directly without recursion.  (I presume that "child" groupItems are added to the "parent" groupItems collection.)

 

My question is: Why the difference?

 

Thanks in advance.

 

Untitled667.png

 

var theLayers = activeDocument.layers;
var theList = "";
for (var i = 0; i < theLayers.length; i++) {
  theList = theList + theLayers[i].name + "\r";
}
alert(theList);//Layer 1 Layer 2 (i.e. parent layers only)
var theItems = activeDocument.groupItems;
var theList = "";
for (var i = 0; i < theItems.length; i++) {
  theList = theList + theItems[i].name + "\r";
}
alert(theList);//A1 A2 A2a A2a1 (i.e. parent and child groupItems)

 

Adobe Community Professional
Correct answer by CarlosCanto | Adobe Community Professional

Hi, I don't know the reason why it works the way you described, but I'm glad it does. The more options we have the better for us. The currrent behaviour comes in very handy at times.

 

If you only want to target top levels groups (excluding nested groups) loop through layers, then through groups in each layer.

 

At the document level, every groupItem is available wheter is nested or not. It's the same behaviour other pageItems have. But I hear you, layers/sublayers have their own logic.

TOPICS
Scripting

Views

156

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
Jun 13, 2020 0
Adobe Community Professional ,
Jun 14, 2020

Copy link to clipboard

Copied

Hi, I don't know the reason why it works the way you described, but I'm glad it does. The more options we have the better for us. The currrent behaviour comes in very handy at times.

 

If you only want to target top levels groups (excluding nested groups) loop through layers, then through groups in each layer.

 

At the document level, every groupItem is available wheter is nested or not. It's the same behaviour other pageItems have. But I hear you, layers/sublayers have their own logic.

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
Reply
Loading...
Jun 14, 2020 2