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
Loop through all layers & layersets
Loop through all layers & layersets
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;
}
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;
}
Loop through all layers & layersets
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?
Loop through all layers & layersets
Hi Patrick,
It's stdlib.js. It should be in the xlib subfolder
Mike
It's stdlib.js. It should be in the xlib subfolder
Mike
Loop through all layers & layersets
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().
Loop through all layers & layersets
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
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
Loop through all layers & layersets
Thanks a lot, I updated the stdlib and it works great!
Patrick
Patrick
Loop through all layers & layersets
Hello xbytor. How can use all layers (including layers in layersets) without Stdlib and xtools? Thanks.
Loop through all layers & layersets
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.
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.
Loop through all layers & layersets
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?
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?