Skip to main content
richardr97091049
Participant
July 17, 2019
해결됨

how can I change the CMYK color of all selected same fill Colors?

  • July 17, 2019
  • 3 답변들
  • 2314 조회

I am trying to select all of the same fill  CMYK color and then change the color to a different fill color with CMYK. I know I can do so using the Select>Same>Fill Color, but I am trying to add it into a larger script. I feel like I am close but when I run this it is only selecting the same fill color, shows the spinning color wheel for a second or two and then leaves the colors the same. I am fairly new to Javascript so I am not sure what I am doing wrong. is anyone able to help?

var newCMYKColor = new CMYKColor(),

//the stockColors are part of the whole script. I am just using the one color for testing purposes. 

stockColors = {

   BlackPoly : [80, 72, 68, 100],

   LightBlue : [100, 43, 0, 30],

   DarkBluePoly: [95, 74, 7, 44],

   PurplePoly : [82, 98, 0, 12],

   GreenPoly : [90, 12, 95, 40],

   YellowPoly : [0, 19, 89, 0],

   EcruPoly : [6, 13, 41, 4],

   OrangePoly : [0, 73, 98, 0],

   RedPoly : [7, 100, 82, 26],

   GreyPoly : [10, 4, 4, 14],

   WhitePoly : [0, 0, 0, 0]

  };

   newCMYKColor.cyan = stockColors[0];

   newCMYKColor.magenta = stockColors[1];

   newCMYKColor.yellow = stockColors[2];

   newCMYKColor.black = stockColors[3];

var doc = app.activeDocument;


if(doc.length < 0){

newColor=stockColors[105,0,0,10];

var swatch =doc.selection["CMYK",0,0,0,10];

app.executeMenuCommand('Find Fill Color menu item');

temp = app.selection;

temp.fillColor=newColor;

}else{

alert('nope nope.')}

이 주제는 답변이 닫혔습니다.
최고의 답변: Disposition_Dev

give this one a try.

function test()

{

   //temporary hard coded values for the

   //target swatch and the new swatch

   const OLD_COLOR = "LightBlue";

   const NEW_COLOR = "GreenPoly";

   if (!app.documents.length)

   {

      alert("Please open a document first.");

      return;

   }

   //oldColor and newColor are strings representing name of color

   function changeColor(oldColor, newColor)

   {

      doc.selection = null;

      var oldSwatch = makeNewSwatch(oldColor);

      var newSwatch = makeNewSwatch(newColor);

      doc.defaultFillColor = oldSwatch.color;

      app.executeMenuCommand("Find Fill Color menu item");

      doc.defaultFillColor = newSwatch.color;

      doc.selection = null;

   }

   function makeNewSwatch(name)

   {

      var newSwatch;

      var colorValues;

      try

      {

         //check to see if the swatch already exists

         newSwatch = swatches[name];

      }

      catch (e)

      {

         try

         {

            colorValues = stockColors[name];

         }

         catch (er)

         {

            alert("Couldn't find color values for " + name + "\nUsing [0,0,0,0]");

            colorValues = [0, 0, 0, 0];

         }

         //swatch doesn't exist yet. create a new one.

         var color = new CMYKColor();

         color.cyan = colorValues[0];

         color.magenta = colorValues[1];

         color.yellow = colorValues[2];

         color.black = colorValues[3];

         newSwatch = swatches.add();

         newSwatch.name = name;

         newSwatch.color = color;

      }

      return newSwatch;

   }

   var doc = app.activeDocument,

      swatches = doc.swatches,

      newColor,

      newCMYKColor = new CMYKColor(),

      //the stockColors are part of the whole script. I am just using the one color for testing purposes.

      stockColors = {

         BlackPoly: [80, 72, 68, 100],

         LightBlue: [100, 43, 0, 30],

         DarkBluePoly: [95, 74, 7, 44],

         PurplePoly: [82, 98, 0, 12],

         GreenPoly: [90, 12, 95, 40],

         YellowPoly: [0, 19, 89, 0],

         EcruPoly: [6, 13, 41, 4],

         OrangePoly: [0, 73, 98, 0],

         RedPoly: [7, 100, 82, 26],

         GreyPoly: [10, 4, 4, 14],

         WhitePoly: [0, 0, 0, 0]

      };

   changeColor(OLD_COLOR, NEW_COLOR);

}

test();

3 답변

Disposition_Dev
Legend
July 19, 2019

give this one a try.

function test()

