Skip to main content
New Participant
March 9, 2013
Answered

How to select active layer?

  • March 9, 2013
  • 2 replies
  • 2802 views

It appears that in order to resize the layer it has to be selected(from UI perspective) and active from API perspective. Otherwise I get error on any function call that this function is not supported.

So before resizing I do

var a = doc.artLayers.getByName("iPad");

app.activeDocument.activeLayer = a;

This doesn't visually change selected layer hence calling resize function fails after that. The only way to get it work, manually click on layer(any layer), then it works.

What is the proper way to resize layer without user interaction?

This topic has been closed for replies.
Correct answer

Hi Pashkatarakashka

Now try this code......

var docRef = app.activeDocument;

docRef.activeLayer=docRef.backgroundLayer;

var layerRef=docRef.layers.getByName("iPad");

docRef.activeLayer=layerRef;

layerRef.resize(120, 120, AnchorPosition.MIDDLECENTER);

-yajiv

2 replies

Kukurykus
Braniac
July 19, 2018

Probably I don't understand but your original code to select layer worked for me.

March 9, 2013

Hi Pashkatarakashka

Try this Code...

var docRef = app.activeDocument;

var layerRef=docRef.layers.getByName("iPad");

docRef.activeLayer=layerRef;

-yajiv

New Participant
March 9, 2013

Hi yajiv,

Still the same error that this function may not be available.

var docRef = app.activeDocument;

var layerRef=docRef.layers.getByName("iPad");

docRef.activeLayer=layerRef;

layerRef.resize(120, 120, AnchorPosition.MIDDLECENTER);

Again, if I click on any layer manually before running the script, it works. But if no layer is selected it fails.

Kukurykus
Braniac
July 19, 2018

Hi, what about the file without background layer?  I can't use getByName to "click on" the layer I want correctly without target background layer first. It seems weird to me that "getByName" is not really visually select layer.


I don't know why with background that doesn't work for others, but with no background there is a bug indeed. This works:

var docRef = app.activeDocument;

//     docRef.activeLayer=docRef.backgroundLayer;

var layerRef=docRef.layers.getByName("iPad");

docRef.activeLayer=layerRef;

Without layerSets it should work too for (no) background versions:

$.level = 0; try{

     aD.activeLayer = (aD = activeDocument).backgroundLayer

} catch(err){}

aL = aD.layers.getByName('iPad'), aD.activeLayer = aL