TLDR:我需要運行Pathfinder > Crop
所有的藝術在一個文件中應用了剪貼蒙版,但似乎無法讓Crop正確啓動。我可以從Pathfinder面板執行「Pathfinder> Crop」而不是「Effects> Pathfinder」菜單嗎?
UPDATE:後在這個小打小鬧小時,我才明白,在主菜單(「效果>探路>農作物」)裁剪選項做了比探路者面板的裁切按鈕完全不同。我使用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中手動運行這些命令時,所有內容都會被完美剪裁。
我在這裏錯過了什麼?
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();
}
這正是我最終做的。 –