1.算法程序
illustrator是矢量编辑软件,画板是绘制处理的重要容器,在印刷方面的一个重要功能就是裁切标记,开发一个裁切标记功能,源代码如下所示:
if (app.documents.length == 0) {
alert("请先建立一个新文件", "错误");
var myDoc = app.documents.add();
}
luRes = "dialog {alignChildren: 'fill', text: '裁切标记',margins:[8,8,8,8]\nall: Group { orientation: 'row',alignChildren: 'fill', \n myType: Panel {text:'类型',alignChildren: 'right',orientation: 'column',bounds:[100,0,225,110],properties:{borderStyle:'sunken'},\n lineType: Group { orientation: 'row', spacing:1,\n st: StaticText { text:'角线:' }, \n ddl: DropDownList {preferredSize: [54, 20] ,fontsize:3,properties:{items:['日式', '罗马','中式']}},\n } \n crossType: Group { orientation: 'row', spacing:1,\n cb: Checkbox { text:'套准', value: true}, \n ddl: DropDownList {preferredSize: [54, 20],properties:{items:['带圆', '无圆']}},\n } \n } \n mySize: Panel {text:'角线设置',alignChildren: 'fill',orientation: 'column',bounds:[100,0,240,0],properties:{borderStyle:'sunken'},\n lineLen: Group { orientation: 'row', \n s: StaticText { text:'长度:' }, \n e: EditText {text: 6 , preferredSize: [36, 20],active:true}, \n s: StaticText { text:'毫米' }, \n } \n lineBleed: Group { orientation: 'row', \n s: StaticText { text:'出血:' }, \n e: EditText { text:3, preferredSize: [36, 20] },\n s: StaticText { text:'毫米' }, \n } \n lineStroke: Group { orientation: 'row', \n s: StaticText { text:'描边:' }, \n e: EditText { text:0.25, preferredSize: [36, 20] },\n s: StaticText { text:'点' }, \n } \n } \n } \nall2: Group { orientation: 'row',alignChildren: 'fill',\n checkboxes: Panel { text:'选项', orientation:'column', alignChildren:'left',properties:{borderStyle:'black'},\n cb1: Checkbox { text:'添加角线图层 ', value: true}, \n cb2: Checkbox { text:'添加色标名称', value: true}, \n cb3: Checkbox { text:'添加文档信息' , value: true}, \n cb6: Checkbox { text:'包含对象边宽' }, \n cb4: Checkbox { text:'添加色条框', value: true}, \n cb5: Checkbox { text:'色条框线',alignment: 'center', value: true}, \n }\n myBisector: Panel { text:'等分线',alignChildren: 'left',orientation: 'column',bounds:[100,0,240,0],properties:{borderStyle:'sunken'},\n cb: Checkbox {text:'使用', value: true}\n myUse: Group { orientation: 'column',alignChildren: 'fill',\n myHor: Group { orientation: 'row', \n s: StaticText { text:'水平数:' }, \n e: EditText { text:2, preferredSize: [55, 20]}, } \n myHor2: Group { orientation: 'row', \n s3: StaticText { text:'切线距:' }, \n e2: EditText { text:6, preferredSize: [25, 20]},\n s4: StaticText { text:'毫米' }, \n } \n myVer: Group { orientation: 'row', \n s: StaticText {text:'垂直数:' }, \n e: EditText { text:4, preferredSize: [55, 20] }, } \n myVer2: Group { orientation: 'row', \n s3: StaticText { text:'切线距:' }, \n e2: EditText { text:6, preferredSize: [25, 20]},\n s4: StaticText { text:'毫米' }, \n } \n }\n }\n }\nbuttonGroups: Group { orientation: 'row', alignment: 'center', \ncancelBtn: Button { text:'取消',properties:{name:'cancel'}}, \nokBtn: Button { text:'确定',properties:{name:'ok'}}, \n }\n}";
var win = new Window(luRes);
win.all.myType.lineType.ddl.selection = win.all.myType.lineType.ddl.items[0];
win.all.myType.crossType.ddl.selection = win.all.myType.crossType.ddl.items[0];
selLine = win.all.myType.lineType.ddl.items[0];
selLine1 = win.all.myType.lineType.ddl.items[1];
selellipse = win.all.myType.crossType.ddl.items[0];
win.all.myType.crossType.cb.onClick = function() {
win.all.myType.crossType.ddl.enabled = this.value;
};
win.all2.myBisector.cb.onClick = function() {
win.all2.myBisector.myUse.enabled = this.value;
};
if (win.all2.myBisector.cb.value == true) {
win.all2.myBisector.cb.notify()
}
win.all2.checkboxes.cb4.onClick = function() {
win.all2.checkboxes.cb5.enabled = this.value;
};
win.center();
myReturn = win.show();
if (myReturn == true) {
var docRef = app.activeDocument;
var pointTomm = 2.83464566929134;
var pw = 0;
var ph = 0;
var x = 0;
var y = 0;
var aiver = app.version;
var fileNameLocation = 0;
var pageNumberLocation = 0;
var mCMYK = [setCMYKcolor(100, 0, 0, 0), setCMYKcolor(0, 100, 0, 0), setCMYKcolor(0, 0, 100, 0), setCMYKcolor(0, 0, 0, 100)];
mCMYK.push(setCMYKcolor(100, 100, 0, 0), setCMYKcolor(100, 0, 100, 0), setCMYKcolor(0, 100, 100, 0), setCMYKcolor(0, 0, 0, 50));
var colorBarSize = 5 * pointTomm;
var regRoundSize = 3.6 * pointTomm;
var myFont = textFonts.getByName("MicrosoftYaHei");
var myFontSize = 9;
var lineLen = eval(win.all.mySize.lineLen.e.text) * pointTomm;
var lineBleed = eval(win.all.mySize.lineBleed.e.text) * pointTomm;
var hBisector = eval(win.all2.myBisector.myUse.myHor.e.text);
var vBisector = eval(win.all2.myBisector.myUse.myVer.e.text);
var hBSpace = eval(win.all2.myBisector.myUse.myHor2.e2.text) * pointTomm;
var vBSpace = eval(win.all2.myBisector.myUse.myVer2.e2.text) * pointTomm;
var SW = eval(win.all.mySize.lineStroke.e.text);
placeCropline = true;
win.all2.checkboxes.cb1.value ? creatCropMarkLayer = true : creatCropMarkLayer = false;
win.all2.myBisector.cb.value ? placeBisector = true : placeBisector = false;
win.all2.checkboxes.cb2.value ? placeColorName = true : placeColorName = false;
win.all2.checkboxes.cb3.value ? placeFileDateName = placePageSize = true : placeFileDateName = placePageSize = false;
win.all.myType.crossType.cb.value ? placeRegMark = true : placeRegMark = false;
win.all2.checkboxes.cb4.value ? placeColorBar = true : placeColorBar = false;
win.all2.checkboxes.cb5.value ? colorBarStroked = true : colorBarStroked = false;
if (lineLen <= 0 || lineBleed <= 0 || isNaN(lineLen) == true || isNaN(lineBleed) == true) {
alert("输入的数值必须大于零!已自动更正为 长度:6mm 出血:3mm。", "错误");
lineLen = 6 * pointTomm;
lineBleed = 3 * pointTomm;
}
if (creatCropMarkLayer == true) {
docRef.layers.add();
docRef.layers[0].name = "角线图层";
}
if (docRef.selection.length > 0) {
var sl = true;
var obj = docRef.selection;
var selVB = getBounds(obj);
var docName = "文件名: " + docRef.name;
pw = selVB[2] - selVB[0];
ph = selVB[1] - selVB[3];
x = selVB[0];
y = selVB[3];
for (var i = 0; i < obj.length; i += 1) {
obj[i].selected = false;
}
setOpt();
} else if (aiver.slice(0, 2) >= 14) {
var ab = docRef.artboards.length - 1;
if (ab >= 49) {
alert("注意:页面数量超过 50 时需要较多的内存与较长的运行时间,请耐心等候。\n当出现 \"PARM\"错误时,请关掉一些不用的软件以释放可用内存!", "提示");
}
for (var j = 0; j <= ab; j += 1) {
docRef.artboards.setActiveArtboardIndex(j);
var artboardnumber = docRef.artboards.getActiveArtboardIndex() + 1;
var docName = "文件名: " + docRef.name + " 页码: " + docRef.artboards.length + "-" + artboardnumber;
pw = docRef.width;
ph = docRef.height;
x = docRef.cropBox[0];
y = docRef.cropBox[1] - ph;
setOpt();
}
} else {
pw = docRef.width;
ph = docRef.height;
var docName = "文件名: " + docRef.name;
setOpt();
}
}
function setOpt() {
regGroup = docRef.groupItems.add();
textGroup = docRef.groupItems.add();
colorBarGroup = docRef.groupItems.add();
cropmark();
if (placeColorName == true) {
colorname();
}
if (placeColorBar == true) {
colorBar();
}
if (placeBisector == true) {
Bisector();
}
if (placeFileDateName == true) {
filenameDate();
}
if (placeRegMark == true) {
Regmark();
}
if (placePageSize == true) {
pagesize();
}
}
function setCMYKcolor(c, m, y, k) {
var newCMYKcolor = new CMYKColor();
newCMYKcolor.cyan = c;
newCMYKcolor.magenta = m;
newCMYKcolor.yellow = y;
newCMYKcolor.black = k;
return newCMYKcolor;
}
function filenameDate() {
var today = new Date();
var week = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
var thisDate = today.getFullYear() + "年" + today.getMonth() + 1 + "月" + today.getDate() + "日" + " " + week[today.getDay()];
var textString = "文件名: " + docName + " " + thisDate;
var textRef = textGroup.textFrames.add();
textRef.contents = docName + " " + thisDate;
textRef.textRange.characterAttributes.size = myFontSize;
textRef.textRange.characterAttributes.textFont = myFont;
textRef.textRange.characterAttributes.fillColor = docRef.swatches[1].color;
textRef.top = (y - 2) - lineBleed;
textRef.left = ((pw - textRef.width) - lineBleed) + x;
}
function colorname() {
var textRef = textGroup.textFrames.add();
textRef.contents = "青色(C) 洋红(M) 黄色(Y) 黑色(K)";
textRef.textRange.characterAttributes.size = myFontSize;
textRef.textRange.characterAttributes.textFont = myFont;
textRef.top = (y - 2) - lineBleed;
textRef.left = x + lineBleed;
for (var i = 0; i <= 5; i += 1) {
textRef.textRange.characters[i].characterAttributes.fillColor = mCMYK[0]
}
for (var i = 6; i <= 11; i += 1) {
textRef.textRange.characters[i].characterAttributes.fillColor = mCMYK[1]
}
for (var i = 12; i <= 17; i += 1) {
textRef.textRange.characters[i].characterAttributes.fillColor = mCMYK[2]
}
for (var i = 18; i <= 22; i += 1) {
textRef.textRange.characters[i].characterAttributes.fillColor = mCMYK[3]
}
}
function pagesize() {
var sizeUnit = "MM";
var sizeX = pw;
var sizeY = ph;
switch (docRef.rulerUnits) {
case RulerUnits.Centimeters:
sizeUnit = " 厘米";
sizeX = Math.round(((pw / pointTomm) / 10) * 100) / 100;
sizeY = Math.round(((ph / pointTomm) / 10) * 100) / 100;
break;
case RulerUnits.Inches:
sizeUnit = " 英吋";
sizeX = Math.round((pw / 72) * 100) / 100;
sizeY = Math.round((ph / 72) * 100) / 100;
break;
case RulerUnits.Millimeters:
sizeUnit = " 毫米";
sizeX = Math.round(((pw / pointTomm) + 0.001) * 100) / 100;
sizeY = Math.round(((ph / pointTomm) + 0.001) * 100) / 100;
break;
case RulerUnits.Points:
sizeUnit = " 点";
sizeX = Math.round(pw * 100) / 100;
sizeY = Math.round(ph * 100) / 100;
break;
case RulerUnits.Pixels:
sizeUnit = " 像素";
sizeX = Math.round(pw * 100) / 100;
sizeY = Math.round(ph * 100) / 100;
break;
case RulerUnits.Qs:
sizeUnit = " Ha";
sizeX = Math.round(pw * 141.111) / 100;
sizeY = Math.round(ph * 141.111) / 100;
break;
default:
}
var textPageSize = textGroup.textFrames.add();
textPageSize.contents = "尺寸: " + sizeX + "X" + sizeY + sizeUnit;
textPageSize.textRange.characterAttributes.size = myFontSize;
textPageSize.textRange.characterAttributes.textFont = myFont;
textPageSize.textRange.characterAttributes.fillColor = docRef.swatches[1].color;
textPageSize.position = [((x + pw) - lineBleed) - textPageSize.width, y + ph + textPageSize.height + lineBleed];
}
function cropmark() {
var lineGroup = regGroup.groupItems.add();
var myLine = {
line: function(PTS) {
var myLineSet = lineGroup.pathItems.add();
myLineSet.setEntirePath(PTS);
myLineSet.filled = false;
myLineSet.stroked = true;
myLineSet.strokeWidth = SW;
myLineSet.strokeColor = docRef.swatches[1].color;
}
};
if (win.all.myType.lineType.ddl.selection == selLine) {
var PTS1 = [
[0, lineLen + lineBleed],
[lineLen, lineLen + lineBleed],
[lineLen, 0]
];
myLine.line(PTS1);
var PTS2 = [
[0, lineLen],
[lineLen + lineBleed, lineLen],
[lineLen + lineBleed, 0]
];
myLine.line(PTS2);
} else if (win.all.myType.lineType.ddl.selection == selLine1) {
var PTS1 = [
[0, lineLen + lineBleed],
[lineLen, lineLen + lineBleed]
];
myLine.line(PTS1);
var PTS2 = [
[lineLen + lineBleed, lineLen],
[lineLen + lineBleed, 0]
];
myLine.line(PTS2);
} else {
var PTS1 = [
[0, lineLen + lineBleed],
[lineLen, lineLen + lineBleed]
];
myLine.line(PTS1);
var PTS2 = [
[0, lineLen],
[lineLen + (lineBleed / 2), lineLen]
];
myLine.line(PTS2);
var PTS3 = [
[lineLen + lineBleed, lineLen],
[lineLen + lineBleed, 0]
];
myLine.line(PTS3);
var PTS4 = [
[lineLen, lineLen + (lineBleed / 2)],
[lineLen, 0]
];
myLine.line(PTS4);
}
lineGroup.position = [-lineGroup.width + x, y];
lineGroup2 = lineGroup.duplicate();
lineGroup2.rotate(90);
lineGroup2.left = lineGroup2.left + lineGroup2.width + pw;
lineGroup3 = lineGroup.duplicate();
lineGroup3.rotate(270);
lineGroup3.top = lineGroup3.top + lineGroup3.height + ph;
lineGroup4 = lineGroup2.duplicate();
lineGroup4.rotate(90);
lineGroup4.top = lineGroup4.top + lineGroup4.height + ph;
}
function Regmark() {
var lineGroup = regGroup.groupItems.add();
if (win.all.myType.crossType.ddl.selection == selellipse) {
ellipse = lineGroup.pathItems.ellipse((regRoundSize / 2) + y, (-regRoundSize / 2) + x, regRoundSize, regRoundSize, false, true);
with(ellipse) {
filled = false;
stroked = true;
strokeWidth = SW;
strokeColor = docRef.swatches[1].color;
}
}
var myLine = {
line: function(X1, Y1, X2, Y2) {
var myLineSet = lineGroup.pathItems.add();
myLineSet.setEntirePath([
[X1, Y1],
[X2, Y2]
]);
myLineSet.filled = false;
myLineSet.stroked = true;
myLineSet.strokeWidth = SW;
myLineSet.strokeColor = docRef.swatches[1].color;
}
};
myLine.line((-lineLen / 2) + x, y, (lineLen / 2) + x, y);
myLine.line(x, -(lineLen + lineBleed) + y, x, lineLen + lineBleed + y);
lineGroup.position = [(-lineGroup.width - lineBleed) + x, ((ph + lineGroup.height) / 2) + y];
lineGroup.duplicate().position = [pw + lineBleed + x, ((ph + lineGroup.height) / 2) + y];
lineGroup.duplicate();
lineGroup.rotate(90);
lineGroup.position = [((pw - lineGroup.width) / 2) + x, -lineBleed + y];
lineGroup.duplicate().position = [((pw - lineGroup.width) / 2) + x, ph + lineGroup.height + lineBleed + y];
}
function colorBar() {
var rectangle = {
bar: function(y, x, colorBarSize, fiColor, swColor) {
var myRectangle = colorBarGroup.pathItems.rectangle(y, x, colorBarSize, colorBarSize);
myRectangle.fillColor = fiColor;
myRectangle.stroked = false;
if (colorBarStroked == true) {
myRectangle.strokeColor = swColor;
myRectangle.strokeWidth = SW;
}
}
};
for (var i = 0; i <= 10; i += 1) {
rectangle.bar(y + (i * colorBarSize) + colorBarSize + lineBleed, ((-colorBarSize - lineBleed) + x) - 1, colorBarSize, colorBarSize, setCMYKcolor(10 * i, 0, 0, 0), mCMYK[0]);
rectangle.bar((((y + (i * colorBarSize)) - (10 * colorBarSize)) - lineBleed) + ph, ((-colorBarSize - lineBleed) + x) - 1, colorBarSize, colorBarSize, setCMYKcolor(0, 10 * i, 0, 0), mCMYK[1]);
rectangle.bar((((y + (i * colorBarSize)) - (10 * colorBarSize)) - lineBleed) + ph, lineBleed + x + pw + 1, colorBarSize, colorBarSize, setCMYKcolor(0, 0, 10 * i, 0), mCMYK[2]);
rectangle.bar(y + (i * colorBarSize) + colorBarSize + lineBleed, lineBleed + x + pw + 1, colorBarSize, colorBarSize, setCMYKcolor(0, 0, 0, 10 * i), mCMYK[3]);
}
for (var j = 0; j <= 7; j += 1) {
rectangle.bar(y + ph + colorBarSize + lineBleed + 1, lineBleed + x + (j * colorBarSize), colorBarSize, colorBarSize, mCMYK[j], docRef.swatches[1].color)
}
}
function Bisector() {
var lineGroup = regGroup.groupItems.add();
var myLine = {
line: function(X1, Y1, X2, Y2) {
var myLineSet = lineGroup.pathItems.add();
myLineSet.setEntirePath([
[X1, Y1],
[X2, Y2]
]);
myLineSet.filled = false;
myLineSet.stroked = true;
myLineSet.strokeWidth = SW;
myLineSet.strokeColor = docRef.swatches[1].color;
}
};
if (vBSpace == 0) {
for (var vML = 1; vML < vBisector; vML += 1) {
var vEquidistant = ph / vBisector;
myLine.line((x - lineLen) - lineBleed, y + (vML * vEquidistant), x - lineBleed, y + (vML * vEquidistant));
myLine.line(x + lineLen + lineBleed + pw, y + (vML * vEquidistant), x + lineBleed + pw, y + (vML * vEquidistant));
}
} else {
for (var vML = 1; vML < vBisector; vML += 1) {
var vEquidistant = (ph + vBSpace) / vBisector;
myLine.line((x - lineLen) - lineBleed, y + (vML * vEquidistant), x - lineBleed, y + (vML * vEquidistant));
myLine.line((x - lineLen) - lineBleed, (y - vBSpace) + (vML * vEquidistant), x - lineBleed, (y - vBSpace) + (vML * vEquidistant));
myLine.line(x + lineLen + lineBleed + pw, y + (vML * vEquidistant), x + lineBleed + pw, y + (vML * vEquidistant));
myLine.line(x + lineLen + lineBleed + pw, (y - vBSpace) + (vML * vEquidistant), x + lineBleed + pw, (y - vBSpace) + (vML * vEquidistant));
}
}
if (hBSpace == 0) {
for (var hML = 1; hML < hBisector; hML += 1) {
var hEquidistant = pw / hBisector;
myLine.line(x + (hML * hEquidistant), (y - lineLen) - lineBleed, x + (hML * hEquidistant), y - lineBleed);
myLine.line(x + (hML * hEquidistant), y + ph + lineBleed + lineLen, x + (hML * hEquidistant), y + ph + lineBleed);
}
} else {
for (var hML = 1; hML < hBisector; hML += 1) {
var hEquidistant = (pw + hBSpace) / hBisector;
myLine.line(x + (hML * hEquidistant), (y - lineLen) - lineBleed, x + (hML * hEquidistant), y - lineBleed);
myLine.line(x + (hML * hEquidistant), y + ph + lineBleed + lineLen, x + (hML * hEquidistant), y + ph + lineBleed);
myLine.line((x + (hML * hEquidistant)) - hBSpace, (y - lineLen) - lineBleed, (x + (hML * hEquidistant)) - hBSpace, y - lineBleed);
myLine.line((x + (hML * hEquidistant)) - hBSpace, y + ph + lineBleed + lineLen, (x + (hML * hEquidistant)) - hBSpace, y + ph + lineBleed);
}
}
}
function getBounds(obj) {
var selObj1 = new Array();
var selObj2 = new Array();
var vgb1 = new Array();
var vgb2 = new Array();
var n = obj.length;
if (n > 0) {
if (obj[0].typename == "GroupItem" && obj[0].clipped && obj[0].pageItems.length > 1) {
clipObj = obj[0].pathItems[0].geometricBounds;
vgb1 = clipObj;
} else {
selObj1 = obj[0];
if (win.all2.checkboxes.cb6.value) {
vgb1 = selObj1.visibleBounds;
} else {
vgb1 = selObj1.geometricBounds;
}
}
if (n > 1) {
for (var i = 1; i < n; i += 1) {
selObj2 = obj[i];
if (obj[i].typename == "GroupItem" && obj[i].clipped && obj[i].pageItems.length > 1) {
if (win.all2.checkboxes.cb6.value) {
vgb2 = obj[i].pathItems[0].visibleBounds;
} else {
vgb2 = obj[i].pathItems[0].geometricBounds;
}
} else {
if (win.all2.checkboxes.cb6.value) {
vgb2 = selObj2.visibleBounds;
} else {
vgb2 = selObj2.geometricBounds;
}
}
if (vgb1[0] > vgb2[0]) {
vgb1[0] = vgb2[0]
}
if (vgb1[1] < vgb2[1]) {
vgb1[1] = vgb2[1]
}
if (vgb1[2] < vgb2[2]) {
vgb1[2] = vgb2[2]
}
if (vgb1[3] > vgb2[3]) {
vgb1[3] = vgb2[3]
}
}
}
}
return vgb1;
}
2.文本转执行
使用记事本或者notepad++等文本编辑器,将代码拷贝至文本文件,然后修改后缀名为js或者jsx,最后再illustrator软件菜单文件》脚本》其它脚本,选择脚本文件,点执行即可,也可以将脚本放在预设文件夹内,会显示再脚本下的二级子菜单。如下图所示:
AI脚本执行.png
3.作者答疑
代码长度过长,如需全部项目或有疑问,请留言。
提示: 作者知了-联系方式1
提示: 作者知了-联系方式2