Skip to content

Instantly share code, notes, and snippets.

@haschdl
Created August 4, 2020 08:31
Show Gist options
  • Save haschdl/2a0cfb5a4b72d05564f3e69682cdb25f to your computer and use it in GitHub Desktop.
Save haschdl/2a0cfb5a4b72d05564f3e69682cdb25f to your computer and use it in GitHub Desktop.
/***
* This script ungroups all items recursivele, and moves path items to 4 layers, using a round-robin approach.
*
* Context: spliting a SVG created for plotting with 4 different pen colors.
* In my workflow the SVGs are created with code using the Processing environment.
*
*
* Tested on: Illustrator CC 2020 only
* Reminder to self: ESTK is compatible with ECMA 3
*
* Author: Half Scheidl @hscheidl
* Date: 04/08/2020
*
* Support references for future updates:
* https://illustrator-scripting-guide.readthedocs.io/jsobjref/GroupItems/
* https://illustrator-scripting-guide.readthedocs.io/jsobjref/Layers/
**/
var ungroupAll = true;
//If there are grouped elements, ungroup them, recursively, to a limit of 100 nested levels
if(ungroupAll) {
var layer = app.activeDocument.activeLayer;
var items = layer.groupItems;
var limit = 0;
while (items.length > 0 && limit < 100) {
for (d=0;d<items.length;d++) {
var group = layer.groupItems[d];
for (i=group.pageItems.length-1; i>=0; i--)
group.pageItems[i].move(layer, ElementPlacement.PLACEATBEGINNING);
}
items = layer.groupItems;
limit++;
}
}
//Stupid way to make sure we have at least 4 layers (at least; if there are more layers we don't care)
var layers = app.activeDocument.layers.length;
var left = 4 - layers
if(left > 0) {
alert("Layers: " + layers + ". Creating additional " + left + " layers.");
while (left > 0) {
app.activeDocument.layers.add()
left = left - 1;
}
}
var pathItemsCount = documents[0].pathItems.length;
for(var i = 0; i < pathItemsCount; i++) {
var targetLayerIndex = i % 4;
var targetLayer = app.activeDocument.layers[targetLayerIndex]
documents[0].pathItems[i].move(targetLayer, ElementPlacement.PLACEATEND);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment