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

Patrick

Loop through all layers & layersets

Post by Patrick »

Does anyone know of a method to loop through every layer and layerset in a document, including nested layers and layersets? I am trying to make a function that will look at everything and check what it's blending mode is set to, and have it set it to normal if it is anything other than that.

Patrick

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

xbytor

Loop through all layers & layersets

Post by xbytor »

Code: Select allStdlib.getLayersList(doc, reverse, incLayerSets)
Returns an array containing all layers in the document.
If 'reverse' is true, the layers are in reverse order. Default is false.
If 'incLayerSets' is true, layer sets are included in the array. Default is false.

Ex:
Code: Select allvar layers = Stdlib.getLayersList(doc, false, true);

for (var i = 0; i < layers.length; i++) {
   var layer = layers;
   // layer processing code goes here....

   // just assign the mode, don't bother checking first
   layer.blendMode = BlendMode.NORMAL;
}
Patrick

Loop through all layers & layersets

Post by Patrick »

Thanks for the reply. I just downloaded xtools for the first time, which file would I need to include in the script to use StdLib?
Mike Hale

Loop through all layers & layersets

Post by Mike Hale »

Hi Patrick,

It's stdlib.js. It should be in the xlib subfolder

Mike
Patrick

Loop through all layers & layersets

Post by Patrick »

Hmm.. I am getting the error getLayersList is not a function. I looked in stdlib.js and I don't see it in there, closest I could find was traveserLayers().
xbytor

Loop through all layers & layersets

Post by xbytor »

You need to go through CVS Browse at sourceforge.net to get the most current version.

http://ps-scripts.cvs.sourceforge.net/* ... /stdlib.js ... /stdlib.js

Most of the stuff in described in the Stdlib.pdf file.

Stdlib.getLayersList is just a convenient wrapper around traverse list.

Note that since it returns an array, you can do stuff like this:
Code: Select allvar layers = Stdlib.getLayersList(doc);

// get all text layers
var textLayers = Stdlib.getByProperty(layers, "kind", LayerKind.TEXT, true);

// get all visible layers
var textLayers = Stdlib.getByProperty(layers, "visible", true, true);

// get all layers with 'Image' in the name
var textLayers = Stdlib.getByName(layers, /Image/, true);


-X
Patrick

Loop through all layers & layersets

Post by Patrick »

Thanks a lot, I updated the stdlib and it works great!

Patrick
kezani

Loop through all layers & layersets

Post by kezani »

Hello xbytor. How can use all layers (including layers in layersets) without Stdlib and xtools? Thanks.
xbytor

Loop through all layers & layersets

Post by xbytor »

kezani wrote:Hello xbytor. How can use all layers (including layers in layersets) without Stdlib and xtools? Thanks.

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.
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?