Skip to content

Instantly share code, notes, and snippets.

@iconifyit
Last active December 13, 2019 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iconifyit/1ce3325258655ab823536b6c1a8cbac1 to your computer and use it in GitHub Desktop.
Save iconifyit/1ce3325258655ab823536b6c1a8cbac1 to your computer and use it in GitHub Desktop.
JSX function for Adobe Illustrator to group layers based on a matching substring in the layer names. See code comment for more details.
/**
* Group layers in Adobe Illustrator into sub-layers based on a substring
* of the layer names. For instance, given layers named with the pattern:
* 'Icon-set-01-some-keywords-here'
* Call:
* groupLayers('Icon-set-01');
*
* The result will be to create a new parent layer named 'Icon-Set-01' and
* to group any layer whose name starts with 'Icon-Set-01' under that layer.
* @param nameStem
* @returns null
*/
function groupLayers(nameStem) {
var doc = app.activeDocument;
var parent, layer;
try {
parent = doc.layers.getByName(nameStem);
}
catch(e) {
parent = doc.layers.add();
parent.name = nameStem;
}
var matches = [];
try {
for (var i = 0; i < doc.layers.length; i++) {
var layer = doc.layers[i];
if (layer.name.indexOf(nameStem) !== -1) {
if (layer.name.length > nameStem.length) {
matches.push(layer);
}
}
}
for (var i = 0; i < matches.length; i++) {
var layer = matches[i];
layer.move(parent, ElementPlacement.PLACEATEND);
}
}
catch(e) {alert(e)}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment