Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Rectangle object required by add method of the artboards object in Illustrator CS5.1+?

I'm trying to add new artboard with the help of java script. I wasn't able to find solution nowhere. The scripting guidelines from adobe are just poor (to not use more strong words).

What ever I'm trying it returns the error:

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

when I use value of artboard.artboardRect from other artboard then it creates artboard in the same place but I can't modify it (resize) which makes this option useless.

artboards.add(artboards[0].artboardRect);//works
artboards.add([0,0,200,50]);//Error 1200: an Illustrator error coccurred: 1346458189('PARAM')
var rect = artboards[0].artboardRect;
rect[0] = 0;
rect[1] = 0;
rect[2] = 200;
rect[3] = 50;
artboards.add(rect);//Error 1242: Illegal argument - argument 1 - Rectangle value expected
like image 282
Lukasz 'Severiaan' Grela Avatar asked Oct 31 '25 08:10

Lukasz 'Severiaan' Grela


2 Answers

After searching extensively I've found this workaround:

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

artboard = artboards.add(artboards[0].artboardRect);
artboard.name = "new name";
artboard.artboardRect = newRect(0, 0, 200, 50);
like image 171
Lukasz 'Severiaan' Grela Avatar answered Nov 02 '25 22:11

Lukasz 'Severiaan' Grela


In several places in Illustrator Scripting PDFs rect or someOtherPropRect is defined as "array of 4 numbers". For example,

var document = app.documents.add();
$.writeln(document.artboards[0].artboardRect); // 0,792,612,0

returned values corresponds to topLeftX, topLeftY, bottomRightX, bottomRightY.

To make sense of these values, we need to take a look at the coordiate system of Illustrator. Illustrator's UI uses a modified coordinate system, not the actual cartesian one. In other words, top left of the screen is the origin, not bottom left. But when scripting, actual cartesian coordinate system is used.

Because of this difference in coordinate system, entered values that are on the Y axis should be nagative. So, if I want to reposition and resize the artboard, say, move it to (200,50) and resize it to (400,300), what I need to do is:

var document = app.activeDocument;
var artboard = document.artboards[0];

var x = 200;
var y = 50;
var w = 400;
var h = 300;

artboard.artboardRect = [x, -y, (x + w), -(y + h)];

$.writeln(document.artboards[0].artboardRect); // 200, -50, 600, -350

This solution can be wrapped in a function:

function rect(x, y, w, h) {
    return [x, -y, (x + w), -(y + h)];
}

artboard.artboardRect = rect(200, 50, 400, 300);

Both Y and H should be negative, otherwise you'll get an error saying

Error 1200: an Illustrator error occurred: 1346458189 ('PARM')

or incorrect reposition/resize.

like image 42
akinuri Avatar answered Nov 02 '25 22:11

akinuri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!