2 Replies Latest reply on Aug 26, 2013 2:06 PM by CarlosCanto

    SpotColor to GrayColor converter (javaScript)

    elDudereno Level 3

      This could be edited to change any spot color to grayscale, currently it is aimed at a color called "spot black". The reason this is better then just using "edit/edit colors/convert to grayscale" is that the tones stay the same value. I'm sure it could be improved, I'm just sharing it so if someone is having the issues I was having they might have an easier time fixing it. The script originated with CarlosCanto on this discussion http://forums.adobe.com/thread/1267562?tstart=0 and was discussed further in this discussion http://forums.adobe.com/message/5610447#5610447. Enjoy.

      // script.name = grayscaleFromSpotBlack.jsx; 
      // script.description = changes art color from a Spot swatch named "spot black", to Grayscale tones;
      // script.requirements = an opened document;
      // script.parent = CarlosCanto // 08/08/13;
      // script.elegant = false;
      
      // reference http://forums.adobe.com/thread/1267562?tstart=0 && http://forums.adobe.com/message/5610447#5610447
      
      // Note: color values get rounded to the nearest integer, to avoid values like black = 99.99999999999999
      //              Hidden and/or Locked Objects will be ignored, as well as objects in Hidden and/or Locked Layers
      
      #target Illustrator
      
      var idoc = app.activeDocument;
      var pi = idoc.pathItems;
      var tf = idoc.textFrames;
      var fcounter = 0;
      var scounter = 0;
      var gcounter = 0;
      
      //apply grayscale to paths and gradient stops
      for (j=0; j<pi.length; j++) {
          var ipath = pi[j];
          if (ipath.hidden==false && ipath.locked==false) {
              if (meetTheParents(ipath) == "isLayerFree"){
                  if (isLayerFree(ipath.parent)){
                      changePath(ipath);
                  } else {
                      if (isGroupFree(ipath)){
                          changePath(ipath);
                      }
                  }
              }
          }
      }
      // choose text and change
      for (t=0; t<tf.length; t++) {
          var iTxtFrm = tf[t];
          if (iTxtFrm.locked==false && iTxtFrm.hidden==false) {
              if (meetTheParents(iTxtFrm) == "isLayerFree"){
                  if (isLayerFree(iTxtFrm.parent)){
                      changeText(iTxtFrm);
                  } else {
                      if (isGroupFree(iTxtFrm)){
                          changeText(iTxtFrm);
                      }
                  }
              }
          }
      }
      alert(fcounter + ' Fill(s), ' + scounter + ' stroke(s) & ' + gcounter + ' Gradient Stop(s) processed.');
      function meetTheParents(PageItem){
          if (PageItem.parent.typename = "Layer"){
              return "isLayerFree";
          } else if (PageItem.parent.typename = "GroupItem"){
              return "isGroupFree";
          } 
      }
      function isGroupFree(groupItem){
          if (groupItem.parent.hidden==false && groupItem.parent.editable==true){
              if (groupItem.parent.typename == "Layer"){
                  var greatgpa = groupItem.parent;
                  isLayerFree(greatgpa);
              }else if (groupItem.parent.typename = "GroupItem"){
                  var gpa = groupItem.parent;
                  if (isGroupFree(gpa));
                  return true
                  } else {
                  return false
              }
          }
      }
      function isLayerFree(layer){
          if (layer.locked == false && layer.visible == true){
              var gpa = layer.parent;
              isLayerFree(gpa);
              return true
              } else {
              return false
          }
      }
      // actually changes text
      function changeText(textFrame){
          var chars = textFrame.characters;
          for (c=0; c<chars.length; c++) {
              var ltr = chars[c];
              if (ltr.characterAttributes.fillColor.typename == "SpotColor"){
                  if (ltr.characterAttributes.fillColor.spot.name == "spot black"){
                      var fillk = Math.round(ltr.characterAttributes.fillColor.tint);
                      SpotToGrey (ltr, true, false, fillk);
                      fcounter++;
                  }
              }
              if (ltr.characterAttributes.strokeColor.typename == "SpotColor"){
                  if (ltr.characterAttributes.strokeColor.spot.name == "spot black") {
                      var strokek = Math.round(ltr.characterAttributes.strokeColor.tint);
                      SpotToGrey (ltr, false, true, strokek);
                      scounter++;
                  }
              }
          }
      };
      //actually changes paths
      function changePath(PathItem){
          var fillColor = PathItem.fillColor;
          if (fillColor.typename == "SpotColor") {
              if (fillColor.spot.name == "spot black") {
                  var fillk = Math.round(fillColor.tint);
                  SpotToGrey (PathItem, true, false, fillk);
                  fcounter++;
              }
          }
          else if (fillColor.typename == "GradientColor") {
              var stops = PathItem.fillColor.gradient.gradientStops;
              for (s=0; s<stops.length; s++) {
                  var iStop = stops[s];
                  var stopColor = iStop.color;
                  if (stopColor.typename == "SpotColor") {
                      if (stopColor.spot.name == "spot black") {
                          var stopk = Math.round(stopColor.tint);
                          iStop.color = GrayColor;
                          iStop.color.gray = stopk;
                          gcounter++;
                      }
                  }
              }
          }
          var strokeColor = PathItem.strokeColor;
          if (strokeColor.typename == "SpotColor") {
              if (strokeColor.spot.name == "spot black") {
                  var strokek = Math.round(strokeColor.tint);
                  SpotToGrey (PathItem, false, true, strokek);
                  scounter++;
              }
          }
          else if (strokeColor.typename == "GradientColor") {
              var stops = PathItem.strokeColor.gradient.gradientStops;
              for (s=0; s<stops.length; s++) {
                  var iStop = stops[s];
                  var stopColor = iStop.color;
                  if (stopColor.typename == "SpotColor") {
                      if (stopColor.spot.name == "spot black") {
                          var stopk = Math.round(stopColor.tint);
                          iStop.color = GrayColor;
                          iStop.color.gray = stopk;
                          gcounter++;
                      }
                  }
              }
          }
      }
      function SpotToGrey (path, fill, stroke, k) {
          if (fill) {
              path.fillColor = GrayColor;
              path.fillColor.gray = k;
          }
          if (stroke) {
              path.strokeColor = GrayColor;
              path.strokeColor.gray = k;
          }
      }
      

      A file that shows what this script does will be hosted here https://docs.google.com/file/d/0BzEoJSYDhH_WekNoYTFzVHVaOXM/edit?usp=sharing until I forget why it's saved there and delete it.