2

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

Lady_dragon4
  • 81
  • 1
  • 10
  • 1
    Can you explain what you mean with "use those as the RGB colors to select"? Assuming everything worked and you wrote the perfect code, what is this script supposed to actually be doing to your illustrator content? It sounds like you want to select every single element that uses any colour found in a range of colours, but the "Find Fill Color menu item" will only find one particular match, so you'd end up basically doing a thousand searching and only seeing/keeping the last one. – Mike 'Pomax' Kamermans Aug 14 '18 at 15:09
  • Incidentally-- I know that CS might have its own flavor of JavaScript, but generally using [`with` is not recommended](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with). – Alexander Nied Aug 14 '18 at 15:12
  • 2
    CS uses Javascript from 1998. You use what you have to, including `with`. It lacks so much of modern JS that it's not worth applying today's best practices to a programming language that's stuck in _literally_ 20 years ago. (It would be lovely if we could at least use the Nodejs that's hidden along the CS applications, which is currently the horribly out of date 4.x, but at least it'd be better than what you're actually forced to script with) – Mike 'Pomax' Kamermans Aug 14 '18 at 15:15
  • Edits made to question – Lady_dragon4 Aug 14 '18 at 15:25
  • It looks like you're defining newRGBColor between the opening condition of your if statement and the opening bracket of the statement logic. I would expect that to throw a syntax error, but maybe that's an OG Javascript thing? – Ryan Gibbs Aug 14 '18 at 15:47
  • yes my coding is having not working, but i dont know how to fix it – Lady_dragon4 Aug 14 '18 at 15:53
  • @Mike. I only need it to find the last color. I have a couple thousand files to go and each one has a logo i need to find, but in each file the color is a little different, so there is only one color it needs to find. – Lady_dragon4 Aug 14 '18 at 16:00
  • I can highly recommend formatting your code to be as clear as possible: I did that for you in your post, which reveals some very obvious problems with your changed code: you've closed your `if` with `;`, so it does nothing anymore, then you make and use `newRGBColor` in a block (which enclosing `{` and `}` that now do nothing because they're not tied to that `if`) that sets it as the new default fill color. So first you'll want to fix that again. If you're not too familiar with programming, space and align things _as much as possible_ because you're going to have to reread your code. – Mike 'Pomax' Kamermans Aug 14 '18 at 16:08
  • Thank you for doing that. I got that code from someone else and since it worked i am afraid to change things. – Lady_dragon4 Aug 14 '18 at 16:26
  • The first block, so the code you showed as "someone else's" is fine. The second block, which you implied you modified yourself, is not fine. Putting a `;` after something tells JS that this statement is done, so `if (...);` is considered a complete statement and the code moves on irrespective of what that condition was. If you want your own code to happen "if the condition passes" then you need to put it inside those `{ }` right after the `if( ... )` - don't put code in between the closing `)` and the subsequent `{`. – Mike 'Pomax' Kamermans Aug 14 '18 at 17:18

2 Answers2

1

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
cybernetic.nomad
  • 6,100
  • 3
  • 18
  • 31
0

This would very easily result in an unwanted value because of the operator &&. You want to combine the different values into one string that represents a RGB-colour:

take a look:

 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

To get it to work you must define the red, green and blue properties of the object RGBColor

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

Final solution (without 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.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
Mouser
  • 13,132
  • 3
  • 28
  • 54
  • At the very least cache some of these things. Have a `var fill = appActiveDocumentPathItems[i].fillColor` and use that in the conditional. Also you kept the `;` after the `if` so this will completely ignore that condition =( – Mike 'Pomax' Kamermans Aug 14 '18 at 17:14
  • The caching is optimizing. Updated it. The `;` was an error indeed. Fixed that. Thanks. – Mouser Aug 14 '18 at 17:22
  • I tried running the code and the sent back an error. _Error 9: Illegal use of the reserved word 'var'. Line: 6 var fill = appActiveDocumentPathItems[i].fillColor;_ – Lady_dragon4 Aug 14 '18 at 17:45
  • that's because it got put inside the `if` statement. – Mike 'Pomax' Kamermans Aug 14 '18 at 18:03
  • how would you recommend i fix it, because i think if i moved it out side of the if statement it would confuse it more. Could i move the whole var statement out side of the block and maybe just add a function or something? – Lady_dragon4 Aug 14 '18 at 18:12