Count Photoshop Layers: a script to find out how many layers and groups your Photoshop file has

posted 2013-Apr-10
  1. Save this script to your computer as “CountLayers.jsx”.
  2. Open the Photoshop document you want to count.
  3. From the File menu choose ScriptsBrowse and select the file.
  4. Wait (sometimes very patiently).

WARNING: Due to horrible efficiency by Adobe’s scripting interface this script may take a VERY long time to complete. On a test file with tens of groups and hundreds of layers the act of walking through the layers collection takes 10s or longer per layer during counting. Be sure to save your work before running this script (in case you have to force-quit Photoshop).

var s = countLayers(app.activeDocument);
alert(s.layers.on+"/"+s.layers.all+" layers are visible, " +
      "organized into "+s.groups.on+"/"+s.groups.all+" visible groups.");

/******************************************************************************
item: a Document or LayerSet
returns an object with the following properties:
  layers.all: the count of layers in the document/group
  layers.on:  the count of layers that are visible (eyeball on)
  layers.off: the count of layers that are not visible (eyeball off)
  groups.all: the count of groups in the document/group
  groups.on:  the count of groups that are visible (eyeball on)
  groups.off: the count of groups that are not visible (eyeball off)
/*****************************************************************************/
function countLayers(item,stats){
  var i;
  if (!stats) stats={layers:{on:0,off:0,all:0},groups:{on:0,off:0,all:0}};
  stats.layers.all += (i=item.layers.length);
  while (i--) stats.layers[item.layers[i].visible ? "on" : "off"]++;
  stats.groups.all += (i=item.layerSets.length);
  while (i--){
    stats.groups[item.layerSets[i].visible ? "on" : "off"]++;
    countLayers(item.layerSets[i],stats);
  }
  return stats;
}

Here’s a more feature-rich version that only counts layers and groups at the root by default, and does not count if visibility is on/off by default. (Checking the visibility of each layer and group can—astonishingly—be super slow on large files.)

var s = countLayers(app.activeDocument,{countChildren:true});
//alert(s.layers.on+"/"+s.layers.all+" layers are visible, " +
//      "organized into "+s.groups.on+"/"+s.groups.all+" visible groups.");
$.writeln(s.layers.on+"/"+s.layers.all+" layers are visible, " +
      "organized into "+s.groups.on+"/"+s.groups.all+" visible groups.");

/******************************************************************************
item: a Document or LayerSet
opts:
  countVisible:true/false (default:false)
  countChildren:true/false (default:false)

returns an object with the following properties:
  layers.all: the count of layers in the document/group
  layers.on:  the count of layers that are visible     (requires countVisible:true)
  layers.off: the count of layers that are not visible (requires countVisible:true)
  groups.all: the count of groups in the document/group
  groups.on:  the count of groups that are visible     (requires countVisible:true)
  groups.off: the count of groups that are not visible (requires countVisible:true)
/*****************************************************************************/
function countLayers(item,opts){
  if (!opts) opts={};
  var s = {layers:{on:0,off:0,all:0},groups:{on:0,off:0,all:0}},
      win   = new Window("window{text:'Counting Groups & Layers',"+
                         "bounds:[100,100,400,150],"+
                         "bar:Progressbar{"+
                         "bounds:[20,20,280,31],value:0,maxvalue:1}};");
  win.show();
  countItem(item,true);
  win.close();
  return s;

  function countItem(item,isRoot){
    $.writeln("Counting "+item.name);
    var l=item.layers, lc=l.length, g=item.layerSets, gc=g.length;
    s.layers.all += lc;
    s.groups.all += gc;
    if (opts.countVisible) for (var i=lc;i--;) s.layers[l[i].visible ? "on" : "off"]++;
    if (opts.countVisible) for (var i=gc;i--;) s.groups[g[i].visible ? "on" : "off"]++;
    if (opts.countChildren) for (var i=0;i<gc;++i){
      if (isRoot){
        win.bar.value = i/gc;
        $.sleep(5);
      }
      countItem(g[i]);
    }
  }
}
net.mind details other résumé contact
Phrogz.net