Skip to main content
marco mda
Participant
May 15, 2015
Answered

How to cope with a document with no selected layers

  • May 15, 2015
  • 3 replies
  • 288 views

Hello everyone,

I'm trying to write a script that needs to check the uniformity of each layer and rename each layer accordingly.

Apparently I managed to do that but it happens that if no layer is selected the script won't work.

So I came up with this solution (this is just a section of the script):

#target photoshop

if (app.documents.length) {

var strtRulerUnits = app.preferences.rulerUnits;

var strtTypeUnits = app.preferences.typeUnits;

app.preferences.rulerUnits = Units.PIXELS;

app.preferences.typeUnits = TypeUnits.PIXELS;

var docRef = app.activeDocument;

//DODGY WAY TO SELECT TOP LAYER

var ghostLyr = docRef.artLayers.add()

ghostLyr.move (docRef, ElementPlacement.PLACEATBEGINNING)

docRef.activeLayer.remove()

//DODGY WAY TO SELECT BACKGROUND

var ghostLyr = docRef.artLayers.add()

ghostLyr.move (docRef, ElementPlacement.PLACEATEND)

docRef.activeLayer.remove()

}

So basically I create an empty layer, place it on top or bottom and immediately delete it, so the layer beneath automatically selects.

Any better idea?

It looks like a workaround to me, it works, but it is... dodgy.

Thanks for all the help!

MD

This topic has been closed for replies.
Correct answer JJMack

You want to process all the layers so why not just target the bottom layer and work your way  the the layers.  If the document contains layer groups you will need to work through the layer in them as well.

var layers = activeDocument.layers;
activeDocument.activeLayer = layers[layers.length-1] // Target Bottom Layer

3 replies

marco mda
marco mdaAuthor
Participant
May 15, 2015

Thank you both for your help! :-)

This makes much more sense than my weird solution...

Chuck Uebele
Community Expert
Community Expert
May 15, 2015

Yes, as JJMack stated, just start at the bottom or top and work your way though the layers

var doc = activeDocument

doc.activeLayer = doc.layers[0]//selects the topmost layer.

JJMack
Community Expert
JJMackCommunity ExpertCorrect answer
Community Expert
May 15, 2015

You want to process all the layers so why not just target the bottom layer and work your way  the the layers.  If the document contains layer groups you will need to work through the layer in them as well.

var layers = activeDocument.layers;
activeDocument.activeLayer = layers[layers.length-1] // Target Bottom Layer
JJMack