I first want to match a top level layer by name then find all group items, but only group items that are under the the "my layer" name and leave the rest of the groups aline. I need to first iterate through the top layers, then through the groupItems, however I cannot seem to access group items as children of layers. here is how I thought it would work.
function findGroups(layerName) {
for (var i = 0; i < numberOfLayers; i++) {
var currentLayer = app.activeDocument.layers[i].name;
if (currentLayer == layerName) {
var numberOfSublayers = app.activeDocument.layers[i].groupItems.length;
for (var i = 0; i < numberOfSublayers; i++) {
// do something
}
}
}
}
findGroups("my layer");
function findGroups(layerName) {
var numberOfLayers = app.activeDocument.layers.length; // numberOfLayers was not defined
for (var i = 0; i < numberOfLayers; i++) {
var currentLayer = app.activeDocument.layers[i].name;
if (currentLayer == layerName) {
var numberOfSublayers = app.activeDocument.layers[i].layers.length; // Number of sublayers
for (var x = 0; x < numberOfSublayers; x++) { // Dont use the i variable twice in the same function. It could work but not best practice.
alert(app.activeDocument.layers[i].layers[x]) // Alert the sublayers
}
}
}
}
findGroups("My Layer"); // Case Sensitive
Oh where have they put the advanced editor? No code thing… Oh well never mind… This should suffice… getByName() should go in atry catch but you get the idea…
#target illustrator
var doc = app.activeDocument;
var grps = doc.layers.getByName( 'my layer' ).groupItems;
for ( var i = 0; i < grps.length; i++ ) {
doc.selection = null;
grps[i].selected = true;
app.redraw();
alert( 'group '+ ( i+1 ) );
};
See pic for difference between types…
no, you're totally right, I'm a dummy and had some extra code in my prototype that was selecting other groups. It works perfect and how you would expect. You're right, since other groups would be out of the parent group. Curious about the app.redraw bit and what it is doing. I have had to use this to fix bugs, is that the case here?
app.redraw(); is just a script method that should cause the GUI window to update by default this does NOT happen for performace… So using it slows things down… I have had some occasions where it has not worked but a dialog will force that… It shouldn't fix anything but I wanted the highlighted selecion to be visible…
North America
Europe, Middle East and Africa
Asia Pacific