Search code examples
javascriptadobe-illustratorextendscript

how to add an artboard to a document in Adobe Illustrator?


I wrote a script in JavaScript for Adobe Illustrator CC 2017. In this script I am trying to add artboard to the document in function, but it isn't working.

Here code:

function addArtboard() {
    var doc = app.documents.add(null,1000,1000);
    doc = app.activeDocument;
    var artboards = doc.artboards;
     artboards.add([0 , 0, 1000, 1000]);
}
 
addArtboard();


Solution

  • Your problems seems to be the measurement of your new artboard. Take a look into the scripting guide here.

    The add method takes an artboardRect as argument.
    screenshot from scripting guide

    The code below creates a new doc and adds an artboard next to the first one.

    /* global app, $ */
    function addArtboard() {
      var doc = app.documents.add(); // create a doc with defaults
      var firstArtBoard = doc.artboards[0]; // get the default atboard
        // inspect its properties
      for(var prop in firstArtBoard) {
        // exclude protptypes
        if(firstArtBoard.hasOwnProperty(prop)) {
          $.writeln(prop);
        }
      }
      // there is a rect property
      // take a look at the values
      $.writeln(firstArtBoard.artboardRect);
      // create a artboard with the same size and an
      // offset of 5 points to the right
      var x1 = firstArtBoard.artboardRect[2] + 10;
      var y1 = firstArtBoard.artboardRect[1];
      var x2 = x1 + firstArtBoard.artboardRect[2];
      var y2 = firstArtBoard.artboardRect[3];
      doc.artboards.add([x1, y1, x2, y2]);
    }
    
    addArtboard();