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

A bug or misunderstanding in array counting?

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

TOPICS
Actions and scripting

Views

468

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

Guide , Oct 14, 2020 Oct 14, 2020

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

...

Votes

Translate

Translate
Adobe
People's Champ ,
Oct 14, 2020 Oct 14, 2020

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

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

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

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
Guide ,
Oct 14, 2020 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]

 

 

 

 

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

Copy link to clipboard

Copied

LATEST

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.

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