Add artboard with JavaScript in Illustrator CS5.1

I’m trying to pull together set of scripts to help with android/ios graphic resources development. I can export artwork to PNG but results due to scaling are not satisfactory. This means that the scaling and adjusting graphic vectors needs to be done by the graphic designer. To help him in this I want to create a script that will produce set of artboards with given name and base dimensions.

I’ve searched the documentation and found that one can add new artboard with the add method of the Artboards instance. Docs says that it receives the rect argument – and nothing more as to what this rect is.

I’ve played with it and got frustrated by meaningless error messages that were not helpful.

Error 1242: Illegal argument – argument 1 – Rectangle value expected

or

Rectangle does not have a constructor

or

Error 1200: an Illustrator error coccurred: 1346458189(‘PARAM’)

After searching here and there I’ve found that you should pass an array of 4 elements with following values (left, top, right, bottom).
PARAM errors means that some of the elements in the given list are invalid e.g. [-300,-300,200,-300]

to calculate them easily I’ve put following function:

var newRect = function(x, y, width, height) {
    var l = 0;
    var t = 1;
    var r = 2;
    var b = 3;

    var rect = [];

    rect[l] = x;
    rect[t] = -y;
    rect[r] = width + x;
    rect[b] = -(height - rect[t]);

    return rect;
};

now it is much easier to add artboards at given size and position

var artboards = app.activeDocument.artboards;
var artboard = artboards.add(newRect(0,-50,50,50));
    artboard.name = "test 1";
    artboard = artboards.add(newRect(100,0,50,50));
    artboard.name = "test 2";
    artboard = artboards.add(newRect(0,100,50,50));
    artboard.name = "test 3";
    artboard = artboards.add(newRect(-50,0,50,50));
    artboard.name = "test 4";
This entry was posted in Illustrator-Scripting, JavaScript and tagged , , , . Bookmark the permalink.