Illustrator 脚本 JavaScript。有它找到的循环使用号

Illustrator scripting JavaScript. have loop use number it is finding

我正在尝试编码以找到一系列颜色并 select 它们。

with (app.activeDocument) { 
  if (pathItems.length > 0) {
    alert(pathItems.length);
    for (var g = 0 ; g < pathItems.length; g++) {
      if (pathItems[g].filled == true) {
        if (
          pathItems[g].fillColor.red > 200 == true &&
          pathItems[g].fillColor.red < 210 == true &&
          pathItems[g].fillColor.green > 200 == true &&
          pathItems[g].fillColor.green < 210 == true &&
          pathItems[g].fillColor.blue > 200 == true &&
          pathItems[g].fillColor.blue < 210 == true
        )
        {
          alert('R' + pathItems[g].fillColor.red + ' G' + pathItems[g].fillColor.green + ' B' + pathItems[g].fillColor.blue);
        }
      }
    }
  }
}

我一直在尝试让它使用为警报找到的数字,并将这些数字用作 select

的 RGB 颜色
with (app.activeDocument) {
  if (pathItems.length > 0) {
    alert(pathItems.length);
    for (var i = 0 ; i < pathItems.length; i++) {
      if (pathItems[i].filled == true) {
        if (
          pathItems[i].fillColor.red > 200 == true &&
          pathItems[i].fillColor.red < 210 == true &&
          pathItems[i].fillColor.green > 200 == true &&
          pathItems[i].fillColor.green < 210 == true &&
          pathItems[i].fillColor.blue > 200 == true &&
          pathItems[i].fillColor.blue < 210 == true
        );

        var newRGBColor = pathItems[i].fillColor.red &&
                          pathItems[i].fillColor.green &&
                          pathItems[i].fillColor.blue; 
        {
          app.activeDocument.defaultFillColor = newRGBColor;
          app.executeMenuCommand("Find Fill Color menu item"); 
        }
      }
    }
  }
}

但这不起作用。我非常不是我的大脑可以理解的编码器。我知道基础知识,但是,这就是我的大脑所能保留的全部内容。这是我的第 100 个版本的代码。我真的很努力,所以如果我遗漏了什么,请不要生我的气。 我参加了 javascripting class,阅读了插画家脚本协会,并且遍及 W3schools。我的大脑无法理解太多编码,所以请不要生我的气。我真的只是在寻求帮助。

更新

谢谢你让我知道 with 已经过时了,但是,我从其他人那里得到了这个编码,如顶部所示,我不知道更新东西。我想让它做的是在 Illustrator 中找到对象上的 RGB 颜色分解。 app.activeDocument.defaultFillColor 将更改我设置的默认颜色填充,app.executeMenuCommand("Find Fill Color menu item") 将调整我拥有的颜色 selected(默认颜色如果不是 selected)

 if (
   pathItems[g].fillColor.red > 200 == true &&
   pathItems[g].fillColor.red < 210 == true &&
   pathItems[g].fillColor.green > 200 == true &&
   pathItems[g].fillColor.green < 210 == true &&
   pathItems[g].fillColor.blue > 200 == true &&
   pathItems[g].fillColor.blue < 210 == true
 )

这会找到我需要的颜色 select 但我可以让我的 defaultFillColor 成为之前代码的颜色

由于运算符 &&,这很容易导致不需要的值。您想要将不同的值组合成一个表示 RGB 颜色的字符串:

看看:

 var pathItems = [{fillColor : {red : 200, green : 240, blue : 156}}]; //array - filled with colors
 
var newRGBColor = pathItems[0].fillColor.red && pathItems[0].fillColor.green && pathItems[0].fillColor.blue; 
                          
console.log(newRGBColor); //not the result expected : 156

要使其正常工作,您必须定义对象 RGBColor

redgreenblue 属性

var pathItems = [{
  fillColor: {
    red: 205,
    green: 206,
    blue: 209
  }
}]; //array - filled with colors


var newRGBColor = new RGBColor() //initial default colour
newRGBColor.red = pathItems[0].fillColor.red;
newRGBColor.green = pathItems[0].fillColor.green;
newRGBColor.blue = pathItems[0].fillColor.blue;

console.log(newRGBColor);

//dummy function to make demo work, not part of the solution
function RGBColor()
{
  this.red;
  this.green;
  this.blue;
}

最终解决方案(没有with):

var appActiveDocumentPathItems = app.activeDocument.pathItems; //create var for this object;
if (appActiveDocumentPathItems.length > 0) {
  for (var i = 0; i < appActiveDocumentPathItems.length; i++) {
    if (appActiveDocumentPathItems[i].filled == true) {
      var fill = appActiveDocumentPathItems[i].fillColor;
      if (
        fill.red > 200 == true &&
        fill.red < 210 == true &&
        fill.green > 200 == true &&
        fill.green < 210 == true &&
        fill.blue > 200 == true &&
        fill.blue < 210 == true
      ) {
        var newRGBColor = new RGBColor(); //this is how you define a new color in Illustrator's extendscript
        newRGBColor.red = pathItems[i].fillColor.red;
        newRGBColor.green = pathItems[i].fillColor.green;
        newRGBColor.blue = pathItems[i].fillColor.blue;
        app.activeDocument.defaultFillColor = newRGBColor;
        app.executeMenuCommand("Find Fill Color menu item");
      }
    }
  }
}

Remember that the this will loop all shapes that have a color filled and the last iteration of the loop (the last filled shape) will be the defaulFillColor. You need more logic to select a different color than the last one.

我觉得我们在这里有一个 XY problem,因为了解您的最终目标是什么会很有用。

由于您提到使用 "Find Fill Color" 菜单项,我假设您希望脚本 select 所有颜色在给定范围内的对象(R、G 和 B 在 200 到 210 之间) .

修改 Mouser 的脚本会给我们:

var appActiveDocumentPathItems = app.activeDocument.pathItems; //create var for this object;
var selectionArray = []; //create array for objects to be selected
if (appActiveDocumentPathItems.length > 0) {
  for (var i = 0; i < appActiveDocumentPathItems.length; i++) {
    if (appActiveDocumentPathItems[i].filled == true) {
      var fill = appActiveDocumentPathItems[i].fillColor;
      if (
        fill.red > 200 == true &&
        fill.red < 210 == true &&
        fill.green > 200 == true &&
        fill.green < 210 == true &&
        fill.blue > 200 == true &&
        fill.blue < 210 == true
      ) {
        selectionArray [selectionArray.length] = appActiveDocumentPathItems[i]; //if an object matches the requirement, add it to the array
      }
    }
  }
}

activeDocument.selection = selectionArray; //select the objects in the array