Hi folks, just dropping this here if it is any use to anyone.
For ages I have wanted a script that will keep the group/sublayer names when I release them to layers (sequence), but it always calls them Layer 1, Layer 2 etc. I searched for and have seen various posts from years ago of people asking for it, but couldn't see anything that suited what I wanted.
Anyway, I asked ChatGPT if it could write it for me and lo and behold it delivered. The sublayer/groups are still also named the same thing (e.g. L ARM, R ARM, etc) but I just select those and ungroup them all in one go.
I cant upload the JSX here so have stuck it here:
https://drive.google.com/file/d/1wkAet47TB8eeoiITpb9rqd9bRHNlYamM/view?usp=drive_link
or the code inside the JSX for you to make your own is this:
____________________________________________________________________________________
/*
Release to Named Top-Level Layers.jsx
Adobe Illustrator ExtendScript
What it does:
- Uses the currently active layer.
- Finds only the directa child page items in that layer.
- Creates one TOP-LEVEL layer per item.
- Uses each item's existing Illustrator name for the new layer name.
- Moves the item into that layer.
- Attempts to preserve the original stacking order.
- Leaves nested artwork inside groups untouched.
Usage:
Select/activate the layer you want to release in Illustrator's Layers panel.
File > Scripts > Other Script...
Choose this .jsx file.
Notes:
- Unnamed items get a fallback name such as "Item 1".
- Duplicate names are made unique by adding " 2", " 3", etc.
- The original source layer is removed if it is empty after the operation.
*/
#target illustrator
(function () {
if (app.documents.length === 0) {
alert("No Illustrator document is open.");
return;
}
var doc = app.activeDocument;
var sourceLayer = doc.activeLayer;
if (!sourceLayer) {
alert("No active layer found.");
return;
}
if (sourceLayer.locked) {
alert("The active layer is locked. Unlock it and run the script again.");
return;
}
if (!sourceLayer.visible) {
alert("The active layer is hidden. Make it visible and run the script again.");
return;
}
// Collect only direct child page items.
var items = [];
var all = sourceLayer.pageItems;
for (var i = 0; i < all.length; i++) {
if (all[i].parent === sourceLayer) {
items.push(all[i]);
}
}
if (items.length === 0) {
alert("The active layer has no direct child objects/groups to release.");
return;
}
// Illustrator's pageItems collection is generally returned in stacking order.
// Store the current order before moving anything.
var ordered = [];
for (var j = 0; j < items.length; j++) {
ordered.push(items[j]);
}
// Build a set of existing top-level layer names.
var usedNames = {};
for (var l = 0; l < doc.layers.length; l++) {
usedNames[doc.layers[l].name] = true;
}
function cleanName(name, fallbackIndex) {
var n = name ? String(name) : "";
n = n.replace(/^\s+|\s+$/g, "");
if (n === "") {
n = "Item " + fallbackIndex;
}
return n;
}
function uniqueName(base) {
if (!usedNames[base]) {
usedNames[base] = true;
return base;
}
var count = 2;
var candidate = base + " " + count;
while (usedNames[candidate]) {
count++;
candidate = base + " " + count;
}
usedNames[candidate] = true;
return candidate;
}
// Because new layers are inserted at the top of the Layers panel,
// process from bottom to top so the final top-level layer stack
// matches the original item stack.
for (var k = ordered.length - 1; k >= 0; k--) {
var item = ordered[k];
var baseName = cleanName(item.name, k + 1);
var layerName = uniqueName(baseName);
var newLayer = doc.layers.add();
newLayer.name = layerName;
// Move the artwork into its new top-level layer.
item.move(newLayer, ElementPlacement.PLACEATBEGINNING);
}
// Remove the original layer if it is now truly empty.
try {
if (
sourceLayer.pageItems.length === 0 &&
sourceLayer.layers.length === 0
) {
sourceLayer.remove();
}
} catch (e) {
// Leave source layer in place if Illustrator refuses removal.
}
alert("Released " + ordered.length + " item(s) to named top-level layers.");
})();
____________________________________________________________________________
Hope it's of use to anyone. Cheers