"targetLayers" ActionList alternative for CC/CS6

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

undavide

"targetLayers" ActionList alternative for CC/CS6

Post by undavide »

Hi,
I've a function to determine whether the current document has just a single active Layer (other possible values are multiple active layers or none of them):



Code: Select allvar hasSelectedLayer = function() {
  var desc, ref, s2t;
  s2t = function(s) {
    return app.stringIDToTypeID(s);
  };
  ref = new ActionReference();
  ref.putProperty(s2t("property"), s2t("targetLayers"));
  ref.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
  desc = executeActionGet(ref);
  if (!desc.hasKey(s2t("targetLayers"))) {
    return false;
  } else {
    desc = desc.getList(s2t("targetLayers"));
    if (desc.count === 1) {
      return true;
    }
    return false;
  }
};

I have to rely on ActionManager since in case there is no active layer, the DOM way (app.activeDocument.activeLayer) still gives you the topmost one in the stack.
The above function works on CC 2014, but fails on CC and CS6 because in the document descriptor there isn't a "targetLayers" ActionList, which I would inspect.

Is there a CC-CS6-compatible way to obtain the same result?
Thank you in advance for any help,

Davide Barranca
---
http://www.davidebarranca.com
http://www.cs-extensions.com
undavide

"targetLayers" ActionList alternative for CC/CS6

Post by undavide »

After some research, I think I have been approaching the problem from the wrong point of view.

The "targetLayers" key in the descriptor is present (on CS6, CC) only when more than 1 layer are active.
Conversely, it is always present on CC 2014 (its count is 0 if there's no active layer, 1 if there's 1, 300 if there's 300).

So my question can be rephrased this way: how do I check for zero active layers on CC and CS6?

Thank you,

Davide Barranca
---
http://www.davidebarranca.com
http://www.cs-extensions.com
xbytor

"targetLayers" ActionList alternative for CC/CS6

Post by xbytor »

You can have zero or more layers selected.
You can have only one active layer at any time, except for the first release of CS6 where a bug caused doc.activeLayer to not have a value. It had an effect on some scripts and no effect on others.
undavide

"targetLayers" ActionList alternative for CC/CS6

Post by undavide »

xbytor wrote:You can have zero or more layers selected.

Thanks xbytor,
I've used "active" but as I understand now I should have written "selected" - I've always some troubles with PS proper terminology (selected in my head involves a Selection, like Select All).
Anyway, the blue stuff in the screenshot
So, how do I check for zero layer selection on CS6/CC?
Thank you very much,

Davide
xbytor

"targetLayers" ActionList alternative for CC/CS6

Post by xbytor »

This should work. Extracted from http://ps-scripts.cvs.sourceforge.net/v ... /stdlib.js

Code: Select all  var selLayers = [];
  Stdlib.newGroupFromLayers(doc);
  var group = doc.activeLayer;
  var layers = group.layers;
  for (var i = 0; i < layers; i++) {
    selLayers.push(layers);
  }

  Stdlib.Undo();


If it doesn't (I haven't tested it), there is also a Stdlib.getSelectedLayers() function that I use instead.