9 Replies Latest reply on Nov 26, 2015 10:23 PM by Qwertyfly...

    Please help me with object color identification

    tpk1982 Level 4

      Hi Experts,

       

      I am new for illustrator scripting. My requirement is,

       

      1) Select a particular layer (i used active layer)

      2) Check all the objects (in active layer) stroke color with name of  "Die" and that should be in spot color. (i have this Die color in swatch list already)

      3) If no, then need to alert

       

      Below is the tried codings:

      var e=0;  
          var docRef = app.activeDocument;    
          var layers = docRef.layers;    
          var myLayer = docRef.activeLayer; //this defines the layer that you want to get the selection from    
          docRef.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.    
          for(var i=0;i<myLayer.pathItems.length;i++){ //here we are looping through each pathItem of myLayer.    
              var currentItem = myLayer.pathItems[i];    
              var findColor = app.activeDocument.swatches.getByName("Die");     
                currentItem.selected = true;    
                     if(currentItem.strokeColor.name != findColor.color){  
                  e=1;  
              }  
           }  
        
          if(e==1){  
              alert("Some process color used in Die");  
          }  
          else{  
              alert("Die only used 'Die' spot color" );  
          }  
      
      

       

      Advance thanks,

      Karthi

        • 1. Re: Please help me with object color identification
          Qwertyfly... Level 4

          a few try statements will stop crashes, and you need to go deeper to get spot color name and your findColor.color should be referencing name.

          try this:

           

          as is will select everything on active layer that does Not have a stroke colour named "Die"

          revers this by changing the first Variable "selectDIE" to true.

           

          function DieLine(){
              var selectDIE = false;
              var doc = app.activeDocument;
              var items = doc.activeLayer.pathItems;
              doc.selection = null;
              try{
                  var col = doc.swatches.getByName('Die');
              }catch(e){
                  alert('No swatched named "Die" in this document');
                  return;
              }
              for(var i=0; i<items.length; i++){
                  try{
                      if(items[i].strokeColor.spot.name == col.name && selectDIE == true){
                          items[i].selected = true;
                      }
                  }catch(e){
                      if(selectDIE == false){
                          items[i].selected = true;
                      }
                  }
              }
          }
          DieLine();
          

           

          is this kinda what you want?

          • 2. Re: Please help me with object color identification
            tpk1982 Level 4

            Hi Qwertyfly,

             

            Awesome. It is selecting the objects of active layer without swatch name "Die".

             

            But if i apply a color which enable "Global" in swatch panel. Script consider this as spot name and ignore this colored objects.

             

            Screen shot 2015-11-26 at 12.46.10 PM.png

             

            Is it possible to rectify this?

             

            Regards,

            Karthi

            • 3. Re: Please help me with object color identification
              tpk1982 Level 4

              Hi Qwertfly,

               

              I modified your codings and its working fine. Is it correct?

               

              DieLine();  
              
              
              function DieLine(){  
                  var e = 0;  
                  var doc = app.activeDocument;  
                  var items = doc.activeLayer.pathItems;  
                  doc.selection = null;  
                  try{  
                      var col = doc.swatches.getByName('Die');  
                  }catch(e){  
                      alert('No swatched named "Die" in this document');  
                      return;  
                  }  
                  for(var i=0; i<items.length; i++){  
                     if(items[i].strokeColor.spot.name != col.name){  
                           e=1
                      }  
              }
              if (e==1){   alert("Objects contain other colors");} 
              else{alert("Objects contain only 'die' colors");} 
              }  
              
              
              
              

               

              Thanks,

              Karthi

              • 4. Re: Please help me with object color identification
                Qwertyfly... Level 4

                I Think you will need to put a try around the if that sits in the for.

                if that does not make sense I can show you.

                but im on my phone and it won't even let me turn off bold...

                • 5. Re: Please help me with object color identification
                  tpk1982 Level 4

                  Hi Qwertyfly,

                   

                  Thanks for your time. Below is the code working fine from my end. Please correct me, the try condition i used is correct.

                   

                  DieLine();  
                  
                  //checking other than Dieline swatch
                  function DieLine(){  
                      var e = 0;  
                      var err=0;
                      var doc = app.activeDocument;  
                      var items = doc.activeLayer.pathItems;  
                      doc.selection = null;  
                      try{  
                          var col = doc.swatches.getByName('Die');  
                      }catch(e){  
                          alert('No swatched named "Die" in this document');  
                          return;  
                      }  
                      for(var i=0; i<items.length; i++){  
                          try{
                              if(items[i].strokeColor.spot.name != col.name){  
                               e=1;            
                              }
                          if(items[i].fillColor.spot.name != col.name){  
                               err=1;            
                              }
                          }
                      catch(e){continue;}
                  }
                      if(e==1){alert("Objects contain other stroke colors");} 
                      else{alert("Objects contain only 'Diee' stroke colors");} 
                      if (err==1){alert("Objects contain other fillcolors");} 
                      else{alert("Objects contain only 'Die' fill colors");}
                  

                   

                  Thanks,

                  Karthi

                  • 6. Re: Please help me with object color identification
                    Qwertyfly... Level 4

                    I think you have an extra if that's not needed but won't hurt it.

                    i Cant test till I'm back at the PC.

                    if it works and does not throw errors or crash then it's doing its job.

                    iI'll take a look tomorrow and let you know.

                    • 7. Re: Please help me with object color identification
                      Qwertyfly... Level 4

                      the extra if I thought you had is fine. reading it on my phone I missed that one checks stroke and 1 fill...
                      I would add your alerts into the function, not that this will effect much. (or is there just a brace missing?)

                      the try catch statement will fill a variable with the error data when an error is thrown, I suggest using a diff var to your e var. (just in case)

                       

                      the only issue I can see is the last try statement.

                      if(items[i].strokeColor.spot.name != col.name){  // if the item it is checking here is cmyk, then it has no .spot. this will throw and error which will be caught by the catch statment.

                      // when it throws the error it will not check the items fill color. it will also throw this error before it can set the variable to 1.

                       

                      give this a shot.

                       

                      DieLine();    
                        
                      //checking other than Dieline swatch  
                      function DieLine(){    
                          var e = 0;    
                          var err=0;  
                          var doc = app.activeDocument;    
                          var items = doc.activeLayer.pathItems;    
                          doc.selection = null;    
                          try{    
                              var col = doc.swatches.getByName('Die');    
                          }catch(error){    
                              alert('No swatched named "Die" in this document');    
                              return;    
                          }    
                          for(var i=0; i<items.length; i++){    
                              try{  
                                  if(items[i].strokeColor.spot.name == col.name){    
                                   e++;              
                                  }  
                              }catch(error){} 
                              try{
                                  if(items[i].fillColor.spot.name == col.name){    
                                      err++;              
                                  }
                              }catch(error){} 
                          }
                              var Smessage = (e<items.length?items.length-e + " Objects contain other stroke colors":"Objects contain only 'Die' stroke colors");
                              var Fmessage = (err<items.length?items.length-err + " Objects contain other fillcolors":"Objects contain only 'Die' fill colors");
                              alert(Smessage + "\n" + Fmessage);
                      }
                      
                      • 8. Re: Please help me with object color identification
                        tpk1982 Level 4

                        HI Qwertyfly,

                         

                        You simply AWESOME. Yes i got that error message for fill. So i convert my 'die' swatch to spot before i run the script. Now it is not necessary with your coding.

                         

                        THANK YOU SO MUCH.

                         

                        I learnt lot from you.

                         

                        Regards,

                        Karthi

                        • 9. Re: Please help me with object color identification
                          Qwertyfly... Level 4

                          glad it helped.

                          hope you get good use from the script...