How to convert all layers into Group
Copy link to clipboard
Copied
Hi all,
I have a file with multiple layers containing multiple items.
i want each layers inside items convert into group but remain in layer how can i do this with scripts. i have used LayerToGroup.jsx script but with this just 1 top layer convert other remain the same. I need to convert every single layer into group
Any help would be very, very appreciated!
Thank you
Explore related tutorials & articles
Copy link to clipboard
Copied
Try over in the Illustrator Scripting Forum. Check on the Overview tab.
Copy link to clipboard
Copied
//if you're using Illustrator CS6+
function groupLayerContents()
{
var docRef = app.activeDocument;
var layers = docRef.layers;
for(var a=0;a<layers.length;a++)
{
docRef.selection = null;
var thisLay = layers;
thisLay.hasSelectedArtwork = true;
app.executeMenuCommand("group");
}
}
groupLayerContents();
//if you're using a legacy version of Illustrator
//CS5 or earlier
function groupLayerContents()
{
var docRef = app.activeDocument;
var layers = docRef.layers;
var tempLay = docRef.layers.add();
tempLay.name = "temp";
for(var a=1;a<layers.length;a++)
{
var thisLay = layers;
var newGroup = tempLay.groupItems.add();
newGroup.name = thisLay.name + " group";
for(var b = thisLay.pageItems.length-1;b >-1; b--)
{
var thisItem = thisLay.pageItems;
thisItem.moveToBeginning(newGroup);
}
newGroup.moveToBeginning(thisLay);
}
tempLay.remove();
}
groupLayerContents();
Copy link to clipboard
Copied
The above didn't work for me, so I hacked this together from a few different references. I'm SURE it needs variables etc to clean up, as I'm only a hack coder, but this works for me:
if (app.documents.length > 0) {
if (app.activeDocument.layers.length > 1) {
for (var i = 0; i < app.activeDocument.layers.length; i++) //loop through all TOP LEVEL layers
{
app.activeDocument.layers[i].hasSelectedArtwork = true; //selects all in active layer
app.executeMenuCommand('group'); //groups all selected
app.activeDocument.selection = null; //deselect
}
} else {
alert("The active document only has only 1 layer");
}
}
Copy link to clipboard
Copied
Here's an interesting catch with this script... if I run it from the File->Scripts menu item... all is well. If I make an Action that runs that same menu item... I get an error dialoge, then another saying the "deselect" line throws the error. Can anyone explain this?
Copy link to clipboard
Copied
Also realized I had extra if statement in there... but still get error when using as Action.
if (app.activeDocument.layers.length > 1) {
for (var i = 0; i < app.activeDocument.layers.length; i++) //loop through all TOP LEVEL layers
{
app.activeDocument.layers[i].hasSelectedArtwork = true; //selects all in active layer
app.executeMenuCommand('group'); //groups all selected
app.activeDocument.selection = null; //deselect
}
} else {
alert("The active document only has only 1 layer");
}

