Skip to main content
Participating Frequently
October 10, 2022
Answered

Script for "select all layers" and "collect in new layer"

  • October 10, 2022
  • 1 reply
  • 1869 views

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

This topic has been closed for replies.
Correct answer femkeblanco

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


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;
}

1 reply

femkeblanco
Legend
October 10, 2022

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. 

Participating Frequently
October 11, 2022

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.

jduncan
Community Expert
Community Expert
October 11, 2022

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]);
}