• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Replacing any black with swatch black

Community Beginner ,
Oct 12, 2017 Oct 12, 2017

Copy link to clipboard

Copied

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.fillColor = blk)

               { 

               item.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.black=100)
               { 
               item.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! )

TOPICS
Scripting

Views

574

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Oct 15, 2017 Oct 15, 2017

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);

 

...

Votes

Translate

Translate
Adobe
Participant ,
Oct 15, 2017 Oct 15, 2017

Copy link to clipboard

Copied

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;

       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;

      path.fillColor = spot;

    }

  }

} catch (e) {

  alert(e);

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 16, 2017 Oct 16, 2017

Copy link to clipboard

Copied

LATEST

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.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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines