Loop through all layers & layersets

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

kezani

Loop through all layers & layersets

Post by kezani »

The APIs for this are in the Reference Manual. Or you could download xtools and dissect the functions in stdlib.js. Or copy the code out of stdlib.js for use in your own scripts.

I try, but doesn't work )

Code: Select allStdlib.traverseLayers = function(doc, ftn, reverse, layerSets) {

  function _traverse(doc, layers, ftn, reverse, layerSets) {
    var ok = true;
    for (var i = 1; i <= layers.length && ok != false; i++) {
      var index = (reverse == true) ? layers.length-i : i - 1;
      var layer = layers[index];

      if (layer.typename == "LayerSet") {
        if (layerSets) {
          ok = ftn(doc, layer);
        }
        if (ok) {
          ok = _traverse(doc, layer.layers, ftn, reverse, layerSets);
        }
      } else {
        ok = ftn(doc, layer);
        try {
          if (app.activeDocument != doc) {
            app.activeDocument = doc;
          }
        } catch (e) {
        }
      }
    }
    return ok;
  };

  return _traverse(doc, doc.layers, ftn, reverse, layerSets);
};

Stdlib.getLayersList = function(doc, reverse, layerSets) {
  function _ftn(doc, layer) {
    _ftn.list.push(layer);
    return true;
  };

  _ftn.list = [];
  Stdlib.traverseLayers(doc, _ftn, reverse, layerSets);

  var lst = _ftn.list;
  _ftn.list = undefined;
  return lst;
};

var layers = Stdlib.getLayersList(doc, false, true);

for (var i = 0; i < layers.length; i++) {
   var layer = layers;

   layer.blendMode = BlendMode.NORMAL;
}


Error - Stdlib is undefined

If i delete all Stdlib. then next error is - doc is undefined

I don't understand this.. sorry. Can you help me?

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

xbytor

Loop through all layers & layersets

Post by xbytor »

Put

Code: Select allStdlib = {};

at the top of the script.

This will call

Code: Select allvar list = Stdlib.getLayersList(app.activeDocument, false, true);

will get the list of all layers and layer sets in the current document.
kezani

Loop through all layers & layersets

Post by kezani »

It works. Thank you X!