小华同学 发表于 2025-3-14 08:23:12

Illustrator 画板适合对象(需要支持蒙版)脚本

我编写了一个画板适合对象脚本,想让脚本适合蒙版,哪位大神帮看看,谢谢了!


/*

Artboard To Selection
Copyright 2022 William Campbell
All Rights Reserved
https://www.marspremedia.com/contact

Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

*/

(function () {

    var defaults = {
      margin: 30,
      // units: "像素"
      // units: "点"
      // units: "派克"
      // units: "英寸"
      units: "毫米"
      // units: "厘米"
    };

    var title = "Artboard To Selection";

    if (!/illustrator/i.test(app.name)) {
      alert("Script for Illustrator", title, false);
      return;
    }

    // Script variables.
    var doc;
    var measurementUnits;
    var measurementUnitsShort;

    // Reusable UI variables.
    var g; // group
    var p; // panel
    var w; // window

    // Permanent UI variables.
    var btnCancel;
    var btnOk;
    var inpMargin;
    var listUnits;

    // SETUP

    measurementUnits = [
      "像素",
      "点",
      "派克",
      "英寸",
      "毫米",
      "厘米"
    ];
    measurementUnitsShort = [
      "px",
      "pt",
      "pc",
      "in",
      "mm",
      "cm"
    ];

    // Script requires open document.
    if (!app.documents.length) {
      alert("打开文档", title, false);
      return;
    }
    doc = app.activeDocument;
    // Need at least one element selected.
    if (!doc.selection) {
      alert("Select something", title, false);
      return;
    }
    // Script doesn't work in Isolation Mode.
    if (doc.layers.name == "隔离模式") {
      alert("Exit Isolation Mode before running script", title, false);
      return;
    }

    // CREATE USER INTERFACE

    w = new Window("dialog", title);
    w.alignChildren = "fill";
    // Panel.
    p = w.add("panel");
    p.alignChildren = "left";
    g = p.add("group");
    g.add("statictext", undefined, "出血:");
    inpMargin = g.add("edittext");
    inpMargin.preferredSize.width = 50;
    listUnits = g.add("dropdownlist", undefined, measurementUnits);
    listUnits.preferredSize.width = 100;

    // Action Buttons.
    g = w.add("group");
    g.alignment = "center";
    btnCancel = g.add("button", undefined, "取消");
    btnOk = g.add("button", undefined, "确定");
    // Panel Copyright.
    p = w.add("panel");
    p.add("statictext", undefined, "Copyright 2022 William Campbell");

    // SET UI VALUES

    inpMargin.text = defaults.margin;
    listUnits.selection = listUnits.find(defaults.units) || 0;
    listUnits.priorIndex = listUnits.selection.index;

    // UI ELEMENT EVENT HANDLERS

    inpMargin.onChange = function () {
      validateFloat(this);
    };

    listUnits.onChange = function () {
      var index;
      var mu1;
      var mu2;
      var convert = function (uiEdit) {
            if (/,/.test(uiEdit.text)) {
                // Has comma: appears decimal comma.
                // Replace comma with point, then restore comma.
                uiEdit.text = String(parseFloat(UnitValue(Number(uiEdit.text.replace(",", ".")), mu1).as(mu2).toFixed(4))).replace(".", ",");
            } else {
                // Default: decimal point.
                uiEdit.text = String(parseFloat(UnitValue(Number(uiEdit.text), mu1).as(mu2).toFixed(4)));
            }
      };
      index = this.selection.index;
      if (index != this.priorIndex) {
            // Units have changed.
            mu1 = measurementUnitsShort;
            mu2 = measurementUnitsShort;
            convert(inpMargin);
      }
      this.priorIndex = index;
    };

    btnOk.onClick = function () {
      w.close(1);
    };

    btnCancel.onClick = function () {
      w.close(0);
    };

    // DISPLAY THE DIALOG

    if (w.show() == 1) {
      try {
            process();
      } catch (e) {
            alert("An error has occurred.\nLine " + e.line + ": " + e.message, title, true);
      }
    }

    //====================================================================
    //               END PROGRAM EXECUTION, BEGIN FUNCTIONS
    //====================================================================

    function process() {
      var artboard;
      var docScaleFactor;
      var padding;
      var vb;

      docScaleFactor = doc.scaleFactor || 1;
      padding = valueAsPoints(inpMargin, listUnits) / docScaleFactor;

      // Get the bounds of the selected items considering masks
      vb = getBoundsWithMask(doc.selection);

      artboard = doc.artboards;
      artboard.artboardRect = [
            vb - padding,
            vb + padding,
            vb + padding,
            vb - padding
      ];
    }

    function validateFloat(uiEdit) {
      var s;
      var v;
      // Remove non-digits.
      s = uiEdit.text.replace(/[^0-9.]/g, "");
      if (s != uiEdit.text) {
            alert("Numeric input only.\nNon-numeric characters removed.", " ", false);
      }
      // No more than one decimal point.
      s = s.replace(/\.{2,}/g, ".");
      v = parseFloat(s) || 0;
      uiEdit.text = v.toString();
    }

    function valueAsPoints(uiEdit, uiList) {
      var mu;
      var v;
      mu = measurementUnitsShort;
      v = parseFloat(uiEdit.text) || 0;
      return UnitValue(v, mu).as("pt");
    }

    // Function to get the bounds of selected items considering masks
    function getBoundsWithMask(selectedItems) {
      var allItems = [];
      for (var i = 0; i < selectedItems.length; i++) {
            var item = selectedItems;
            if (item.typename === "GroupItem" && item.clipped) {
                var groupItems = item.pageItems;
                for (var j = 0; j < groupItems.length; j++) {
                  allItems.push(groupItems);
                }
            } else {
                allItems.push(item);
            }
      }

      // Initialize bounds
      var minX = Infinity;
      var minY = Infinity;
      var maxX = -Infinity;
      var maxY = -Infinity;

      // Calculate bounds
      for (var k = 0; k < allItems.length; k++) {
            var bounds = allItems.geometricBounds;
            minX = Math.min(minX, bounds);
            minY = Math.min(minY, bounds);
            maxX = Math.max(maxX, bounds);
            maxY = Math.max(maxY, bounds);
      }

      return ;
    }

})();

mxliuxing 发表于 2025-3-14 10:10:25

帮你改好了,拿去用吧

hongcailh 发表于 2025-3-14 10:18:40

这个我也需要,下了试下看好不好用!先谢谢了

mxliuxing 发表于 2025-3-14 10:37:17

mxliuxing 发表于 2025-3-14 10:10
帮你改好了,拿去用吧

适合纯蒙版录屏

hongcailh 发表于 2025-3-14 10:52:38

mxliuxing 发表于 2025-3-14 10:10
帮你改好了,拿去用吧

默认出血30,能不能改一下,我拼版出血3就够了,每次用都要重新输入数字,比较麻烦

mxliuxing 发表于 2025-3-14 11:03:55

hongcailh 发表于 2025-3-14 10:52
默认出血30,能不能改一下,我拼版出血3就够了,每次用都要重新输入数字,比较麻烦 ...

默认输入边距3mm:画板适合对象(支持蒙版-3mm)

hongcailh 发表于 2025-3-14 11:21:33

mxliuxing 发表于 2025-3-14 11:03
默认输入边距3mm:画板适合对象(支持蒙版-3mm)

太感谢了,我先试用下,这样应该就方便多了!

小华同学 发表于 2025-3-14 16:31:10

mxliuxing 发表于 2025-3-14 10:10
帮你改好了,拿去用吧

技术不错啊! {:4_314:}

hongcailh 发表于 2025-3-17 15:31:08

昨天用软件试了下,单纯的带蒙版图片是可以的,如果是蒙版图片和文字(或其它图形)再群组一起的话就无效,希望能修改下,谢谢
页: [1]
查看完整版本: Illustrator 画板适合对象(需要支持蒙版)脚本