Skip to main content
Participating Frequently
October 10, 2022
解決済み

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

  • October 10, 2022
  • 返信数 1.
  • 1869 ビュー

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

このトピックへの返信は締め切られました。
解決に役立った回答 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

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. 

peter_fischer作成者
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.

femkeblanco
femkeblanco解決!
Legend
October 12, 2022

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