Skip to main content
dublove
Legend
Jun 3, 2026 4:57 AM UTC
Answered

This Illustrator script is weird—why doesn’t the loop reach `i == 2`?

I tried deleting the guides, but it only executed up to the second loop.
Is there something wrong with Illustrator’s scripting architecture? Some scripts that worked two or three years ago seem to have stopped working.


It’s such a mess—no wonder so few people are working on it.

var d=app.activeDocument;
var pl = d.pathItems.length;
//var isg = [];

for (var i = 0; i < d.pathItems.length; i++) {
var p = d.pathItems[i];
if (p.hasOwnProperty('guides')) {
p.remove();
}
//if (p.guides == true) {
// //isg.push(p);
// p.remove();
//}
}

 

    sttk3Correct answer
    Legend
    Jun 3, 2026 9:22 AM UTC
    var d = app.activeDocument ;
    var pl = d.pathItems.length ;

    // Deleting an item will change the index, so process in descending order
    for(var i = pl - 1 ; i >= 0 ; i--) {
    var p = d.pathItems[i] ;

    // Every PathItem has a guides property, which is a boolean. Check it as a boolean
    if(p.guides) {
    p.remove() ;
    }
    }

    // Layer and layer group are the same in Illustrator. Add them using `layers.add`
    var layerGroup = d.layers.add() ;
    layerGroup.name = 'Layer Group' ;
    d.layers[1].move(layerGroup, ElementPlacement.PLACEATEND) ;

     

    12 replies

    pixxxelschubser
    Community Expert
    Community Expert
    Jun 3, 2026 3:29 PM UTC

    I probably wouldn't take the hard and rocky path.

    Try this one-liner:

    app.executeMenuCommand("clearguide");

     

    Translate
    dublove
    dubloveAuthor
    Legend
    Jun 3, 2026 3:58 PM UTC

    ​@sttk3  ​@pixxxelschubser 

    How to write the code to unlock the guide lines

    Translate
    pixxxelschubser
    Community Expert
    Community Expert
    Jun 3, 2026 4:43 PM UTC

    Try
     

    // turns on the opposite state
    app.executeMenuCommand('lockguide');

    (However, when deleting guide lines using `executeMenuCommand`, it shouldn't really matter whether they are locked or unlocked.)
    😉

    Translate
    dublove
    dubloveAuthor
    Legend
    Jun 3, 2026 5:06 AM UTC

    I think I made a mistake. The loop is using a variable.
    var pl = d.pathItems.length;
    The value of pl has changed.

    But the first loop is supposed to run three times.

     

    By the way, how do I create a new layer group?
    A lot of the information on the forum doesn't seem to work anymore.
     

    Translate
    sttk3Correct answer
    Legend
    Jun 3, 2026 9:22 AM UTC
    var d = app.activeDocument ;
    var pl = d.pathItems.length ;

    // Deleting an item will change the index, so process in descending order
    for(var i = pl - 1 ; i >= 0 ; i--) {
    var p = d.pathItems[i] ;

    // Every PathItem has a guides property, which is a boolean. Check it as a boolean
    if(p.guides) {
    p.remove() ;
    }
    }

    // Layer and layer group are the same in Illustrator. Add them using `layers.add`
    var layerGroup = d.layers.add() ;
    layerGroup.name = 'Layer Group' ;
    d.layers[1].move(layerGroup, ElementPlacement.PLACEATEND) ;

     

    Translate
    dublove
    dubloveAuthor
    Legend
    Jun 3, 2026 11:05 AM UTC

    Hi ​@sttk3 

    How to check if a layer group exists.
    If it does not exist, an error will be reported:
    No such element

    var GGroup = d.layers.getByName(“GGroup”);
    if (!GGroup == false) {
        var layerGroup = d.layers.add();
        layerGroup.name = ‘GGroup’;
    }

     

    Translate