Get bounds of all artboards in Illustrator CS5.1 with JavaScript

When you need to create new artboard you will, most certainly, would like to know where to place it safely. One of the options could be to place it next to the “edges” of the “bounding box” created by all artboards.

To find this “bounding box” we need to know bounding box of each artboard. The bounds of the artboard are returned by artboardRect property.
All we have to do is to go through all of the artboards, take maximum/minimum of each of the properties (left, top, right, bottom) and use it.

Script below will create new artboard matching those extremes.

var artboards = app.activeDocument.artboards;
var extreme = [0,0,0,0];
for (var i = 0; i < artboards.length; i++) {
    //[0 left, 1 top, 2 right, 3 bottom]
    var rect = artboards[i].artboardRect;
    //
    if(rect[0] < extreme[0]){
        extreme[0] = rect[0];
    }
    if(rect[1] > extreme[1]){
        extreme[1] = rect[1];
    }
    if(rect[2] > extreme[2])
    {
        extreme[2] = rect[2];
    }
    if(rect[3] < extreme[3])
    {
        extreme[3] = rect[3];
    }
}
alert(extreme);
var a = artboards.add(extreme);
a.name = "Extreme";
This entry was posted in Illustrator-Scripting, JavaScript and tagged , , . Bookmark the permalink.