【问题标题】:Can I execute “Pathfinder > Crop” from the Pathfinder panel and NOT the “Effects > Pathfinder” menu?我可以从 Pathfinder 面板执行“Pathfinder > Crop”而不是“Effects > Pathfinder”菜单吗?
【发布时间】:2017-08-12 06:34:43
【问题描述】:

TLDR:我需要对应用了剪贴蒙版但似乎无法正确触发 Crop 的文件中的所有艺术作品运行 Pathfinder > Crop

更新:经过几个小时的努力,我开始意识到主菜单中的裁剪选项(“效果 > 探路者 > 裁剪”)与探路者面板。我正在使用app.executeMenuCommand('Live Pathfinder Crop'); 来裁剪图像,但这显然会触发菜单操作。我需要从 Pathfinder 面板访问裁剪操作。


我有几层应用了剪贴蒙版的艺术作品。面具会导致最终产品出现一些问题,因此我需要:

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

【问题讨论】:

    标签: javascript adobe-illustrator extendscript


    【解决方案1】:

    据我所知,Pathfinder 面板中的“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) {}
    }
    

    它只是遍历所有组,尝试选择第一个路径,如果路径是剪辑蒙版,则执行命令。我的测试文件没有任何区别。我错过了什么吗?

    【讨论】:

      【解决方案2】:

      是的,您可以通过创建 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",""); 
      

      【讨论】:

      • 这正是我最终要做的。
      猜你喜欢
      • 1970-01-01
      • 2022-07-28
      • 1970-01-01
      • 2020-06-27
      • 2022-07-02
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多