Adober Illustrator Script
I have a script that's meant to collect objects of similar fill color and place them in seperate layers, then name the layer by the name of the spot color of the objects, then order the layers alphanumerically. It runs sometimes, while other times Illustrator stops responding halfway through creating layers. Can anyone suggest what may be the problem?
I run this once I have used Edit Colors to make every color in my file a Spot Color and I get no error message when it doesn't work. With some files it works, but with others, it doesn't.
Please let me know any advice you may have!
var acl = app.activeDocument.activeLayer;
while (acl.pathItems.length != 0) {
try {
var layer = app.activeDocument.layers.add();
layer.name = acl.pathItems[0].fillColor.spot.name;
activeDocument.selection = acl.pathItems[0];
app.executeMenuCommand("Find Fill & Stroke menu item");
for (var j = 0; j < activeDocument.selection.length; j++) {
activeDocument.selection[j].move(layer, ElementPlacement.PLACEATBEGINNING);
}
// app.executeMenuCommand("compoundPath");
} catch (e) {
continue;
}
};
if (activeDocument.selection.length) {
app.activeDocument.activeLayer.name = activeDocument.selection[0].fillColor.spot.name
}
var doc = app.activeDocument;
var layers = doc.layers;
var layersArray = [];
// Push all layers into an array
for (var i = 0; i < layers.length; i++) {
layersArray.push(layers[i]);
}
// Sort the array by layer name
layersArray.sort(function(a, b) {
var colorA = a.name.split(" ")[0].split("=")[1];
var colorB = b.name.split(" ")[0].split("=")[1];
return colorB - colorA;
});
// Add the sorted layers back to the document
for (var i = 0; i < layersArray.length; i++) {
layersArray[i].zOrder(ZOrderMethod.BRINGTOFRONT);
}
