get array of selected layers

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

Moderators: Tom, Kukurykus

lukasz

get array of selected layers

Post by lukasz »

Is it possible to get a list of selected layers? activeLayer is closest I can find to getting selected layers, but this doesn't allow me to get multiples.
xbytor

get array of selected layers

Post by xbytor »

Something like this should work. I sliced it out of xtools/xlib/stdlib.js.

Code: Select allcTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };

function newGroupFromLayers(doc) {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putClass( sTID('layerSection') );
    desc.putReference( cTID('null'), ref );
    var lref = new ActionReference();
    lref.putEnumerated( cTID('Lyr '), cTID('Ordn'), cTID('Trgt') );
    desc.putReference( cTID('From'), lref);
    executeAction( cTID('Mk  '), desc, DialogModes.NO );
};

function undo() {
   executeAction(cTID("undo", undefined, DialogModes.NO);
};

function getSelectedLayers(doc) {
  var selLayers = [];
  newGroupFromLayers();

  var group = doc.activeLayer;
  var layers = group.layers;

  for (var i = 0; i < layers; i++) {
    selLayers.push(layers);
  }

  undo();

  return selLayers;
};
lukasz

get array of selected layers

Post by lukasz »

just FYI I had to add a parenthesis that were missing if anyone else finds this.
I tried to run this, maybe I don't understand how it works but I am getting error (using photoshop CC):

Code: Select allGeneral photoshop error occurred, This functionality may not be available in this version of photoshop.

Here is what I am putting in as a test:

Code: Select all
cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };

function newGroupFromLayers(doc) {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putClass( sTID('layerSection') );
    desc.putReference( cTID('null'), ref );
    var lref = new ActionReference();
    lref.putEnumerated( cTID('Lyr '), cTID('Ordn'), cTID('Trgt') );
    desc.putReference( cTID('From'), lref);
    executeAction( cTID('Mk  '), desc, DialogModes.NO );
};

function undo() {
   executeAction(cTID("undo", undefined, DialogModes.NO));
};

function getSelectedLayers(doc) {
  var selLayers = [];
  newGroupFromLayers();

  var group = doc.activeLayer;
  var layers = group.layers;

  for (var i = 0; i < layers; i++) {
    selLayers.push(layers);
  }

  undo();

  return selLayers;
};

var selectedLayers = getSelectedLayers(app.activeDocument);
alert(selectedLayers);

xbytor

get array of selected layers

Post by xbytor »

There was one more JS error:
Code: Select all for (var i = 0; i < layers; i++) {

should be
Code: Select all for (var i = 0; i < layers.length; i++) {

Just tried it out with CC 2014 and it seems to be working fine.
lukasz

get array of selected layers

Post by lukasz »

ah, that did the trick, not sure how I missed that one. thanks!