Copy link to clipboard
Copied
Hi fellas. It's been a while!
I tend not to use groups (layer sets) myself, but other people do. I've got several scripts that loop over all layers in a psd and then peform various tasks.
Of course they fall over if layersets are involved; so I need to change that.
To test this out I wrote a script and made a simple psd; New document (any size), background, one layer (called "layer 1") and another layer ("layer 2") which is in a layer set ("a_group")
I've got a basic script here that loops through all layers (or so I thought)
<code>
var srcDoc = app.activeDocument;
var numOfLayers = srcDoc.layers.length;
for (var i = numOfLayers -1; i >= 0 ; i--)
{ var layerName = srcDoc.layers.name;
// check for groups
if (srcDoc.layers.typename == "LayerSet")
{
alert (layerName + " is a group")
}
else
{
alert (layerName + " is not in a group")
}
</code>
The alert calls all layer names as it goes up the stack. It however misses out layer 2 (which is in a group) So what am I missing out?
Cheers
Copy link to clipboard
Copied
I think you need a function that calls itself, because Layers in LayerSets are children of the LayerSet and not the document.
var theLayers = collectLayers(app.activeDocument, []);
alert (theLayers.join("\n"));
////// function collect all layers //////
function collectLayers (theParent, allLayers) {
if (!allLayers) {var allLayers = new Array}
else {};
for (var m = theParent.layers.length - 1; m >= 0;m--) {
var theLayer = theParent.layers
; // apply the function to layersets;
if (theLayer.typename == "ArtLayer") {
allLayers.push(theLayer)
}
else {
allLayers = (collectLayers(theLayer, allLayers))
// this line includes the layer groups;
allLayers.push(theLayer);
}
};
return allLayers
};
Copy link to clipboard
Copied
The script reports a list of layers and layer sets from the active document.
How can this script check for a specific layer either as part of a layer set or stand alone layer in the active document?
Copy link to clipboard
Copied
How are the Layers you are looking for defined?
Anyway, Action Manager code would likely considerably faster.
Copy link to clipboard
Copied
They are defined with unique names in upper case.
How would the action manager work with the script?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Thanks, this is a great script. I tested the script with PSCC 2014 on two separate files and noticed that it is not able to recognize the top most layer in the layer pallet.
If I move the top layer down in the layer pallet then the scripts finds the layer. Otherwise, the script finds any layer below the top layer even in a layer set.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi
I am using your layer extraction. It works great. I can print out all text elements. I am trying to remove line breaks within the text.
Here is what I have, but it does not remove them. Am I calling the layers correctly?
for (n=0; n<theLayers.length; n++){
if (theLayers
theLayers
}
}
Thanks
Beate
Copy link to clipboard
Copied
Would it not be
/\r/
instead of
/-\r/