3 Replies Latest reply on May 15, 2015 8:16 AM by marco mda

    How to cope with a document with no selected layers

    marco mda Level 1

      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