{

   //temporary hard coded values for the

   //target swatch and the new swatch

   const OLD_COLOR = "LightBlue";

   const NEW_COLOR = "GreenPoly";

   if (!app.documents.length)

   {

      alert("Please open a document first.");

      return;

   }

   //oldColor and newColor are strings representing name of color

   function changeColor(oldColor, newColor)

   {

      doc.selection = null;

      var oldSwatch = makeNewSwatch(oldColor);

      var newSwatch = makeNewSwatch(newColor);

      doc.defaultFillColor = oldSwatch.color;

      app.executeMenuCommand("Find Fill Color menu item");

      doc.defaultFillColor = newSwatch.color;

      doc.selection = null;

   }

   function makeNewSwatch(name)

   {

      var newSwatch;

      var colorValues;

      try

      {

         //check to see if the swatch already exists

         newSwatch = swatches[name];

      }

      catch (e)

      {

         try

         {

            colorValues = stockColors[name];

         }

         catch (er)

         {

            alert("Couldn't find color values for " + name + "\nUsing [0,0,0,0]");

            colorValues = [0, 0, 0, 0];

         }

         //swatch doesn't exist yet. create a new one.

         var color = new CMYKColor();

         color.cyan = colorValues[0];

         color.magenta = colorValues[1];

         color.yellow = colorValues[2];

         color.black = colorValues[3];

         newSwatch = swatches.add();

         newSwatch.name = name;

         newSwatch.color = color;

      }

      return newSwatch;

   }

   var doc = app.activeDocument,

      swatches = doc.swatches,

      newColor,

      newCMYKColor = new CMYKColor(),

      //the stockColors are part of the whole script. I am just using the one color for testing purposes.

      stockColors = {

         BlackPoly: [80, 72, 68, 100],

         LightBlue: [100, 43, 0, 30],

         DarkBluePoly: [95, 74, 7, 44],

         PurplePoly: [82, 98, 0, 12],

         GreenPoly: [90, 12, 95, 40],

         YellowPoly: [0, 19, 89, 0],

         EcruPoly: [6, 13, 41, 4],

         OrangePoly: [0, 73, 98, 0],

         RedPoly: [7, 100, 82, 26],

         GreyPoly: [10, 4, 4, 14],

         WhitePoly: [0, 0, 0, 0]

      };

   changeColor(OLD_COLOR, NEW_COLOR);

}

test();

richardr97091049
Participant
July 22, 2019

this is super helpful thank you! the color part is working fine. it seems to be grabbing the wrong layers and having issues with that but that shouldn't be hard to figure out. to answer your questions,

the stockColors are going to be pulled from a JSON file which will happen many times a day so the new color would be `stockColor[clipBoardColor]` or something like that.

the starting old color is always the same `new  CMYKColor(0,0,0,10)`.

thank you so much for your Help williamadowling​​!

Disposition_Dev
Legend
July 22, 2019

you're very welcome. be aware, however that this isn't very robust and there is not much error handling, so please use it carefully and backup your files just in case.

as far as the layers issue.. this script doesn't manipulate layers in any way, nor does it discriminate which layer from which to select artwork. If there are specific layers you want to exclude from the selection, you can lock those layers before the selection is made.

Disposition_Dev
Legend
July 19, 2019

Ok, so there are a few issues here. You're misusing your objects and you're not correctly defining your swatch and newColor variables for the same reason.

In this line:

newCMYKColor.cyan = stockColors[0];

you aren't referencing any specific stockColor property. You're trying to access the first element of the stockColors object as though it's an array. You can't grab object properties by index, you need to grab them by key. You need to call out a specific property from stockColors, like so:

newCMYKColor.cyan = stockColors["BlackPoly"][0];

newCMYKColor.magenta = stockColors["BlackPoly"][1];

etc.

However i just noticed that while you are initializing a new CMYKColor() object and filling out it's properties, you never actually use it... But the above information is still good to know.

The next issue is your newColor variable. Again, you need to reference a specific property from stockColors.. Try this:

newColor = stockColors["PurplePoly"]; //this will set newColor equal to the array of integers, then you can use those to make your swatch

Hmmm. there are more issues, but i'm struggling to decide what the best path is here... I can see a lot of ways to improve this whole thing and make it more abstract for future use, but I don't know the use cases or how often this will be used (and thus whether it's worth the investment to do that stuff).

How do you plan to tell the script what the old color is (which color should be selected) and what the new color is (the color to which you want to change the selected artwork)? I assume this will be dynamic and that it's not always going to be changing the exact same colors.

pixxxelschubser
Community Expert
Community Expert
July 17, 2019