Skip to main content
dublove
Legend
June 3, 2026
Answered

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

  • June 3, 2026
  • 2 replies
  • 75 views

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();
//}
}

 

    Correct answer sttk3
    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) ;

     

    2 replies

    pixxxelschubser
    Community Expert
    Community Expert
    June 3, 2026

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

    Try this one-liner:

    app.executeMenuCommand("clearguide");

     

    dublove
    dubloveAuthor
    Legend
    June 3, 2026

    @sttk3  ​@pixxxelschubser 

    How to write the code to unlock the guide lines

    pixxxelschubser
    Community Expert
    Community Expert
    June 3, 2026

    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.)
    😉

    dublove
    dubloveAuthor
    Legend
    June 3, 2026

    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.
     

    sttk3Correct answer
    Legend
    June 3, 2026
    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) ;

     

    dublove
    dubloveAuthor
    Legend
    June 3, 2026

    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’;
    }