Skip to main content
Known Participant
December 13, 2018
Question

get current layer ID

  • December 13, 2018
  • 1 reply
  • 1790 views

Hello

I want to get the activeLayer Id and then move to top layer one by one (have to do some actions on each layer).

My problem is I don't know how get the ID of current layer.

I found these syntaxes but seems none of them working:

app.activeDocument.activeLayer.length

getLayerIndex(app.activeDocument.activeLayer)

any help will be appreciate

This topic has been closed for replies.

1 reply

Legend
December 14, 2018

in CC

app.activeDocument.activeLayer.id

dc-masterAuthor
Known Participant
December 14, 2018

it works fine on unlock layers but when the active layer is a locked layer, it returns a different number (like 175)

let me explain a bit more, maybe you have a better suggestion:

I have some PSD file with several layers. I want to do an action on each layer. it have to start from current layer and continue to top layer (the action will not apply on  layers).

I thought by getting the layer ID, and use it in a loop, can visible each layer, do the action, hide the layer and then go to next layer.

Legend
December 14, 2018

Maybe it helps

var i0 = activeDocument.activeLayer.itemIndex;

var i1 = activeDocument.layers[0].itemIndex;

var di = 1;

try { activeDocument.backgroundLayer; } catch(e) { di = 0; }

for (var i = i0; i <= i1; i++)

    {

    select_layer(i-di);

    action();

    }

alert("Done");

function action()

    {

    // do something

    // refresh();

    }

function select_layer(idx)

    {

    try {

        var r = new ActionReference();

        r.putIndex(stringIDToTypeID("layer"), idx);

        var d = new ActionDescriptor();

        d.putReference(stringIDToTypeID("null"), r);

        executeAction(stringIDToTypeID("select"), d, DialogModes.NO);

        return true;

        }

    catch (e) { alert(e); return false; }

    }