Last active
December 13, 2019 16:42
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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