illustrator是矢量编辑软件,画板是绘制处理的重要容器,开发一个旋转画板功能,源代码如下所示:
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
var scriptName = "旋转画板和对象";
var scriptVersion = "1.2";
try {
if (documents.length > 0) {
var doc = app.activeDocument;
var currArt = doc.artboards[doc.artboards.getActiveArtboardIndex()];
var currArtNum = doc.artboards.getActiveArtboardIndex() + 1;
var lockedItems = new Array();
var hiddenItems = new Array();
var dlg = new Window("dialog", scriptName + " ver." + scriptVersion + "", undefined);
dlg.orientation = "column";
dlg.alignChildren = ["fill", "fill"];
var slctTarget = dlg.add("panel", undefined, "旋转范围");
slctTarget.orientation = "column";
slctTarget.alignChildren = "left";
slctTarget.margins = 20;
var currArtRadio = slctTarget.add("radiobutton", undefined, "当前画板 #" + currArtNum);
var allArtRadio = slctTarget.add("radiobutton", undefined, "所有画板 " + doc.artboards.length + " Artboards");
currArtRadio.value = true;
var slctAngle = dlg.add("panel", undefined, "旋转角度:");
slctAngle.orientation = "row";
slctAngle.alignChildren = ["fill", "fill"];
slctAngle.margins = 20;
var cwAngle = slctAngle.add("radiobutton", undefined, "顺时针90度");
var ccwAngle = slctAngle.add("radiobutton", undefined, "逆时针90度");
cwAngle.value = true;
var btns = dlg.add("group");
btns.alignChildren = ["fill", "fill"];
btns.margins = [0, 10, 0, 0];
var cancel = btns.add("button", undefined, "取消", {
name: "cancel"
});
cancel.helpTip = "Press Esc to Close";
cancel.onClick = function() {
dlg.close();
};
var ok = btns.add("button", undefined, "确定", {
name: "ok"
});
ok.helpTip = "Press Enter to Run";
ok.active = true;
ok.onClick = okClick;
selection = null;
app.redraw();
dlg.center();
dlg.show();
function okClick() {
saveItemsState();
if (currArtRadio.value == true) {
rotateArt(currArt);
} else {
for (var i = 0; i < doc.artboards.length; i += 1) {
doc.artboards.setActiveArtboardIndex(i);
rotateArt(doc.artboards[i]);
}
}
restoreItemsState();
dlg.close();
}
} else {
throw new Error(scriptName + "\nPlease open a document before running this script.")
}
} catch (e) {
showError(e);
}
function saveItemsState() {
for (var i = 0; i < doc.pageItems.length; i += 1) {
var currItem = doc.pageItems[i];
if (currItem.locked == true) {
lockedItems.push(i);
currItem.locked = false;
}
if (currItem.hidden == true) {
hiddenItems.push(i);
currItem.hidden = false;
}
}
}
function restoreItemsState() {
for (var i = 0; i < lockedItems.length; i += 1) {
var index = lockedItems[i];
doc.pageItems[index].locked = true;
}
for (var j = 0; j < hiddenItems.length; j += 1) {
var index = hiddenItems[j];
doc.pageItems[index].hidden = true;
}
}
function rotateArt(board) {
app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
var artRect = [].concat(board.artboardRect);
var artWidth = artRect[2] - artRect[0];
var artHeight = -(artRect[3] - artRect[1]);
deselect();
doc.selectObjectsOnActiveArtboard();
var newArtRect = [(artRect[0] + (artWidth / 2)) - (artHeight / 2), (artRect[1] - (artHeight / 2)) + (artWidth / 2), (artRect[2] - (artWidth / 2)) + (artHeight / 2), (artRect[3] + (artHeight / 2)) - (artWidth / 2)];
for (var k = 0; k < selection.length; k += 1) {
var bnds = selection[k].position;
var __width = selection[k].width;
var __height = selection[k].height;
var top = bnds[1] - artRect[1];
var left = bnds[0] - artRect[0];
if (cwAngle.value == true) {
selection[k].rotate(-90, true, true, true, true, Transformation.CENTER);
selection[k].position = [(newArtRect[2] - __height) + top, newArtRect[1] - left];
} else {
selection[k].rotate(90, true, true, true, true, Transformation.CENTER);
selection[k].position = [newArtRect[0] - top, newArtRect[3] + left + __width];
}
}
deselect();
board.artboardRect = newArtRect;
}
function deselect() {
selection = null;
}
function showError(err) {
if (confirm(scriptName + ": an unknown error has occurred.\n" + "Would you like to see more information?", true, "Unknown Error")) {
alert(err + ": on line " + err.line, "Script Error", true);
}
}
合理的脚本代码可以有效的提高工作效率,减少重复劳动。
文章引用至 作者知了-联系方式1
文章引用至 作者知了-联系方式2