我可以从 Pathfinder 面板执行“Pathfinder > Crop”而不是“Effects > Pathfinder”菜单吗?

Can I execute “Pathfinder > Crop” from the Pathfinder panel and NOT the “Effects > Pathfinder” menu?

TLDR: 我需要 运行 Pathfinder > Crop 应用了剪切蒙版但似乎无法裁剪的文件中的所有艺术作品正确开火。

更新: 经过数小时的努力,我开始意识到主菜单中的“裁剪”选项(“效果 > 探路者 > 裁剪”)所做的事情完全不同于“路径查找器”面板中的“裁剪”按钮。我正在使用 app.executeMenuCommand('Live Pathfinder Crop'); 裁剪图像,但这显然会触发菜单操作。我需要从探路者面板访问裁剪操作。


我有几个图层应用了剪贴蒙版。面具在最终产品中引起了几个问题,所以我需要:

  1. 遍历每一层;
  2. 将内容复制到新图层(可能是可选的,但使用原始图层似乎很有问题);
  3. 循环遍历层中的所有组以找到具有 pathItem[0].clipping === true;
  4. 的任何组
  5. 移除剪贴蒙版;
  6. Select 并将图层上剩余的所有内容分组;
  7. 在艺术作品的顶部创建一个矩形,其尺寸和坐标与剪贴蒙版具有的相同;
  8. Select组和矩形;和
  9. 运行 Outline StrokePathfinder > CropExpand 所选项目。

这是我的脚本。

#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();
}

是的,您可以通过创建 Action 和 运行 从代码中执行 Pathfinder > Crop。

  • 打开动作面板并创建新的动作集,例如 PathfinderSet
  • 创建新的空动作,例如 act-crop
  • 开始记录并记录一步 - 对某些对象应用探路者裁剪
  • 打开操作上下文菜单并将 PathfinderSet 保存到文件,例如 'pathfinder-actions.aia'

运行 此操作来自脚本:

  var fAction = new File('pathfinder-actions.aia');
  app.loadAction(fAction); 
  app.doScript("act-crop", "PathfinderSet", false);   
  app.unloadAction("PathfinderSet",""); 

据我所知,探路者面板中的 'Crop' 和菜单 'Effect > Pathfinder' 中的 'Crop' 之间的唯一区别是后者仅适用于分组或蒙版对象。而前者也处理未分组的对象。

所以,这个脚本变体对我来说很好用:

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) {}
}

它只是遍历所有组,尝试选择第一条路径,如果路径是剪辑蒙版,则执行命令。我看不出我的测试文件有什么不同。我错过了什么吗?