Skip to main content
mickyhulse
Known Participant
December 1, 2015
Answered

Active and current layer

  • December 1, 2015
  • 1 reply
  • 984 views

Hello,

When looping over layers, what is the best way to:

1. Get the "active" layer?

2. Set a new layer as active?

Also, is there an easy way to get the index of the app.activeDocument.activeLayer when not looping over layers? In other words, can I determine the activeLayer's index without using a loop to find it?

Thanks!

Correct answer Silly-V

Setting the activeLayer property does the setting trick:

    doc.activeLayer = doc.layers[1];

And getting the index seems to work using the zOrderPosition property for getting the 1-based stacking order:

    alert(doc.activeLayer.zOrderPosition); // 2

1 reply

Silly-V
Silly-VCorrect answer
Legend
December 1, 2015

Setting the activeLayer property does the setting trick:

    doc.activeLayer = doc.layers[1];

And getting the index seems to work using the zOrderPosition property for getting the 1-based stacking order:

    alert(doc.activeLayer.zOrderPosition); // 2

mickyhulse
Known Participant
December 2, 2015

Awesome! Thanks Silly-V‌!

I can't believe I didn't think to set the activeLayer like that. It's so obvious now that you point it out. Lol.

+1 on the zOrderPosition. I saw that in the object viewer, but I wasn't sure if that was a reliable way to get the index. Much appreciated!

Also, for future readers, one way to check for active layer when looping over layers is:

_doc = app.activeDocument;

for (i = 0, l = _doc.layers.length; i < l; i++) {

  current = _doc.layers;

  if (current === _doc.activeLayer) {

  // The `current` layer is the `activeLayer`, so do something.

  }

}

Thanks a billion Silly-V‌, I totally appreciate all of your pro help!

Have a great week!


Cheers,

M

johnjoeparrot
Known Participant
April 9, 2025

Thanks! This helped find the selected layer

    var doc = app.activeDocument;
    var layerCount = doc.layers.length;

    // Get the currently selected (active) layer
    var activeLayer = doc.activeLayer;

    // Loop through all the layers
    for (var i = 0; i < layerCount; i++) {
        var currentLayer = doc.layers[i];

        // Check if the current layer is the same as the active layer
        if (currentLayer == activeLayer) {
           alert("The selected (active) layer is: " + currentLayer.name);
            // Perform any additional actions for the selected layer here
        } else {
           alert("This is not the active layer: " + currentLayer.name);
        }
    }