Search code examples
javascriptadobe-illustrator

Illustrator scripting JavaScript. have loop use number it is finding


I am trying to get coding to find a range of colors and select them. Someone was able to help with this code.

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

I have been trying to get it to use the numbers it is finding for the alert and use those as the RGB colors to select

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"); 
        }
      }
    }
  }
}

But this is not working. I am very much of not a coder my brain just can understand it. I know the basics but, that is about all can my brain will retain. This is about my 100th version of this code. I am really trying, so please don't be mad at me if i am missing something. I have taken a javascripting class, read the illustrator scripting guilds, and am all over W3schools. My brain just can not understand much for coding, so please please do not get mad at me. I really am just looking for help.

Updates

Thank you for letting me know with is out dated but, I got this coding from someone else as shown on top and i dont know enough to update things. What i am trying to get it to do is Find the RGB color breakdowns in Illustrator on objects. app.activeDocument.defaultFillColor will change my set my default color fill and app.executeMenuCommand("Find Fill Color menu item") will fine the color i have selected(default color if not one 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
 )

This will find the colors i need to select but i can get my defaultFillColor to be the colors for the code before


Solution

  • I have the feeling we are having an XY problem here as it would be useful to know what your ultimate goal is.

    Since you mention using the "Find Fill Color" menu item, I will assume you want the script to select all objects with colors in the given range (R, G and B between 200 and 210).

    Modifying Mouser's script will give us:

    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