9 Replies Latest reply on Nov 27, 2015 9:33 AM by tpk1982

    Replace color via swatches

    tpk1982 Level 4

      Hi,

       

      I just need to find objects, texts or anything in page have spot color value of C=0 M=0 Y=0 K=100 into "Black" color.

      The below is the tried coding, it is not finding  "Black" color in swatch list.


      var doc = app.activeDocument;  
      var item = doc.pageItems;  
      var col = doc.swatches.getByName('Black');      
      for(var i=0; i<item.length; i++){  
               if(item[i].fillColor.spot.color.cyan == 0 && item[i].fillColor.spot.color.magenta == 0 && item[i].fillColor.spot.color.yellow == 0 && item[i].fillColor.spot.color.black == 100){  
                      item[i].fillColor.spot.color == col;}     }
      



      Not sure why it is not applying. It is doing fine if the color is in process mode.

       

      if(item[i].fillColor.cyan == 0 && item[i].fillColor.magenta == 0 && item[i].fillColor.yellow == 0 && item[i].fillColor.black == 100){  
                   item[i].fillColor == col;}  
      

       

      Thanks,

      Karthi

        • 1. Re: Replace color via swatches
          Qwertyfly... Level 4

          a process color does not have the spot variable/object in it. This will error.

          have a go at fillColor.typename

          if typename == "spot" seach the spot object else if typename == "CMYK" search as you have for process.

          nnot sure I have that 100% correct as I can't test it right now. run an alert on typename to double check I have the strings correct.

          • 2. Re: Replace color via swatches
            Vinicius Baptista Level 2

            Hi, try the code below:

             

            var doc = app.activeDocument;
            var item = doc.pageItems;
            
            
            //function to verify that the black color there is
            var changeColor = function(){
            var col = doc.swatches["Black"].color;
            };
            
            
            //If doesn't exist then set value to ""
            try{changeColor ()}
            catch(e){
                col = "";
            };
            
            
            //If value is "" then create the swatche Black
            if (col == ""){
                var col = doc.swatches.add();
                col.Color = CMYKColor;
                col.name = "Black";
                col.cyan = 0;
                col.magenta = 0;
                col.yellow = 0;
                col.black = 100;
            };
            
            
            //Apply the Black color to all spots with color is C=0 Y=0 M=0 K=100
            for(var i=0; i<item.length; i++){
                     if(item[i].fillColor.spot.color.cyan == 0 && item[i].fillColor.spot.color.magenta == 0 && item[i].fillColor.spot.color.yellow == 0 && item[i].fillColor.spot.color.black == 100){
                            item[i].fillColor = doc.swatches["Black"].color;
                            };
                        };
            

             

            I think it's what you want to do.

            I hope this will be useful.

             

            Best regards

            -Vinicius

            • 3. Re: Replace color via swatches
              tpk1982 Level 4

              Hi Vinicius,

               

              Thanks for your time. Your script working fine for fill color. I tried the same as stroke color. It throws error. I just tried another method. Finding k-100 value from swatch and apply with "Black". Please see the below tried codings and let me know your opinion.

               

              var docRef=app.activeDocument;

              for(i=0;i<docRef.swatches.length;i++){

                  var currSwatch=docRef.swatches[i];

                      if(currSwatch.color.typename=="SpotColor"){

                          var fillColor = currSwatch.color; 

                          if (isSpotColorBlack (fillColor) && currSwatch.name!="Black") { 

                              currSwatch.remove();

                          }

                      }

                  else if(currSwatch.color.typename=="CMYKColor"){

                          var fillColor = currSwatch.color; 

                              if (isColorBlack (fillColor) && currSwatch.name!="Black") { 

                                  currSwatch.remove();

                              }

                          }

                  }

                 

                   function isColorBlack (cmykColor) { 

                      var c = Math.round(cmykColor.cyan); 

                      var m = Math.round(cmykColor.magenta); 

                      var y = Math.round(cmykColor.yellow); 

                      var k = Math.round(cmykColor.black); 

                      if (c==0 && m==0 && y==0 && k == 100) 

                          return true 

                      else 

                          return false  } 

               

               

                  function isSpotColorBlack (spotColor) { 

                      var c = Math.round(spotColor.spot.color.cyan); 

                      var m = Math.round(spotColor.spot.color.magenta); 

                      var y = Math.round(spotColor.spot.color.yellow); 

                      var k = Math.round(spotColor.spot.color.black); 

                      if (c==0 && m==0 && y==0 && k == 100) 

                          return true 

                      else 

                          return false  }

               

              Thanks,

              Karthi

              • 4. Re: Replace color via swatches
                Vinicius Baptista Level 2

                Hummm

                 

                I did some tests and not getting any errors, please see the video below:

                 

                https://www.sendspace.com/file/wl38j8

                 

                Please, can you let me know what's your returned error?

                 

                Tks, see ya,

                -Vinicius

                • 5. Re: Replace color via swatches
                  tpk1982 Level 4

                  Hi Vinicius,

                   

                  The videos shows you tried with my codings. The error where i get is from your coding.

                   

                  var doc = app.activeDocument;  
                  var item = doc.pageItems;  
                    
                    
                  //function to verify that the black color there is  
                  var changeColor = function(){  
                  var col = doc.swatches["Black"].color;  
                  };  
                    
                    
                  //If doesn't exist then set value to ""  
                  try{changeColor ()}  
                  catch(e){  
                      col = "";  
                  };  
                    
                    
                  //If value is "" then create the swatche Black  
                  if (col == ""){  
                      var col = doc.swatches.add();  
                      col.Color = CMYKColor;  
                      col.name = "Black";  
                      col.cyan = 0;  
                      col.magenta = 0;  
                      col.yellow = 0;  
                      col.black = 100;  
                  };  
                    
                    
                  //Apply the Black color to all spots with color is C=0 Y=0 M=0 K=100  
                  for(var i=0; i<item.length; i++){  
                           if(item[i].fillColor.spot.color.cyan == 0 && item[i].fillColor.spot.color.magenta == 0 && item[i].fillColor.spot.color.yellow == 0 && item[i].fillColor.spot.color.black == 100){  
                                  item[i].fillColor = doc.swatches["Black"].color;  
                                  };  
                              }; 
                  

                   

                   

                  I changed line 32 as

                   

                  for(var i=0; i<item.length; i++){  
                           if(item[i].strokeColor.spot.color.cyan == 0 && item[i].strokeColor.spot.color.magenta == 0 && item[i].strokeColor.spot.color.yellow == 0 && item[i].strokeColor.spot.color.black == 100){  
                                  item[i].strokeColor = doc.swatches["Black"].color;  
                                  };  
                              }; 
                  

                   

                   

                  This is not working.

                   

                  Regards,

                  Karthi

                  • 6. Re: Replace color via swatches
                    Vinicius Baptista Level 2

                    Hi Karthi,

                     

                    Oh, my mistake, I think it's happening because you perform the code when the strokeColor of the item isn't a SpotColor, try verify it before, using a "If" structure like this:

                     

                    for(var i=0; i<item.length; i++){
                        if(item[i].strokeColor.typename == "SpotColor"){
                             if(item[i].strokeColor.spot.color.cyan == 0 && item[i].strokeColor.spot.color.magenta == 0 && item[i].strokeColor.spot.color.yellow == 0 && item[i].strokeColor.spot.color.black == 100){    
                                    item[i].strokeColor = doc.swatches["Black"].color;    
                            };    
                        };
                    };
                    

                     

                    Maybe it's work!

                     

                    Regards,

                    -Vinicius

                    • 7. Re: Replace color via swatches
                      tpk1982 Level 4

                      Hi Vinicius,

                       

                      Awesome...... Yes it is working for both fill and stroke spot color

                       

                      For process color  stroke i need to change like below?


                      if(item[i].strokeColor.typename == "SpotColor"


                      into


                      if(item[i].strokeColor.typename == "CMYKColor"), isn't it?

                       

                      Regards,

                      Karthi

                      • 8. Re: Replace color via swatches
                        Vinicius Baptista Level 2

                        Hi,

                         

                        You are welcome Karthi, and yes you are right, to work with Process you need use "CMYKColor" and don't forget changing this line:


                        item[i].strokeColor.spot.color.cyan == 0

                         

                        to

                         

                        item[i].strokeColor.cyan == 0


                        Regards,

                        -Vinicius

                        • 9. Re: Replace color via swatches
                          tpk1982 Level 4

                          Hi Vinicius,

                           

                          Yes, i keep that in mind.

                           

                          THANK FOR YOUR TIME AND HELP.

                           

                          Regards,

                          Karthi