Copy link to clipboard
Copied
I am trying to make a script that will:
Loop through all layers of a document.
Add the unlocked layers to array
Loop through this array and paste in place on each layer.
I am not sure how to loop through all layers.
Here is my script (I have used ??? to represent where the loop through all layers part would go):
// JavaScript Document
//Set up an array to hold unlocked layers
var layercollection = [];
//Loop through Layers and unlocked layers to the layercollection array
for (i = 0; ? ? ? i++) {
app.activeDocument.activeLayer = app.activeDocument.layers.itemByName( ? ? ? );
if (app.documents[0].layers. ().locked) == false;
layercollection.push(layers.itemByName())
}
// Loop through layercollection array and paste in place
for (i = 0; i < layercollection.length; i++) {
app.activeDocument.activeLayer = app.activeDocument.layers.itemByName(tl);
app.pasteInPlace(); //Paste
}
Can someone help me complete my script.
Thanks!
1 Correct answer
Did you all test your solutions in a Real World Scenario?
Theoretically it should work this way (if we neglect that a layer could be invisible, too), but even without the problem of invisible layers app.pasteInPlace() prefers to paste into the original layer, if the option "Paste Remembers Layers" is checked.
So I would recommend to do the following:
...var oldPasteRem = app.clipboardPreferences.pasteRemembersLayers;
//uncheck "Paste Remembers Layers"app.clipboardPreferences.pasteRemembersLayers = fal
Copy link to clipboard
Copied
One loop is enough:
for (i = 0; i < app.activeDocument.layers.length; i++) {
app.activeDocument.activeLayer = app.activeDocument.layers);
app.pasteInPlace();
}
Peter
Copy link to clipboard
Copied
Hi
Peter is right. You have add your condition in this code:
for (i = 0; i < app.activeDocument.layers.length; i++) {
if(app.activeDocument.layers.locked == false) {
app.activeDocument.activeLayer = app.activeDocument.layers;
app.pasteInPlace();
}
}
Suresh
Copy link to clipboard
Copied
Did you all test your solutions in a Real World Scenario?
Theoretically it should work this way (if we neglect that a layer could be invisible, too), but even without the problem of invisible layers app.pasteInPlace() prefers to paste into the original layer, if the option "Paste Remembers Layers" is checked.
So I would recommend to do the following:
var oldPasteRem = app.clipboardPreferences.pasteRemembersLayers;
//uncheck "Paste Remembers Layers"app.clipboardPreferences.pasteRemembersLayers = false;
for (i = 0; i < app.activeDocument.layers.length; i++) {
//Maybe the user does not want objects pasted to invisible layers:
if(app.activeDocument.layers.visible == true && app.activeDocument.layers.locked == false) {
app.activeDocument.activeLayer = app.activeDocument.layers;
app.pasteInPlace();
};
};
//Restore the old condition:
app.clipboardPreferences.pasteRemembersLayers = oldPasteRem;
Uwe
Copy link to clipboard
Copied
Absolutely check for layers being locked and the state of the clipboard preferences. I was just concerned with the loops.
Peter
Copy link to clipboard
Copied
Thanks for all the useful and helpful answers. The script Laubender posted worked perfectly, and I have learnt a lot about scripting from it.

