2 Replies Latest reply on Oct 16, 2017 8:11 AM by MFreemanRKS

    Replacing any black with swatch black

    MFreemanRKS

      I am working with a variety of files, all of which have various shapes and paths on them.  I am trying to make a script that can look at everything in the selected layer, take anything that's black (working in cmyk, so k value of 100) and make it a swatch color named "Fill", if its not black then delete it. Part of what I am doing is bringing old files into a new template file so the template will always have this swatch.

       

      I haven't gotten to the deleting part, and the little tweaks and whatnots ive tried to the script below either brings up an error "undefined is not an object" or the script will do nothing or turn everything to "fill" including things that are not black or don't even have a fill color.

       

      Any ideas?

       

      This version of the script does not seem to do anything but I feel is closer to what I need, I'm just missing something.

      var doc = app.activeDocument;  

      var layer = app.activeDocument.activeLayer;

       

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

      var item = doc.pathItems;  

      var blk = new CMYKColor ("0,0,0,100")

      if (item[i].fillColor = blk)

                     { 

                     item[i].fillColor = doc.swatches["Fill"].color; 

                     }; 

         };

       

      This version just makes everything "fill" but I assume that's because I'm using "page item" instead of "path item"

      var doc = app.activeDocument;  
      var item = doc.pageItems;   
      for(var i=0; i<item.length; i++){ 

      if (item[i].black=100)
                     { 
                     item[i].fillColor = doc.swatches["Fill"].color; 
                     }; 
         };

       

      Thanks! 

       

      (I'm pretty new to all this script stuff, so any help on the delete things that aren't black part would be amazing too! )

        • 1. Re: Replacing any black with swatch black
          itsBenMason Level 2

          Hellos,

          Below is a script that should do as you have asked including the removing of paths that are not black.

          Read through and use to learn from.

           

          var doc = activeDocument;
          
          
          /*
           * Methods for colors
           */
          
          
          function colorExists(name) {
           try {
             var swatch = doc.spots.getByName(name);
             return swatch !== undefined;
           } catch (e) {
             return false;
           }
          }
          
          
          function getSpotColor(name, c, m, y, k) {
           if (colorExists(name)) {
             return doc.spots.getByName(name).color;
           } else {
             var cmykColor = createCMYKColor(c, m, y, k);
          
          
             var spot = doc.spots.add();
             spot.name = name;
             spot.colorType = ColorModel.SPOT;
             spot.color = cmykColor;
          
          
             var color = new SpotColor();
             color.spot = spot;
          
          
             return color;
           }
          }
          
          
          function createCMYKColor(c, m, y, k) {
           var cmykColor = new CMYKColor();
           cmykColor.cyan = c;
           cmykColor.magenta = m;
           cmykColor.yellow = y;
           cmykColor.black = k;
          
          
           return cmykColor;
          }
          
          
          function isBlack(path) {
             if (path.filled) {
               var color = path.fillColor;
               if (color.typename == "CMYKColor") {
                 return color.cyan == 0 && color.magenta == 0 && color.yellow == 0 && color.black == 100;
               }
             }
             return false;
          }
          
          
          /*
           * Getting black path method
           */
          
          
           function getPaths() {
             var paths = [];
          
          
             var documentPaths = doc.pathItems;
             if (documentPaths.length > 0) {
               for (var i = documentPaths.length - 1; i >= 0; i--) {
                 var path = documentPaths[i];
                 if (isBlack(path)) {
                   paths.push(path);
                 } else {
                   path.remove();
                 }
               }
             }
          
          
             return paths;
           }
          
          
           /*
            * Run script
            */
          
          try {
            var paths = getPaths();
            if (paths.length > 0) {
              var spot = getSpotColor("Fill", 0, 0, 0, 100);
              for (var i = 0; i < paths.length; i++) {
                var path = paths[i];
                path.fillColor = spot;
              }
            }
          } catch (e) {
            alert(e);
          }
          
          
          • 2. Re: Replacing any black with swatch black
            MFreemanRKS Level 1

            I have two demo files.  In both demo files it does delete everything that is not black, however it is not applying the swatch color that it creates.  Since I was getting this to work in my 2nd example listed in the problem I compared what you were doing to make things fill and what I was doing.

             

            I combined lines 99 and 100 to make this line:

                      paths[i].fillColor = doc.swatches["Fill"].color;

             

            seems to be working fine from there.

             

            THANK YOU SO MUCH!  I have been trying to figure this out for multiple days.