Skip to main content
Inspiring
November 14, 2022
Answered

Script has an Error Pop up and I don't know why

  • November 14, 2022
  • 2 replies
  • 561 views

Good Morning,

 

I have a script I wrote that uses Radio Buttons to select which function to use.  It works as intended however when the "Extra Artboards" button is selected there is an Error that Pops up and I don't know why.

 

Thank you for your time!!

 

 

var choice
var w = new Window("dialog");
var g = w.add("group");
var a = g.add("radiobutton", undefined, "Main Artboard");
var b = g.add("radiobutton", undefined, "Extra Artboards");
var button = w.add("button", undefined, "OK");
var radiobuttons = [a, b];
for (var i = 0; i < radiobuttons.length; i++) {
    (function (i) {
        radiobuttons[i].onClick = function () {
            choice = radiobuttons[i].text;
        };
    })(i);
}
w.show();

var removeArtboards;
if (choice == "Main Artboard") {
    // Main
    function removeArtboards ( arr ) {
        var i = arr.length; while ( i-- ) activeDocument.artboards[ arr[i] ].remove();  
    } 
    removeArtboards( [0] );

} else if (choice == "Extra Artboards") {
    // Extra
    function removeArtboards() {
        var doc = app.activeDocument;
        var artboardsLength = doc.artboards.length
        for (var i = artboardsLength - 1; i >= 0; i--) {
            if (app.activeDocument.artboards[i].name != 'Artboard 1') {
                activeDocument.artboards[i].remove();
            }
        }
    }
    removeArtboards();
}

 

 

This topic has been closed for replies.
Correct answer CarlosCanto

it errors when it tries to remove the last artboard. 

 

change the below line to ">" instead of ">="

for (var i = artboardsLength - 1; i >= 0; i--)

  

2 replies

CarlosCanto
Community Expert
Community Expert
November 14, 2022

you probably don't need this line after the fix above, since the last artboard name could be anything ie "Artboard 2"

if (app.activeDocument.artboards[i].name != 'Artboard 1') {
CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
November 14, 2022

it errors when it tries to remove the last artboard. 

 

change the below line to ">" instead of ">="

for (var i = artboardsLength - 1; i >= 0; i--)

  

Inspiring
November 14, 2022

That did the trick!   Thank you!