Is it possible to count the number of layers selected in Photoshop?

I have a very large PS document with over 1100 layers. I have to arrange them in neat rows, but to accomplish that, I need to do some math to decide the best arrangement.

It would make the process much smoother, if it were readily possible to count the number of layers currently selected, making it easier to figure which layers to move, and where to move them, as they have to remain in their current order. I know how to see the number of total layers, but I can’t seem to find a way to automatically count the number in a given selection.

I
I’ve looked all over the software, so I’m afraid this just isn’t possible, but I appreciate any feedback anyone can give me!

Answer

Well you can use scripting, which is the way to go for this kind of stuff. Obviously I can not write the script for you because 1100 layers with some math to arrange in grid is not a actionable piece of description for a computer. Although I can guess what you want. I prefer not to since that will just lead to problems for me. So instead i will just push you on your way and hope you’re smart enough to lift yourself up by the bootstraps with this info.

I’m not going to go terribly into deep detail how you use scripting, you open extendscript toolkit and paste this in. When your PS is open, you run it by hitting the triangle on top of extendscript toolkit window.

So first finding out how many layers are selected (using this trick):

#target photoshop
var selectedLayers = getSelectedLayers(app.activeDocument);
alert(selectedLayers.length)

function cTID(s) { return app.charIDToTypeID(s); };
function sTID(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.length; i++) {
    selLayers.push(layers[i]);
  }

  undo();

  return selLayers;
};

Oh dear, Adobes scripting API is pretty useless. Ok now thet we know how many layers we have all we need to know is how to move a layer. Well that is pretty simple.

To move the topmost selected layer 10 units down from its curret position then do:

var pos = selectedLayers[0].bounds;
pos[1] = 10 - pos[1];
selectedLayers[0].translate(0 , 10);

From this follows that you can calculate a new position to all layers with something like:

var pos = selectedLayers[0].bounds;
pos[1] = 10 - pos[1];
selectedLayers[0].translate(0 , pos[1]);

From this follows that you can loop through each layer and do something for each position like so:

#target photoshop
var selectedLayers = getSelectedLayers(app.activeDocument);
alert(selectedLayers.length)


for( i = 0; i < selectedLayers.length; i++) {
    var pos = selectedLayers[0].bounds;
    pos[0] = yourmath - pos[0];
    pos[1] = yourmath - pos[1];
    selectedLayers[0].translate(pos[0] , pos[1]);
}



function cTID(s) { return app.charIDToTypeID(s); };
function sTID(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.length; i++) {
    selLayers.push(layers[i]);
  }

  undo();

  return selLayers;
};

Remeber to replace: yourmath with something better.

Personal opinion: you would be much better off doing this in InDesign or Illustrator. Not only is the script cleaner but also the operating environment is cleaner. But yes you can use Photoshop if you are interested in masochism and are a firm believer that all objects are nails when your wielding a hammer.

Attribution
Source : Link , Question Author : APD , Answer Author : Ovaryraptor

Leave a Comment