Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Community Beginner ,
Oct 10, 2022 Oct 10, 2022

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

TOPICS
Scripting
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Oct 12, 2022 Oct 12, 2022

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
...
Translate
Adobe
Guide ,
Oct 10, 2022 Oct 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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 11, 2022 Oct 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 11, 2022 Oct 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]);
}

 

dad x 2. designer. maker. fun haver. ‍
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 12, 2022 Oct 12, 2022

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 12, 2022 Oct 12, 2022

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

 

dad x 2. designer. maker. fun haver. ‍
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 12, 2022 Oct 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. 😞

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 12, 2022 Oct 12, 2022

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;
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 12, 2022 Oct 12, 2022

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

dad x 2. designer. maker. fun haver. ‍
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 12, 2022 Oct 12, 2022

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

dad x 2. designer. maker. fun haver. ‍
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 13, 2022 Oct 13, 2022

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

peter_fischer_0-1665647896068.png

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 13, 2022 Oct 13, 2022
 
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 13, 2022 Oct 13, 2022
LATEST

Strangely, I can't upload my file in ai format.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines