Copy link to clipboard
Copied
Hi everbody,
can someone help me with a script idea that selects all layers in an Illustrator file (regardless of name or number of layers) and collects these layers in a new file?
So that afterwards I have only one layer in the file and the original layers are kept as sub-layers.
Unfortunately there is no menu command to select all layers, so that I can create an action. As far as I know, this can only be done by clicking. Everything I have found in script libraries so far works with single individual layer names.
Thanks a lot for your help!
Many greetings,
Peter
There is a problem in the name of the variable "visible", for some reason. Change it to anyting else. E.g.
var doc = app.activeDocument;
doc.layers.add().name = "COLLECTED";
var layerToMove, isVisible, locked;
for (var i = doc.layers.length - 1; i > 0; i--) {
// get current layer and it's locked/hidden status
layerToMove = doc.layers[i];
isVisible = layerToMove.visible;
locked = layerToMove.locked;
// make visible and unhide layer for moving
layerToMove.visible = true;
layerToMove
...
Copy link to clipboard
Copied
As far as I know, layers cannot be duplicated or moved between documents/files. One way forward would be to move all layers to a new layer (which can be done with a couple of lines of script, see here) and then "save as" a new file. This means that the original file will be closed.
Copy link to clipboard
Copied
Your code snippet is already pretty good.
I didn't want to move the layers to a new document. They should be collected in a new layer in the existing document.
Unfortunately, your code doesn't work for locked or hidden layers. The command "collect in new layer" does not take the status into account and work with all layers.
Copy link to clipboard
Copied
You can just add checks to @femkeblanco's script for hidden and locked layers. If the layer is hidden, locked, or both, the two if statements will change the boolean value allowing the layer to be moved. You actually don't even need the if statement, you could just set those values for every layer but I left them here for the explanation.
var doc = app.activeDocument;
doc.layers.add().name = "COLLECTED";
var layerToMove;
for (var i = doc.layers.length - 1; i > 0; i--) {
layerToMove = doc.layers[i];
if (!layerToMove.visible) layerToMove.visible = true;
if (layerToMove.locked) layerToMove.locked = false;
layerToMove.moveToBeginning(doc.layers[0]);
}
Copy link to clipboard
Copied
That already takes me a big step further.
Unfortunately, this causes the layers to lose their original status (visible and/or locked), so that layers that were previously hidden suddenly become visible.
Is it possible to keep the status so that the appearance of the graphic does not change?
Copy link to clipboard
Copied
Pretty simple. Just capture the hidden/locked status for each layer before moving it, then reapply those settings after moving.
var doc = app.activeDocument;
doc.layers.add().name = "COLLECTED";
var layerToMove, visible, locked;
for (var i = doc.layers.length - 1; i > 0; i--) {
// get current layer and it's locked/hidden status
layerToMove = doc.layers[i];
visible = layerToMove.visible;
locked = layerToMove.locked;
// make visible and unhide layer for moving
layerToMove.visible = true;
layerToMove.locked = false;
layerToMove.moveToBeginning(doc.layers[0]);
// reapply the locked/hidden status after moving
if (!visible) layerToMove.visible = false;
if (locked) layerToMove.locked = true;
}
Copy link to clipboard
Copied
Hey @jduncan ,
there seems to be a small error.
The locked status seems to be taken over correctly, but the visibility status is not. All levels are shown after a script run.
Unfortunately I can't detect the error. 😞
Copy link to clipboard
Copied
There is a problem in the name of the variable "visible", for some reason. Change it to anyting else. E.g.
var doc = app.activeDocument;
doc.layers.add().name = "COLLECTED";
var layerToMove, isVisible, locked;
for (var i = doc.layers.length - 1; i > 0; i--) {
// get current layer and it's locked/hidden status
layerToMove = doc.layers[i];
isVisible = layerToMove.visible;
locked = layerToMove.locked;
// make visible and unhide layer for moving
layerToMove.visible = true;
layerToMove.locked = false;
layerToMove.moveToBeginning(doc.layers[0]);
// reapply the locked/hidden status after moving
if (!isVisible) layerToMove.visible = false;
if (locked) layerToMove.locked = true;
}
Copy link to clipboard
Copied
@femkeblanco, yep, seems 'visible' is a global variable. And since I run my scripts from a panel, I think they are somewhat "sandboxed" so they don't have collisions with the global vars.
Copy link to clipboard
Copied
@peter_fischer, that's weird... Seems to work fine for me, even with nested objects hidden and locked. See the attached screen recording and let me know if that is the correct behavior you were expecting. Send me your working Ai file too if you can and I'll take a look to see where things may be going wrong.
Copy link to clipboard
Copied
@femkeblanco That's great. After renaming the variable "visible" to "isVisible" it works!
@jduncan Thanks you too. femkeblancos note make the script work. After renaming the variable, the layers stay correct. I'll attach my test-file and a screenshot of my layer palette before and after scripting.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Strangely, I can't upload my file in ai format.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now