Highlighted

A bug or misunderstanding in array counting?

Explorer ,
Oct 14, 2020

Copy link to clipboard

Copied

Hello,

I  am trying to understand the logic here after we all know that the first elements in an array in JavaScript is starting to be counted from 0, the second with 1 and so on. In the below example things are different:

 

layer = activeDocument.layers[0];
alert(layer);

 

When I create more than one layer, the topmost layer is the first layer, i.e. zero layer. Why? I need explanation on why is this happening? Maybe I have missed something in the guides?

 

Thanks,

Damian

r-bin, documents are also stored in a special object - a collection, but there 0 is always an older object 🙂

 

mimozemská hůl, perhaps this feature of the presentation of a collection of layers is due to the fact that the presentation of layers in the DOM is closely related to the ActionManager, where indexing starts from 0, then from 1 (depending on the presence of a background layer). When representing a collection as an array, this 0-1 feature can cause certain problems, which is probably why we decided to index them in the reverse order.

 

You should distinguish between the layer index (property activeDocument.activeLayer.itemIndex) and the index of the collection item. On page 14 of Introduction to Scripting, apparently, it is written about the itemIndex layer property, and not about the indexes of the collection itself.

 

to select bottom layer:

 

 

activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length-1]

 

 

 

 

TOPICS
Actions and scripting, Bug, How to, Problem or error

Views

214

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

A bug or misunderstanding in array counting?

Explorer ,
Oct 14, 2020

Copy link to clipboard

Copied

Hello,

I  am trying to understand the logic here after we all know that the first elements in an array in JavaScript is starting to be counted from 0, the second with 1 and so on. In the below example things are different:

 

layer = activeDocument.layers[0];
alert(layer);

 

When I create more than one layer, the topmost layer is the first layer, i.e. zero layer. Why? I need explanation on why is this happening? Maybe I have missed something in the guides?

 

Thanks,

Damian

r-bin, documents are also stored in a special object - a collection, but there 0 is always an older object 🙂

 

mimozemská hůl, perhaps this feature of the presentation of a collection of layers is due to the fact that the presentation of layers in the DOM is closely related to the ActionManager, where indexing starts from 0, then from 1 (depending on the presence of a background layer). When representing a collection as an array, this 0-1 feature can cause certain problems, which is probably why we decided to index them in the reverse order.

 

You should distinguish between the layer index (property activeDocument.activeLayer.itemIndex) and the index of the collection item. On page 14 of Introduction to Scripting, apparently, it is written about the itemIndex layer property, and not about the indexes of the collection itself.

 

to select bottom layer:

 

 

activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length-1]

 

 

 

 

TOPICS
Actions and scripting, Bug, How to, Problem or error

Views

215

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

Copy link to clipboard

Copied

It is not an array, it is a special object - a collection.
 

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...
Oct 14, 2020 1
Explorer ,
Oct 14, 2020

Copy link to clipboard

Copied

Thank you r-bin, but I need more clarification on this matter.

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...
Oct 14, 2020 0
Explorer ,
Oct 14, 2020

Copy link to clipboard

Copied

An excerpt from Introduction to Scripting on page 14 it says:

 

"The layer’s index is [0]. If you add a layer, the new layer’s index is [1]; if you add a second layer, its index is [2]".

 

I am totaly lost by what you are saying and why it is not acting like an array.

 

How to select the bottom layer?

 

Why when changing from:

activeDocument.layers.getByName('LayerName');

to:

activeDocument.layers[0];

the whole script execution process stops working although the above code is contained in an separate function?

 

Please, if your will allows, explanation would come in handy to all who are willing to learn, not only to me.

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...
Oct 14, 2020 0
Enthusiast ,
Oct 14, 2020

Copy link to clipboard

Copied

r-bin, documents are also stored in a special object - a collection, but there 0 is always an older object 🙂

 

mimozemská hůl, perhaps this feature of the presentation of a collection of layers is due to the fact that the presentation of layers in the DOM is closely related to the ActionManager, where indexing starts from 0, then from 1 (depending on the presence of a background layer). When representing a collection as an array, this 0-1 feature can cause certain problems, which is probably why we decided to index them in the reverse order.

 

You should distinguish between the layer index (property activeDocument.activeLayer.itemIndex) and the index of the collection item. On page 14 of Introduction to Scripting, apparently, it is written about the itemIndex layer property, and not about the indexes of the collection itself.

 

to select bottom layer:

 

 

activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length-1]

 

 

 

 

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...
Oct 14, 2020 1
Explorer ,
Oct 15, 2020

Copy link to clipboard

Copied

Thank you Dmitry,

Actually I was trying the same as in your example, but obviously have missed to subtract 1 from the length by accident.

Now what you and r-bin have explained, things are a bit more clear, although PS scripting is a bit foggy for me.

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...
Oct 15, 2020 0