How do I create the Rectangle required by the add artboards method in Illustrator CS5.1 +?
I am trying to add a new panel using a java script. I haven't been able to find a solution anywhere. The adobe scripting guidelines are just poor (so as not to use stronger words).
Whatever I try, it returns an error:
Error 1242: Invalid argument - argument 1 - expected rectangle value
when i use a value artboard.artboardRect
from another artboard then it creates the artboard in the same place, but i can't resize it, 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
source to share
After searching thoroughly, I 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);
source to share
In several places, PDF files of Illustrator scripts rect
or someOtherPropRect
defined as "an array of 4 numbers". For example,
var document = app.documents.add();
$.writeln(document.artboards[0].artboardRect); // 0,792,612,0
the return values โโmatch topLeftX, topLeftY, bottomRightX, bottomRightY
.
To understand these values, we need to take a look at Illustrator's coordinating system. Illustrator's interface uses a modified coordinate system rather than an actual Cartesian one . In other words, the top left of the screen is the start, not the bottom left. But when using scripts, the actual Cartesian coordinate system is used.
Due to this difference in the coordinate system, the values โโentered that are on the axis Y
must be nagative. So, if I want to reposition and resize the artboard, say move it to (200,50)
and resize it to (400,300)
, I need to do the following:
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
must be negative, otherwise you will get an error
Error 1200: Illustrator error occurred: 1346458189 ('PARM')
or incorrect resizing / resizing.
source to share
Just in case, someone meets Error: an Illustrator error occurred: 1346458189 ('PARM')
.
In CS6, at least this happens if height
not negative or ifwidth
negative. This applies to all values โโof the type rect
.
x
and y
can be positive or negative, it doesn't matter.
so that will work:
app.activeDocument.artboards.add([0 , 0, 200, -50]);
app.activeDocument.artboards.add([-10 , -10, 200, -50]);
app.activeDocument.artboards.add([10 , 10, 200, -50]);
but this one won't work:
app.activeDocument.artboards.add([0 , 0, 200, 50])
app.activeDocument.artboards.add([0 , 0, -200, -50])
app.activeDocument.artboards.add([0 , 0, -200, 50])
source to share