6 Replies Latest reply on Jan 5, 2015 7:10 AM by williamadowling

    Ignore patterns and process swatches when pushing to array

    williamadowling Level 4

      How do i go about ignoring process colors and pattern swatches when pushing swatches to an array? So far i've been able to exclude swatches by name, but ColorType.CMYK doesn't seem to do it... Here's the snippet in question..

       

      //Get Document Swatches 
      
      
      var swatchList = docRef.swatches;
      
      
      var colorList = [];
      
      
      
      
      for(var k=0; k< swatchList.length; k++) {
          if (swatchList[k].name.toLowerCase() != "thru-cut" && swatchList[k].name.toLowerCase() != "[registration]"// 
          && swatchList[k].name.toLowerCase() != "[none]" && swatchList[k].name.toLowerCase() != "cut line" //
          && swatchList[k] != ColorType.CMYK){
             
             colorList.push(swatchList[k].name);
                 }
             }
      

       

      am i way off on the syntax for ColorType? While i'm at it.. is there a way to clean up this list to make things easier to add/remove in the future. ideally i think i'd have an array that just includes the swatches to ignore.. something like this:

       

      var ignoredSwatches = ["thru-cut", "[registration]"]
      
      for (var k=0; k<swatchList.length; k++) {
           if swatchList[k].name.toLowerCase() != ignoredSwatches {
                colorList.push(swatchList[k].name);
                }
           }
      

       

       

      Thanks in advance, folks.

        • 1. Re: Ignore patterns and process swatches when pushing to array
          williamadowling Level 4

          ok. i figured out the syntax to ignore certain color types which is this:

           

          &&  swatchList[k].color.typename != "CMYKColor"
          
          

           

          the syntax is the same for pattern or RGB colors as well. so that problem is solved.

           

          anyone have any idea about creating a color list via an array instead of including the variable[index].name.toLowerCase() every time?

           

          the way i have it works just fine, but i can't imagine it's the correct way.

          • 2. Re: Re: Ignore patterns and process swatches when pushing to array
            pixxxel schubser Level 5

            Ok williamadowling, sometimes it's difficult to answer without to seeing an example document.

             

            But normally the color[None] is the first and the color[Registration] is the second swatch (in all document without manually changing). And if your colors "thru cut" and "cut-line" are spot colors, you can easily do something like that (without any additional if-clauses):

            var swatchList = app.activeDocument.swatches;
            var colorList = [];
            for(var k=2; k< swatchList.length; k++) {    // k=2 begins at the third swatch in every "normal" document
                if(swatchList[k].color.typename === "CMYKColor") {
                    colorList.push(swatchList[k].name);
                    }
                }
            $.writeln(colorList.join("\r"));
            

             

            Have fun

             

            • 3. Re: Re: Ignore patterns and process swatches when pushing to array
              williamadowling Level 4

              i try to avoid things like starting at the 3rd swatch as you suggest. because it is possible to re-order the swatches which could render that method ineffective. i'd like to cycle through every swatch regardless of its index position and ensure that it does not match one of the instances in the "ignore list".

               

              additionally, i decided i do not want to actually ignore the CMKY swatches (or use only spot colors swatches). i wanted those to be added to the list. what i did then was add a second array and pushed CMKY swatches and other non-ideal swatches into a separate array and scripted an alert with the contents of that array. it's important that my production artists get some notification that there are unwanted colors in the document, especially because illustrator doesn't have a functioning preflight tool like indesign does.

               

              i don't necessarily want to ignore all non-spot swatches. I only want to ignore "none, registration, thru-cut" etc. These are specific swatches that i definitely do not want to add to the colorList[array] because they can interfere with my production processes.

               

              i was just curious if there was a way to make it cleaner than saying:

               

              if (it's != this && it's != this && it's != this){

                   push

              }

               

               

              i wanted something more like:

               

              var notThis = ["color", "color", "color"]

               

              if (it's != notThis){

                   push

              }

               

              i was just hoping to make the notThis list easier to update/curate in the future and avoid adding all the extra syntax of getting the lowercase name of each unwanted swatch.

              • 4. Re: Ignore patterns and process swatches when pushing to array
                Silly-V Adobe Community Professional

                Hey, I dug up this from a script I did a long time ago:

                 

                                for(var s=0; s<swcs.length; s++){

                                    var thisSw=swcs[s];

                                    if(thisSw.color!="[PatternColor]" && thisSw.color!="[GradientColor]"

                                        && thisSw.name!="[Registration]" && thisSw.name!="[None]"){

                                        //stuff

                                    }

                                }

                 

                So it looks like, swatch.color can be matched up against strings "[PatternColor]" or "[GradientColor]", otherwise, you can use swatch.name to select against Registration and None.

                • 5. Re: Ignore patterns and process swatches when pushing to array
                  Silly-V Adobe Community Professional

                  What you have there in a need for an in_array function of some kind, you can swipe something from here: JavaScript equivalent of PHP's in_array() - Stack Overflow
                  Then, there will be a need to still differentiate the swatches with .color and .name 

                  • 6. Re: Ignore patterns and process swatches when pushing to array
                    williamadowling Level 4

                    Thanks Silly,

                     

                    i think i understand what you're saying.. and i think i'll leave the code the way i have it now. it works just fine as it is, but i was basically looking for a way that i could copy and paste an entire list of color names if i needed to without worrying about all the extra syntax. but i think it's unnecessary.

                     

                    i appreciate the contribution though. = )