Skip to content

Instantly share code, notes, and snippets.

@creold
Created November 8, 2024 13:54
Show Gist options
  • Save creold/6ae88e09836400e43a472db4c0973576 to your computer and use it in GitHub Desktop.
Save creold/6ae88e09836400e43a472db4c0973576 to your computer and use it in GitHub Desktop.
Merge layers with the same name into one layer. Adobe Illustrator script
/*
Merge layers with the same name into one layer
Discussion: https://community.adobe.com/t5/illustrator-discussions/script-to-merge-layers-that-have-the-same-name-into-sublayers/td-p/14968548
Author: Sergey Osokin, email: [email protected]
Check my other scripts: https://github.com/creold
Donate (optional):
If you find this script helpful, you can buy me a coffee
- via Buymeacoffee: https://www.buymeacoffee.com/aiscripts
- via Donatty https://donatty.com/sergosokin
- via DonatePay https://new.donatepay.ru/en/@osokin
- via YooMoney https://yoomoney.ru/to/410011149615582
*/
//@target illustrator
preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file
function main() {
var isSublayer = false;
if (!/illustrator/i.test(app.name)) {
alert('Wrong application\nRun script from Adobe Illustrator', 'Script error');
return;
}
if (!documents.length) {
alert('No documents\nOpen a document and try again', 'Script error');
return;
}
isSublayer = confirm('How to merge layers?\nAs sublayers of a new layer (Yes) or their objects (No)');
// alert(isSublayer);
// return;
var doc = app.activeDocument;
var layerGroups = collectSameLayers(doc.layers);
for (var i = layerGroups.length - 1; i >= 0; i--) {
mergeLayersToNew(doc, layerGroups[i], isSublayer);
}
}
// Group layers by matching names into an array of objects
function collectSameLayers(layers) {
var layerGroups = [];
for (var i = 0; i < layers.length; i++) {
var currLayer = layers[i];
var currName = currLayer.name;
var isFound = false;
for (var j = 0; j < layerGroups.length; j++) {
if (layerGroups[j].name === currName) {
layerGroups[j].layers.push(currLayer);
isFound = true;
break;
}
}
if (!isFound) {
layerGroups.push({
name: currName,
layers: [currLayer]
});
}
}
return layerGroups;
}
// Merges multiple layers into a single layer
function mergeLayersToNew(doc, group, isSublayer) {
// Exit if there are fewer than two layers to merge
if (group.layers.length < 2) return;
var tgtLayer = isSublayer ? doc.layers.add() : group.layers[0];
tgtLayer.name = group.name;
tgtLayer.color = group.layers[0].color;
var startIdx = isSublayer ? 0 : 1;
for (var i = startIdx; i < group.layers.length; i++) {
var currLayer = group.layers[i];
var isVisible = currLayer.visible;
var isLocked = currLayer.locked;
currLayer.visible = true;
currLayer.locked = false;
// Move the entire layer or all pageItems
if (isSublayer) {
currLayer.move(tgtLayer, ElementPlacement.PLACEATEND);
currLayer.visible = isVisible;
currLayer.locked = isLocked;
} else {
movePageItems(currLayer, tgtLayer);
currLayer.remove();
}
}
}
// Moves all items from a source layer to a target layer
function movePageItems(srcLayer, tgtLayer) {
var pItems = [];
for (var i = 0; i < srcLayer.pageItems.length; i++) {
pItems.push(srcLayer.pageItems[i]);
}
for (var j = 0; j < pItems.length; j++) {
pItems[j].move(tgtLayer, ElementPlacement.PLACEATEND);
}
}
try {
main();
} catch (err) {}
@creold
Copy link
Author

creold commented Nov 8, 2024

More about script in Telegram channel.

MergeLayersByName

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment