javascript - Illustrator JavaScript 只能运行一次 - 不会切换

标签 javascript adobe-illustrator extendscript

我正在编写一个脚本来改变火柴人 ARM 和腿的颜色。一旦选择“he”,它实际上只是镜像颜色:

//Declare and initialize variables
var msgType = "";
var app;
var docRef = app.activeDocument;
var testColor = docRef.swatches.getByName("CMYK Green");
var leftColor = docRef.swatches.getByName("LeftColor");
var rightColor = docRef.swatches.getByName("RightColor");

function sameColor(CMYKColor1, CMYKColor2) {
    "use strict";
    var isTheSameColor;
    if ((CMYKColor1.cyan === CMYKColor2.cyan) && (CMYKColor1.magenta === CMYKColor2.magenta) && (CMYKColor1.yellow === CMYKColor2.yellow) && (CMYKColor1.black === CMYKColor2.black)) {
        isTheSameColor = true;
    } else {
        isTheSameColor = false;
    }
    return isTheSameColor;
}


// check if a document is open in Illustrator.
if (app.documents.length > 0) {
    var mySelection = app.activeDocument.selection;
    var index;
    // Loop through all selected objects
    for (index = 0; index < mySelection.length; index += 1) {
        // Switch left and right colours
        if (sameColor(mySelection[index].strokeColor, leftColor.color)) {
            mySelection[index].strokeColor = rightColor.color;
        }
        if (sameColor(mySelection[index].strokeColor, rightColor.color)) {
            mySelection[index].strokeColor = leftColor.color;
        }
        if (sameColor(mySelection[index].fillColor, leftColor.color)) {
            mySelection[index].fillColor = rightColor.color;
        }
        if (sameColor(mySelection[index].fillColor, rightColor.color)) {
            mySelection[index].fillColor = leftColor.color;
        }

    }
}

它可以工作,但只能工作一次(即我无法再次切换更改)。如果我撤消更改并重试,它会再次起作用。这是为什么?

最佳答案

经过大量的绞尽脑汁/调试后,发现它正在将 CMYK 值更改为不完全相同(只有很小一部分)。

enter image description here

更改了以下内容:

if ((CMYKColor1.cyan === CMYKColor2.cyan) ...

至:

if ((Math.round(CMYKColor1.cyan) === Math.round(CMYKColor2.cyan)) ... 

现在工作正常。

关于javascript - Illustrator JavaScript 只能运行一次 - 不会切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24015252/

相关文章:

javascript - angularjs ng-model 绑定(bind)到 wtforms csrf 字段

javascript - 令人困惑的 JavaScript 语句 : "var x = new this();"

javascript - 插画填充颜色对象

html - 我怎样才能让这个 svg 元素从中心旋转

javascript - Illustrator插件区

Javascript:访问数组中作为对象属性的给定索引

javascript - AngularJS:单选按钮不适用于 Bootstrap 3

javascript - 尽管使用了 zIndex,React-native-paper 菜单仍隐藏在其他元素后面。我如何将元素置于顶部?

javascript - extendscript:获取项目的文件路径

javascript - 无法使用 Extendscript 在 Illustrator 中以编程方式创建剪贴蒙版