2 Replies Latest reply on Feb 7, 2013 12:12 PM by AAJJRR

    color change command in JavaScript

    AAJJRR

      Hello,

      I managed to write this javascript with the help of this forum. It automatically changes the colors from one swatch (byName) to another (byName) in my opened illustrator file. But if this one color doesn't exist as a fill or outline color in a file, I get an error message, because the script can not detect the color. The script stops. I am working on a batch of Illustrator files with 3 colors to change, but some files contain just 1 or 2 of the colors.

      My question is: Is there a command for javascript which says: "Only change the color, if the color appears"?

      I didn't found anything in different forums and it would help me a lot!!

       

      Many thanks in advance, I hope somebody could give me a hint!

       

      Here is the script:

       

       

       

      var docRef = app.activeDocument;

       

      with (docRef) {

       

      var findColor = swatches.getByName('TSB Dark Blue').color;

      var replaceColor = swatches.getByName('TSBDB').color;

       

      for (var i = 0; i < pathItems.length; i++) {

      if (pathItems[i].filled == true) {

      with (pathItems[i].fillColor) {

      if (red == findColor.red && green == findColor.green && blue == findColor.blue) {

      $.writeln('True');

      red = replaceColor.red, green = replaceColor.green, blue = replaceColor.blue;

      } else {

      $.writeln('False');

      }

      }

      }

      if (pathItems[i].stroked == true) {

      with (pathItems[i].strokeColor) {

      if (red == findColor.red && green == findColor.green && blue == findColor.blue) {

      $.writeln('True');

      red = replaceColor.red, green = replaceColor.green, blue = replaceColor.blue;

      } else {

      $.writeln('False');

      }

      }

      }

      }

      //saveAs(filePath, saveOptions)

      }