Skip to main content
July 24, 2009
Answered

JS CS4: Create variable for current layer then select the layer at the end of a script

  • July 24, 2009
  • 4 replies
  • 1611 views

I'm coming up blank with the verbiage and spinning my wheels with the OMV and script guide.

var myOriginalLayer = app.activeDocument.layers.item(0);
alert(myOriginalLayer)

returns "object layer" and not the layer's name.

var myOriginalLayer = app.activeDocument.layers.lastItem().name;

Does return the layer name I want but not by selection.

I'm challenged on how to select the layer.

This topic has been closed for replies.
Correct answer Jongware

Hi John,

The difference lies in that the [Object Layer] is the actual object, and the 'name' is just a property -- just like its color.

You can use one of these two approaches:

var myOriginalLayer = app.activeDocument.layers.item(0);
alert(myOriginalLayer.name);

will hold the original layer object, and you can see the name if required :-)

To re-select the layer, use

app.activeDocument.activeLayer = myOriginalLayer;

Alternatively, selecting by name goes like

var myOriginalLayerNAME = app.activeDocument.layers.lastItem().name;

...

app.activeDocument.activeLayer = app.activeDocument.layers.item(myOriginalLayerNAME);

I also noticed the following:

app.activeDocument.layers.item(0) points to the first layer of your document; not the 'active' one. Similarly, app.activeDocument.layers.lastItem() just points to the very last one. I'm not sure if the order of the activeDocument.layers array mimicks the order as shown in the Labels palette, but I would hope it does :-)

To get/set the active layer, use app.activeDocument.activeLayer as shown above.

4 replies

July 26, 2009

I apologize Jongware, I missed your last line:

"To get/set the active layer, use app.activeDocument.activeLayer as shown above."

Thank you Kasyan for spelling it out for me.

John

July 25, 2009

Thank you both for the help, it solves my need for the use of the script.

For my edification though:

"var myOriginalLayer = app.activeDocument.layers.lastItem().name;"

Returns the last layer in the list of layers but not the layer I have selected.

Is there a way to obtain the layer name that is selected?

Kasyan Servetsky
Legend
July 26, 2009

var mySelectedLayer = app.activeDocument.activeLayer.name;

Larry G. Schneider
Community Expert
Community Expert
July 25, 2009

I'm not sure if this helps but you can use app.activeDocument.layers.item(0).name to get the name of the layer but I'm still not sure how to use it.

Ok you have a more correct answer but the order is from the top down in the layers panel, ie the top layer is (0).

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
July 24, 2009

Hi John,

The difference lies in that the [Object Layer] is the actual object, and the 'name' is just a property -- just like its color.

You can use one of these two approaches:

var myOriginalLayer = app.activeDocument.layers.item(0);
alert(myOriginalLayer.name);

will hold the original layer object, and you can see the name if required :-)

To re-select the layer, use

app.activeDocument.activeLayer = myOriginalLayer;

Alternatively, selecting by name goes like

var myOriginalLayerNAME = app.activeDocument.layers.lastItem().name;

...

app.activeDocument.activeLayer = app.activeDocument.layers.item(myOriginalLayerNAME);

I also noticed the following:

app.activeDocument.layers.item(0) points to the first layer of your document; not the 'active' one. Similarly, app.activeDocument.layers.lastItem() just points to the very last one. I'm not sure if the order of the activeDocument.layers array mimicks the order as shown in the Labels palette, but I would hope it does :-)

To get/set the active layer, use app.activeDocument.activeLayer as shown above.