Skip to main content
matts68240464
Participant
June 29, 2018
Answered

Can I get rid of this error?

  • June 29, 2018
  • 1 reply
  • 819 views

I have a simple script. I am removing all artboards in the document from back to front. I get an error when running the script - but after the error, my script runs as planned ( all artboards are removed, except for artboard 1). Any way to remove this error, or write the script a little bit cleaner? Thanks!

var myDoc = app.activeDocument;

for (i=myDoc.artboards.length +1; i > myDoc.artboards.length; i--) {

    activeDocument.artboards[i-2].remove();

    }

This topic has been closed for replies.
Correct answer Inventsable

I ran the script and it returns "an Illustrator error occured: 1128549443 ('CDLC')". Admittedly I'm not sure where to reference what that means, but I think it probably has to do with either your index number in the loop (if i = 1, then your use of i - 2 returns a value of negative 1) or use of activeDocument.artboards (instead of app.activeDocument.artboards or myDoc.artboards). I tested this with no errors returning:

var myDoc = app.documents[0];

var myABs = myDoc.artboards.length - 1;

for (var i = myABs; i > 0; i--) {

  myDoc.artboards.setActiveArtboardIndex(i) 

  myDoc.artboards.remove();

}

You could also use try and catch statements like so:

// attempt to execute code in a try{ ... } block

try {

  for (var i = myABs; i > 0; i--) {

    myDoc.artboards.setActiveArtboardIndex(i) 

    myDoc.artboards.remove();

  }

} catch(error){

  // catch statement is only in effect if an error occurs:

  alert(error);

  // if you wanted to bypass an error purposefully, you could use throw or something like:

  // } catch(e){return}

};

1 reply

Inventsable
InventsableCorrect answer
Legend
June 30, 2018

I ran the script and it returns "an Illustrator error occured: 1128549443 ('CDLC')". Admittedly I'm not sure where to reference what that means, but I think it probably has to do with either your index number in the loop (if i = 1, then your use of i - 2 returns a value of negative 1) or use of activeDocument.artboards (instead of app.activeDocument.artboards or myDoc.artboards). I tested this with no errors returning:

var myDoc = app.documents[0];

var myABs = myDoc.artboards.length - 1;

for (var i = myABs; i > 0; i--) {

  myDoc.artboards.setActiveArtboardIndex(i) 

  myDoc.artboards.remove();

}

You could also use try and catch statements like so:

// attempt to execute code in a try{ ... } block

try {

  for (var i = myABs; i > 0; i--) {

    myDoc.artboards.setActiveArtboardIndex(i) 

    myDoc.artboards.remove();

  }

} catch(error){

  // catch statement is only in effect if an error occurs:

  alert(error);

  // if you wanted to bypass an error purposefully, you could use throw or something like:

  // } catch(e){return}

};

matts68240464
Participant
July 2, 2018

Thanks Tom.

I believe it was the i-2 in my original code that was creating the error when the loop reached the final artboard. Your script solved the problem.

CarlosCanto
Community Expert
Community Expert
July 3, 2018

the problem is the way you're building your loop, to loop forward you usually go from 0 to the length of your array. Similarly, to loop backwards you should loop from the length of your array to 0.

instead of

for (i=myDoc.artboards.length +1; i > myDoc.artboards.length; i--) {

it should be

for (i=myDoc.artboards.length-1 ; i > 0; i--) {

then remove artboard using index i

activeDocument.artboards.remove();

**********************************************************************

the error is due to trying to remove last artboard. This line [i-2] always evaluates to zero at the end of your loop. Try this line on a document with 1 artboard. You'll get the same error

activeDocument.artboards[0].remove();