layer = activeDocument.layers; 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?
Thank you r-bin, but I need more clarification on this matter.
An excerpt from Introduction to Scripting on page 14 it says:
"The layer’s index is . If you add a layer, the new layer’s index is ; if you add a second layer, its index is ".
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:
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.
Copy link to clipboard
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]
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.