illustrator是矢量编辑软件,画板是绘制处理的重要容器,在印刷方面的一个重要功能就是更多方式选择目标对象,开发一个增强的选择目标对象功能,以下功能仅用于学习交流,请勿用于非法用途和商业用途,源代码如下所示:
var kOpenFilled = 0;
var kOpenUnfilled = 1;
var kClosedFilled = 2;
var kClosedUnfilled = 3;
var kStroked = 4;
var kUnstroked = 5;
var kDashed = 6;
var kUndashed = 7;
var kGuide = 8;
var kPath = 9;
var kGradient = 10;
var kPattern = 11;
var kRGBColor = 12;
var kCMYKColor = 13;
var kGrayColor = 14;
var kLabColor = 15;
var kSpotColor = 16;
function isEmpty(obj) {
for (var i in obj) {
return false;
}
return true;
}
function selectPaths(progBar, type) {
var nrFound = 0;
activeDocument.selection = null;
var pathItems = activeDocument.pathItems;
for (var i = 0; i < pathItems.length; i += 1) {
progBar.value = i * (100 / (pathItems.length - 1));
var item = pathItems[i];
switch (type) {
case kOpenFilled:
if (!item.closed && item.filled) {
item.selected = true;
nrFound++;
}
break;
case kOpenUnfilled:
if (!item.closed && !item.filled) {
item.selected = true;
nrFound++;
}
break;
case kClosedUnfilled:
if (item.closed && !item.filled) {
item.selected = true;
nrFound++;
}
break;
case kClosedFilled:
if (item.closed && item.filled) {
item.selected = true;
nrFound++;
}
break;
case kStroked:
if (item.stroked) {
item.selected = true;
nrFound++;
}
break;
case kUnstroked:
if (!item.stroked) {
item.selected = true;
nrFound++;
}
break;
case kDashed:
if (!isEmpty(item.strokeDashes)) {
item.selected = true;
nrFound++;
}
break;
case kUndashed:
if (isEmpty(item.strokeDashes)) {
item.selected = true;
nrFound++;
}
break;
case kGuide:
if (item.guides) {
item.selected = true;
nrFound++;
}
break;
case kPath:
item.selected = true;
nrFound++;
break;
case kGradient:
if (item.filled && item.fillColor.typename == "GradientColor") {
item.selected = true;
nrFound++;
}
break;
case kPattern:
if (item.filled && item.fillColor.typename == "PatternColor") {
item.selected = true;
nrFound++;
}
break;
case kRGBColor:
if (item.filled && item.fillColor.typename == "RGBColor" || item.stroked && item.strokeColor.typename == "RGBColor") {
item.selected = true;
nrFound++;
}
break;
case kCMYKColor:
if (item.filled && item.fillColor.typename == "CMYKColor" || item.stroked && item.strokeColor.typename == "CMYKColor") {
item.selected = true;
nrFound++;
}
break;
case kGrayColor:
if (item.filled && item.fillColor.typename == "GrayColor" || item.stroked && item.strokeColor.typename == "GrayColor") {
item.selected = true;
nrFound++;
}
break;
}
}
redraw();
return nrFound;
}
function selectItems(progBar, items) {
var nrFound = 0;
activeDocument.selection = null;
for (var i = 0; i < items.length; i += 1) {
progBar.value = i * (100 / (items.length - 1));
var item = items[i];
item.selected = true;
nrFound++;
}
redraw();
return nrFound;
}
function selectTransparency(progBar, items) {
var nr = 0;
for (var i = 0; i < items.length; i += 1) {
progBar.value = i * (100 / (items.length - 1));
var item = items[i];
if (item.opacity < 100) {
item.selected = true;
nr++;
}
}
return nr;
}
function selectBlendMode(progBar, items) {
var nr = 0;
for (var i = 0; i < items.length; i += 1) {
progBar.value = i * (100 / (items.length - 1));
var item = items[i];
if (item.blendingMode != BlendModes.NORMAL) {
item.selected = true;
nr++;
}
}
return nr;
}
function selectRasterItems(progBar) {
return selectItems(progBar, activeDocument.rasterItems);
}
function selectPlacedItems(progBar) {
return selectItems(progBar, activeDocument.placedItems);
}
function selectCompoundPathItems(progBar) {
return selectItems(progBar, activeDocument.compoundPathItems);
}
function selectGroupItems(progBar) {
return selectItems(progBar, activeDocument.groupItems);
}
function selectMeshItems(progBar) {
return selectItems(progBar, activeDocument.meshItems);
}
function selectSymbolItems(progBar) {
return selectItems(progBar, activeDocument.symbolItems);
}
function selectGraphItems(progBar) {
return selectItems(progBar, activeDocument.graphItems);
}
function selectTransparentItems(progBar) {
var nr = 0;
activeDocument.selection = null;
nr += selectTransparency(progBar, activeDocument.layers);
nr += selectTransparency(progBar, activeDocument.groupItems);
nr += selectTransparency(progBar, activeDocument.pathItems);
nr += selectTransparency(progBar, activeDocument.meshItems);
nr += selectTransparency(progBar, activeDocument.placedItems);
nr += selectTransparency(progBar, activeDocument.rasterItems);
nr += selectTransparency(progBar, activeDocument.symbolItems);
nr += selectTransparency(progBar, activeDocument.textFrames);
nr += selectTransparency(progBar, activeDocument.graphItems);
redraw();
return nr;
}
function selectBlendModes(progBar) {
var nr = 0;
activeDocument.selection = null;
nr += selectBlendMode(progBar, activeDocument.layers);
nr += selectBlendMode(progBar, activeDocument.groupItems);
nr += selectBlendMode(progBar, activeDocument.pathItems);
nr += selectBlendMode(progBar, activeDocument.meshItems);
nr += selectBlendMode(progBar, activeDocument.placedItems);
nr += selectBlendMode(progBar, activeDocument.rasterItems);
nr += selectBlendMode(progBar, activeDocument.symbolItems);
nr += selectBlendMode(progBar, activeDocument.textFrames);
nr += selectBlendMode(progBar, activeDocument.graphItems);
redraw();
return nr;
}
function closeSelectedPaths(progBar) {
var nrFound = 0;
var pathItems = activeDocument.pathItems;
for (var i = 0; i < pathItems.length; i += 1) {
progBar.value = i * (100 / (pathItems.length - 1));
var item = pathItems[i];
if (item.selected) {
if (!item.closed && item.filled) {
item.closed = true;
nrFound++;
}
}
}
redraw();
return nrFound;
}
function SelectDialogBuilder() {
}
SelectDialogBuilder.prototype.run = function() {
var retval = true;
function createBuilderDialog() {
var dlg = new Window("dialog", "AI脚本增强选择");
dlg.listPnl = dlg.add("panel", undefined, "请选择:");
dlg.listPnl.size = [238, 60];
dlg.listPnl.selectList = dlg.listPnl.add("dropdownlist");
dlg.listPnl.selectList.add("item", "有填充的开放路径");
dlg.listPnl.selectList.add("item", "无填充的开放路径");
dlg.listPnl.selectList.add("item", "有填充的闭合路径");
dlg.listPnl.selectList.add("item", "无填充的闭合路径");
dlg.listPnl.selectList.add("item", "有描边的路径");
dlg.listPnl.selectList.add("item", "无描边的路径");
dlg.listPnl.selectList.add("item", "虚线描边的路径");
dlg.listPnl.selectList.add("item", "实线描边的路径");
dlg.listPnl.selectList.add("item", "栅格化图形图像");
dlg.listPnl.selectList.add("item", "已导入的图片");
dlg.listPnl.selectList.add("item", "所有路径对象");
dlg.listPnl.selectList.add("item", "参考线");
dlg.listPnl.selectList.add("item", "复合路径");
dlg.listPnl.selectList.add("item", "群组对象");
dlg.listPnl.selectList.add("item", "网格对象");
dlg.listPnl.selectList.add("item", "符号对象");
dlg.listPnl.selectList.add("item", "图表曲线图");
dlg.listPnl.selectList.add("item", "透明对象");
dlg.listPnl.selectList.add("item", "已设置混合模式对象");
dlg.listPnl.selectList.add("item", "渐变填充的路径");
dlg.listPnl.selectList.add("item", "图案填充的路径");
dlg.listPnl.selectList.add("item", "RGB填充的路径");
dlg.listPnl.selectList.add("item", "CMYK填充的路径");
dlg.listPnl.selectList.add("item", "灰度填充的路径");
dlg.listPnl.selectList.add("item", "Lab填充的路径");
dlg.listPnl.selectList.add("item", "专色填充的路径");
dlg.prgPnl = dlg.add("panel", undefined, "");
dlg.prgPnl.progBar = dlg.prgPnl.add("progressbar", [20, 35, 238, 60], 0, 100);
dlg.msgPnl = dlg.add("panel", undefined, "");
dlg.msgPnl.alignChildren = "right";
dlg.msgPnl.msg = dlg.msgPnl.add("group");
with(dlg.msgPnl) {
msg.et = msg.add("statictext");
msg.et.preferredSize = [220, 30];
}
dlg.actionPnl = dlg.add("panel", undefined, " www.52cnp.com ");
dlg.actionPnl.orientation = "column";
dlg.actionPnl.closePathBtn = dlg.actionPnl.add("button", undefined, "关闭所选路径", {
name: "Close selected paths"
});
dlg.actionPnl.exitBtn = dlg.actionPnl.add("button", undefined, "退出", {
name: "Exit"
});
return dlg;
}
function initializeBuilder(builder) {
setMsgColor = function(msg, red) {
var g = msg.graphics;
if (red) {
c = g.newPen(g.PenType.SOLID_COLOR, [1, 0, 0, 1], 1);
} else {
c = g.newPen(g.PenType.SOLID_COLOR, [0, 0, 0, 1], 1);
}
g.foregroundColor = c;
};
with(builder.listPnl) {
selectList.onChanging = function() {
builder.msgPnl.msg.et.text = "";
};
selectList.onChange = function() {
builder.prgPnl.progBar.value = 0;
if (this.selection != null) {
var nr = 0;
this.enabled = false;
if (this.selection.text == "有填充的开放路径") {
nr = selectPaths(builder.prgPnl.progBar, kOpenFilled);
} else if (this.selection.text == "无填充的开放路径") {
nr = selectPaths(builder.prgPnl.progBar, kOpenUnfilled);
} else if (this.selection.text == "栅格化图形图像") {
nr = selectRasterItems(builder.prgPnl.progBar);
} else if (this.selection.text == "已导入的图片") {
nr = selectPlacedItems(builder.prgPnl.progBar);
} else if (this.selection.text == "参考线") {
nr = selectPaths(builder.prgPnl.progBar, kGuide);
} else if (this.selection.text == "所有路径对象") {
nr = selectPaths(builder.prgPnl.progBar, kPath);
} else if (this.selection.text == "无填充的闭合路径") {
nr = selectPaths(builder.prgPnl.progBar, kClosedUnfilled);
} else if (this.selection.text == "有填充的闭合路径") {
nr = selectPaths(builder.prgPnl.progBar, kClosedFilled);
} else if (this.selection.text == "有描边的路径") {
nr = selectPaths(builder.prgPnl.progBar, kStroked);
} else if (this.selection.text == "无描边的路径") {
nr = selectPaths(builder.prgPnl.progBar, kUnstroked);
} else if (this.selection.text == "虚线描边的路径") {
nr = selectPaths(builder.prgPnl.progBar, kDashed);
} else if (this.selection.text == "实线描边的路径") {
nr = selectPaths(builder.prgPnl.progBar, kUndashed);
} else if (this.selection.text == "复合路径") {
nr = selectCompoundPathItems(builder.prgPnl.progBar);
} else if (this.selection.text == "群组对象") {
nr = selectGroupItems(builder.prgPnl.progBar);
} else if (this.selection.text == "网格对象") {
nr = selectMeshItems(builder.prgPnl.progBar);
} else if (this.selection.text == "符号对象") {
nr = selectMeshItems(builder.prgPnl.progBar);
} else if (this.selection.text == "图表曲线图") {
nr = selectMeshItems(builder.prgPnl.progBar);
} else if (this.selection.text == "渐变填充的路径") {
nr = selectPaths(builder.prgPnl.progBar, kGradient);
} else if (this.selection.text == "图案填充的路径") {
nr = selectPaths(builder.prgPnl.progBar, kPattern);
} else if (this.selection.text == "RGB填充的路径") {
nr = selectPaths(builder.prgPnl.progBar, kRGBColor);
} else if (this.selection.text == "CMYK填充的路径") {
nr = selectPaths(builder.prgPnl.progBar, kCMYKColor);
} else if (this.selection.text == "灰度填充的路径") {
nr = selectPaths(builder.prgPnl.progBar, kGrayColor);
} else if (this.selection.text == "Lab填充的路径") {
nr = selectPaths(builder.prgPnl.progBar, kLabColor);
} else if (this.selection.text == "专色填充的路径") {
nr = selectPaths(builder.prgPnl.progBar, kSpotColor);
} else if (this.selection.text == "透明对象") {
nr = selectTransparentItems(builder.prgPnl.progBar);
} else if (this.selection.text == "已设置混合模式对象") {
nr = selectBlendModes(builder.prgPnl.progBar);
} else {
alert("Internal error");
}
this.enabled = true;
with(builder.msgPnl) {
msg.et.text = this.selection.text + " 找到: " + nr + " 个";
setMsgColor(msg, nr > 0);
}
}
};
}
with(builder.actionPnl) {
closePathBtn.onClick = function() {
var nr = closeSelectedPaths(builder.prgPnl.progBar);
with(builder.msgPnl) {
msg.et.text = "Paths closed: " + nr;
setMsgColor(msg, nr == 0);
}
};
exitBtn.onClick = function() {
this.parent.parent.close(1);
};
}
}
function runBuilder(builder) {
return builder.show();
}
var builder = createBuilderDialog();
initializeBuilder(builder);
runBuilder(builder);
return retval;
};
if (documents.length > 0) {
new SelectDialogBuilder().run();
}
合理的脚本代码可以有效的提高工作效率,减少重复劳动。
文章引用至 作者知了-联系方式1
文章引用至 作者知了-联系方式2