TLDR:我需要在应用了剪贴蒙版的文件中对所有艺术品运行Pathfinder > Crop
,但似乎无法正确触发裁剪。
更新:经过一段时间的努力,我开始意识到主菜单中的裁剪选项(“效果>路径查找器>裁剪”)与裁剪按钮完全不同在“路径查找器”面板中。我正在使用app.executeMenuCommand('Live Pathfinder Crop');
裁剪图像,但这显然会触发菜单操作。我需要从“路径查找器”面板访问裁剪操作。
我有几层艺术品都有剪裁蒙版。面具会导致最终产品出现问题,因此我需要:
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中手动运行这些命令,一切都会被完美裁剪。
我在这里缺少什么?
SOLUTION:
似乎来自实际“路径查找器”面板的“裁剪”功能无法通过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();
}
答案 0 :(得分:2)
是的,你可以执行Pathfinder&gt;通过创建Action并从代码运行它来裁剪。
从脚本运行此操作:
var fAction = new File('pathfinder-actions.aia');
app.loadAction(fAction);
app.doScript("act-crop", "PathfinderSet", false);
app.unloadAction("PathfinderSet","");
答案 1 :(得分:0)
据我所知,“路径查找器”面板中的“裁剪”与菜单中的“效果>路径查找器”中的“裁剪”之间的唯一区别在于,后者以后仅适用于分组或遮罩的对象。前者也可以处理未分组的对象。
因此,此脚本变体对我来说很好用:
var groups = app.activeDocument.groupItems;
for (var i=0; i<groups.length; i++) {
try {
if (groups[i].pathItems[0].clipping) {
groups[i].selected = true;
app.executeMenuCommand('OffsetPath v22');
app.executeMenuCommand("Live Pathfinder Crop");
app.executeMenuCommand("expandStyle");
app.selection = null;
}
} catch(e) {}
}
它只是遍历所有组,尝试选择第一个路径,如果该路径是剪贴蒙版,则执行命令。我的测试文件没有差异。我想念什么吗?