• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Engaged ,
Nov 14, 2022 Nov 14, 2022

Copy link to clipboard

Copied

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!!

 

Capture.JPG

 

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

 

 

TOPICS
Scripting

Views

350

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 14, 2022 Nov 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--)

  

Votes

Translate

Translate
Adobe
Community Expert ,
Nov 14, 2022 Nov 14, 2022

Copy link to clipboard

Copied

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

  

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 14, 2022 Nov 14, 2022

Copy link to clipboard

Copied

LATEST

That did the trick!   Thank you!

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 14, 2022 Nov 14, 2022

Copy link to clipboard

Copied

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') {

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines