【发布时间】:2017-08-12 06:34:43
【问题描述】:
TLDR:我需要对应用了剪贴蒙版但似乎无法正确触发 Crop 的文件中的所有艺术作品运行 Pathfinder > Crop。
更新:经过几个小时的努力,我开始意识到主菜单中的裁剪选项(“效果 > 探路者 > 裁剪”)与探路者面板。我正在使用app.executeMenuCommand('Live Pathfinder Crop'); 来裁剪图像,但这显然会触发菜单操作。我需要从 Pathfinder 面板访问裁剪操作。
我有几层应用了剪贴蒙版的艺术作品。面具会导致最终产品出现一些问题,因此我需要:
- 循环遍历每一层;
- 将内容复制到新图层(可能是可选的,但使用原始图层似乎很成问题);
- 遍历层中的所有组以查找任何带有
pathItem[0].clipping === true的组; - 移除剪贴蒙版;
- 选择图层上剩余的所有内容并将其分组;
- 在艺术作品的顶部创建一个与剪贴蒙版具有相同尺寸和坐标的矩形;
- 同时选择组和矩形;和
- 在所选项目上运行
Outline Stroke、Pathfinder > Crop和Expand。
这是我的脚本。
#target illustrator
var doc = app.activeDocument;
var tempName = '-temp';
function cropGroups() {
var layers = doc.layers;
var layerCount = layers.length;
// Lock all layers
for (var i = 0; i < layerCount; i++) {
layers[i].locked = true;
}
for (var i = 0; i < layerCount; i++) {
var layer = layers[i];
// Create new empty layer
var layerCopy = layers.add();
layerCopy.name = layer.name + tempName;
// Copy all objects from original layer to new layer
var pageItems = layer.pageItems;
var pageItemCount = pageItems.length;
for (var a = pageItemCount - 1; a >= 0; a--) {
pageItems[a].duplicate(layerCopy, ElementPlacement.PLACEATBEGINNING);
}
// Loop through the new layer’s groups
var groups = layerCopy.groupItems;
var totalGroups = groups.length;
for (var g = totalGroups - 1; g >= 0; g--) {
var group = groups[g];
// Ensure group isn’t empty and has a clipping mask
if (group.pathItems.length && group.pathItems[0].clipping) {
var clippingMask = group.pathItems[0];
var clippingRect = { left: clippingMask.left, top: clippingMask.top, height: clippingMask.height, width: clippingMask.width };
clippingMask.remove();
// Time to start the selection dance…
layerCopy.hasSelectedArtwork = true;
// Add selected items to a new group
var selectedItems = doc.selection;
var cropGroup = layerCopy.groupItems.add(); // Create empty group
for (var s = 0; s < selectedItems.length; s++) {
selectedItems[s].move( cropGroup, ElementPlacement.PLACEATEND ); // Add all selected items to the new group
}
doc.selection = null;
// Create a new rectangle that matches the size of the clipping mask
var tile = layerCopy.pathItems.rectangle(clippingRect.top, clippingRect.left, clippingRect.width, clippingRect.height);
var tileColor = new RGBColor;
tile.fillColor = tileColor;
tile.move(layerCopy, ElementPlacement.PLACEATBEGINNING);
// Select all layer art again
// layerCopy.hasSelectedArtwork = true;
tile.selected = true;
cropGroup.selected = true;
// Live Pathfinder Crop
app.executeMenuCommand('OffsetPath v22');
app.executeMenuCommand('Live Pathfinder Crop');
app.executeMenuCommand('expandStyle');
doc.selection = null;
}
}
// Return the layer name back to it’s original
layerCopy.name = layerCopy.name.replace(tempName, '');
// Remove the original layer
layer.locked = false;
layer.remove();
}
}
cropGroups();
它在技术上运行良好,但裁剪操作完全不是我所期望的。当我运行脚本没有 executeMenuCommand 行,然后在 Illustrator 中手动运行这些命令时,一切都会被完美地裁剪。
我在这里错过了什么?
解决方案:
看来实际路径查找器面板中的“裁剪”功能无法通过 ExtendScript 使用,因此我最终制作了一个仅处理该任务的操作并将其保存为文件。然后我为文档中的每个剪贴蒙版调用它:
function cropTiles(cb) {
// Load the action file relative to the location of this script
var thisFile = new File($.fileName);
var basePath = thisFile.path;
app.unloadAction('action','');
app.loadAction(new File(basePath + '/actions/action.aia'));
doc.selection = null;
app.executeMenuCommand("Clipping Masks menu item");
var thisClipItem;
var esc = 50;
while (doc.selection.length != 0 && esc > 0) {
esc--;
thisClipItem = doc.selection[0];
doc.selection = null;
thisClipItem.parent.selected = true;
app.executeMenuCommand('Live Outline Stroke');
app.doScript('Crop Gallery Tile', 'action');
app.executeMenuCommand('expandStyle');
doc.selection = null;
app.executeMenuCommand("Clipping Masks menu item");
}
cb && typeof cb === 'function' && cb();
}
【问题讨论】:
标签: javascript adobe-illustrator extendscript