如何在 Illustrator CS5.1+ 中创建画板对象的添加方法所需的矩形对象

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

本文关键字:对象 添加 方法 Illustrator CS5 创建      更新时间:2023-09-26

我正在尝试在java脚本的帮助下添加新的画板。我无处可寻。Adobe的脚本指南很差(不使用更有力的词(。

无论我尝试什么,它都会返回错误:

错误 1242:非法参数 - 参数 1 - 应显示矩形值

当我使用来自其他画板的 artboard.artboardRect 值时,它会在同一位置创建画板,但我无法修改它(调整大小(,这使得此选项无用。

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

经过广泛搜索,我发现了以下解决方法:

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

在 Illustrator 脚本的几个地方,rectsomeOtherPropRect的 PDF 被定义为"4 个数字的数组"。例如

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

返回值对应于topLeftX, topLeftY, bottomRightX, bottomRightY

为了理解这些值,我们需要看看Illustrator的协调系统。Illustrator 的 UI 使用修改后的坐标系,而不是实际的笛卡尔坐标系。换句话说,屏幕的左上角是原点,而不是左下角。但是在编写脚本时,使用实际的笛卡尔坐标系。

由于坐标系的这种差异,在Y轴上输入的值应该是 nagative 的。因此,如果我想重新定位和调整画板的大小,例如,将其移动到(200,50)并将其调整为(400,300),我需要做的是:

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

此解决方案可以包装在一个函数中:

function rect(x, y, w, h) {
    return [x, -y, (x + w), -(y + h)];
}
artboard.artboardRect = rect(200, 50, 400, 300);

YH都应该是负数,否则你会收到一个错误,说

错误 1200:发生 Illustrator 错误:1346458189("PARM"(

或不正确的重新定位/调整大小。

以防万一其他人遇到Error: an Illustrator error occurred: 1346458189 ('PARM').

在CS6中,如果height不是负数或width是负数,至少会发生这种情况。这适用于类型 rect 的所有值。

xy可以是积极的,也可以是消极的,这并不重要。

所以这将起作用:

app.activeDocument.artboards.add([0 , 0, 200, -50]);
app.activeDocument.artboards.add([-10 , -10, 200, -50]);
app.activeDocument.artboards.add([10 , 10, 200, -50]);

但这行不通:

app.activeDocument.artboards.add([0 , 0, 200, 50])
app.activeDocument.artboards.add([0 , 0, -200, -50])
app.activeDocument.artboards.add([0 , 0, -200, 50])