24 Replies Latest reply on Jan 13, 2014 5:01 AM by elDudereno

    Error 21 (Changing color on text)

    elDudereno Level 3

      http://forums.adobe.com/message/5580527#5580527

       

      Carlos wrote a script in response to the above thread. It changes CMYK black, and grayscale black to a swatch called "spot black". It's awesome, but it's only aimed at paths. I've made some adjustments to prevent it from effecting white and unpainted objects. I'm going to need it to work on text and gradients. Right now I am working on text.

       

      I've tried to use the same logic that works on the paths on text, but I'm missing something. Help help.

      Screen Shot 2013-08-13 at 9.39.43 AM.png

       

      This is the latest working version, it does not effect text or gradients.

      // script.name = cmykBlackNgrayscaleToSpotBlack.jsx; 
      // script.description = changes art color from standard CMYK Black and Grayscale tones to an EXISTING Spot swatch named "spot black";
      // script.requirements = an opened document;
      // script.parent = CarlosCanto // 08/08/13;
      // script.elegant = false;
      
      // reference http://forums.adobe.com/thread/1267562?tstart=0
      
      // 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 sw = idoc.swatches["spot black"];
      var fcounter = 0;
      var scounter = 0;
      
      for (j=0; j<pi.length; j++) {
          var ipath = pi[j];
          if (ipath.layer.visible==true && ipath.layer.locked==false && ipath.hidden==false && ipath.locked==false) {
              var fillColor = ipath.fillColor;
              if (fillColor.typename == "CMYKColor") {
                  if (isColorBlack (fillColor)) {
                      var fillk = Math.round(fillColor.black);
                      cmykBlackToSpot (ipath, true, false, fillk);
                      fcounter++;
                  }
              }
              else if (fillColor.typename == "GrayColor") {
                  if (grayNotWhiteOrClear (fillColor)) {
                      var fillk = Math.round(fillColor.gray);
                      cmykBlackToSpot (ipath, true, false, fillk);
                      fcounter++;
                  }
              }
      
              var strokeColor = ipath.strokeColor;
              if (strokeColor.typename == "CMYKColor") {
                  if (isColorBlack (strokeColor)) {
                      var strokek = Math.round(strokeColor.black);
                      cmykBlackToSpot (ipath, false, true, strokek);
                      scounter++;
                  }
              }
              else if (strokeColor.typename == "GrayColor") {
                  if (grayNotWhiteOrClear (strokeColor)) {
                      var strokek = Math.round(strokeColor.gray);
                      cmykBlackToSpot (ipath, false, true, strokek);
                      scounter++;
                  }
              }
          }
      }
      alert(fcounter + ' Fill(s) & ' + scounter + ' stroke(s) processed');
      
      function cmykBlackToSpot (path, fill, stroke, k) {
          if (fill) {
              path.fillColor = sw.color;
              path.fillColor.tint = k;
          }
          if (stroke) {
              path.strokeColor = sw.color;
              path.strokeColor.tint = k;
          }
      }
      
      function isColorBlack (cmykColor) {
          var c = Math.round(cmykColor.cyan);
          var m = Math.round(cmykColor.magenta);
          var y = Math.round(cmykColor.yellow);
          var k = Math.round(cmykColor.black);
      
          if (c==0 && m==0 && y==0 && k != 0)
              return true
          else
              return false
      }
      function grayNotWhiteOrClear (GrayColor) {
          var pct = Math.round(GrayColor.gray);
      
          if (pct != 0)
              return true
          else
              return false
      }
      

       

      This is the version I'm working on now where I'm trying to include the text.

       

      // script.name = cmykBlackNgrayscaleToSpotBlack.jsx; 
      // script.description = changes art color from standard CMYK Black and Grayscale tones to an EXISTING Spot swatch named "spot black";
      // script.requirements = an opened document;
      // script.parent = CarlosCanto // 08/08/13;
      // script.elegant = false;
      
      // reference http://forums.adobe.com/thread/1267562?tstart=0
      
      // 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 sw = idoc.swatches["spot black"];
      var ch = idoc.textFrames[0].characters[0];
      var fcounter = 0;
      var scounter = 0;
      
      for (j=0; j<pi.length; j++) {
          var ipath = pi[j];
          if (ipath.layer.visible==true && ipath.layer.locked==false && ipath.hidden==false && ipath.locked==false) {
              var fillColor = ipath.fillColor;
              if (fillColor.typename == "CMYKColor") {
                  if (isColorBlack (fillColor)) {
                      var fillk = Math.round(fillColor.black);
                      cmykBlackToSpot (ipath, true, false, fillk);
                      fcounter++;
                  }
              }
              else if (fillColor.typename == "GrayColor") {
                  if (grayNotWhiteOrClear (fillColor)) {
                      var fillk = Math.round(fillColor.gray);
                      cmykBlackToSpot (ipath, true, false, fillk);
                      fcounter++;
                  }
              }
      
              var strokeColor = ipath.strokeColor;
              if (strokeColor.typename == "CMYKColor") {
                  if (isColorBlack (strokeColor)) {
                      var strokek = Math.round(strokeColor.black);
                      cmykBlackToSpot (ipath, false, true, strokek);
                      scounter++;
                  }
              }
              else if (strokeColor.typename == "GrayColor") {
                  if (grayNotWhiteOrClear (strokeColor)) {
                      var strokek = Math.round(strokeColor.gray);
                      cmykBlackToSpot (ipath, false, true, strokek);
                      scounter++;
                  }
              }
          }
      }
      
      for (t=0; t<ch.length; t++) {
          var txt = ch[t];
          if (txt.layer.visible==true && txt.layer.locked==false && txt.hidden==false && txt.locked==false) {
              var fillColor = txt.fillColor;
              if (fillColor.typename == "CMYKColor") {
                  if (isColorBlack (fillColor)) {
                      var fillk = Math.round(fillColor.black);
                      cmykBlackToSpot (txt, true, false, fillk);
                      fcounter++;
                  }
              }
              else if (fillColor.typename == "GrayColor") {
                  if (grayNotWhiteOrClear (fillColor)) {
                      var fillk = Math.round(fillColor.gray);
                      cmykBlackToSpot (txt, true, false, fillk);
                      fcounter++;
                  }
              }
      
              var strokeColor = txt.strokeColor;
              if (strokeColor.typename == "CMYKColor") {
                  if (isColorBlack (strokeColor)) {
                      var strokek = Math.round(strokeColor.black);
                      cmykBlackToSpot (txt, false, true, strokek);
                      scounter++;
                  }
              }
              else if (strokeColor.typename == "GrayColor") {
                  if (grayNotWhiteOrClear (strokeColor)) {
                      var strokek = Math.round(strokeColor.gray);
                      cmykBlackToSpot (txt, false, true, strokek);
                      scounter++;
                  }
              }
          }
      }
      alert(fcounter + ' Fill(s) & ' + scounter + ' stroke(s) processed');
      
      function cmykBlackToSpot (path, fill, stroke, k) {
          if (fill) {
              path.fillColor = sw.color;
              path.fillColor.tint = k;
          }
          if (stroke) {
              path.strokeColor = sw.color;
              path.strokeColor.tint = k;
          }
      }
      
      function isColorBlack (cmykColor) {
          var c = Math.round(cmykColor.cyan);
          var m = Math.round(cmykColor.magenta);
          var y = Math.round(cmykColor.yellow);
          var k = Math.round(cmykColor.black);
      
          if (c==0 && m==0 && y==0 && k != 0)
              return true
          else
              return false
      }
      function grayNotWhiteOrClear (GrayColor) {
          var pct = Math.round(GrayColor.gray);
      
          if (pct != 0)
              return true
          else
              return false
      }
      

       

      Here is a test file

      https://docs.google.com/file/d/0BzEoJSYDhH_WdENjc092SF9GN0U/edit?usp=sharing

      Thanks for playing.

        • 1. Re: Error 21 (Changing color on text)
          Muppet Mark Level 5

          I doubt you are going to be able to utilize the same syntax for your differing items… Anyhows…

           

          var pi = idoc.pathItems; // Carlos sets variable to a collection…

           

          var ch = idoc.textFrames[0].characters[0]; // Here your variable is to a single character… I doubt this can be tested for locked or hidden…

           

          var ch = idoc.textFrames; // Will give you something to loop thru but you will need to dig deeper still as every character can be different…

           

          Personally I would go for stories when looking thru text…

          • 2. Re: Error 21 (Changing color on text)
            elDudereno Level 3

            So...

            var ch = idoc.stories[0].textRanges[0].CharacterAttributes;

             

            I'm looking in "Adobe IllustratorCS5 Scripting Reference: JavaScript" on page 226 (textRange) and I'm not seeing locked, hidden, or layers as textRange properties. So I'm guessing I can't test for them for stories either.

             

            My scripting powers mostly consist of looking at an example, deciphering it, and applying what I need to make it work in my instance. Where could I go to find an example of a script that changes the fill and stroke of cmyk and grayscale text that isn't locked or hidden?

             

            I'm pretty ignorant at all of this, so please bear with me.

             

             

            P.S. If I didn't care if locked or hidden text was changed could I use idoc.textFrames[0].characters[0];  ?

            • 3. Re: Error 21 (Changing color on text)
              CarlosCanto Adobe Community Professional & MVP

              if you don't care checking for Locked or Hidden, then you can skip that, only you'll get an error if a text frame is locked or hidden.

               

              in short you need to loop through all text frames and within each, you have to loop through all characters, once you are at the character level, you can get its characterAttributes property and finally check for fillColor/strokeColor

               

              if you do want to check for lock/hidden then you do that at the textFrame level

               

              ...note that textFrames don't have "layer" property...so, it complicates things, to determine if the layer it belongs to is locked or hidden, you have to check its parent...problem is, it can have parent, grand parent, great grand parent...

              1 person found this helpful
              • 4. Re: Error 21 (Changing color on text)
                elDudereno Level 3

                The good news is I'm testing for the "spot black" swatch, and creating it if necessary now. So that's good. The other good news is that this script doesn't throw any errors. The bad news is that it doesn't do a darn thing to the text. Please tell me why I'm wrong. (I only wrote far enough to effect the fills of cmyk black in order to test.)

                 

                My theory was that if I couldn't test the text for locked layers, I would test the layers for being locked and then test for text to change.

                 

                // script.name = cmykBlackNgrayscaleToSpotBlack.jsx; 
                // script.description = changes art color from standard CMYK Black and Grayscale tones to a Spot swatch named "spot black";
                // script.requirements = an opened document;
                // script.parent = CarlosCanto // 08/08/13;
                // script.elegant = false;
                
                // reference http://forums.adobe.com/thread/1267562?tstart=0
                
                // 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 la = idoc.layers;
                createSpotBlack();
                var sw = idoc.swatches["spot black"];
                var fcounter = 0;
                var scounter = 0;
                
                //apply spot to paths
                for (j=0; j<pi.length; j++) {
                    var ipath = pi[j];
                    if (ipath.layer.visible==true && ipath.layer.locked==false && ipath.hidden==false && ipath.locked==false) {
                        var fillColor = ipath.fillColor;
                        if (fillColor.typename == "CMYKColor") {
                            if (isColorBlack (fillColor)) {
                                var fillk = Math.round(fillColor.black);
                                cmykBlackToSpot (ipath, true, false, fillk);
                                fcounter++;
                            }
                        }
                        else if (fillColor.typename == "GrayColor") {
                            if (grayNotWhiteOrClear (fillColor)) {
                                var fillk = Math.round(fillColor.gray);
                                cmykBlackToSpot (ipath, true, false, fillk);
                                fcounter++;
                            }
                        }
                    
                        var strokeColor = ipath.strokeColor;
                        if (strokeColor.typename == "CMYKColor") {
                            if (isColorBlack (strokeColor)) {
                                var strokek = Math.round(strokeColor.black);
                                cmykBlackToSpot (ipath, false, true, strokek);
                                scounter++;
                            }
                        }
                        else if (strokeColor.typename == "GrayColor") {
                            if (grayNotWhiteOrClear (strokeColor)) {
                                var strokek = Math.round(strokeColor.gray);
                                cmykBlackToSpot (ipath, false, true, strokek);
                                scounter++;
                            }
                        }
                    }
                }
                
                //check through layers for text
                for (l=0; l<la.length; l++){
                    var lyr=la[l];
                    if (lyr.visible==true && lyr.locked==false && lyr.hidden==false) {
                        var lyrtxt = lyr.textFrames; {
                        for (lt=0; lt<lyrtxt.length; lt++)
                            if (lyrtxt.visible==true && lyrtxt.locked==false && lyrtxt.hidden==false) {
                            var chars=lyrtxt.characters;
                            for (c=0; c<chars.length; c++) {
                                if (chars.characterAttributes.fillColor == "CMYKColor"){
                                    if (isColorBlack (fillColor)) {
                                        var fillk = Math.round(fillColor.black);
                                        cmykBlackToSpot (chars, true, false, fillk);
                                        fcounter++;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                
                alert(fcounter + ' Fill(s) & ' + scounter + ' stroke(s) processed');
                
                function cmykBlackToSpot (path, fill, stroke, k) {
                    if (fill) {
                        path.fillColor = sw.color;
                        path.fillColor.tint = k;
                    }
                    if (stroke) {
                        path.strokeColor = sw.color;
                        path.strokeColor.tint = k;
                    }
                }
                
                function isColorBlack (cmykColor) {
                    var c = Math.round(cmykColor.cyan);
                    var m = Math.round(cmykColor.magenta);
                    var y = Math.round(cmykColor.yellow);
                    var k = Math.round(cmykColor.black);
                    
                    if (c==0 && m==0 && y==0 && k != 0)
                        return true
                    else
                        return false
                }
                function grayNotWhiteOrClear (GrayColor) {
                    var pct = Math.round(GrayColor.gray);
                
                    if (pct != 0)
                        return true
                    else
                        return false
                }
                function createSpotBlack(){
                    if (!doesColorExist("spot black")){
                        // Create CMYKColor
                        var cmykColor = new CMYKColor();
                        cmykColor.cyan = 9;
                        cmykColor.magenta = 9;
                        cmykColor.yellow = 9;
                        cmykColor.black = 96;
                        // Create Spot
                        var spot = idoc.spots.add();
                        spot.color = cmykColor;
                        spot.colorType = ColorModel.SPOT;
                        spot.name = "spot black";
                    }
                }
                function doesColorExist(){
                    var clrs = idoc.swatches;
                    for (var i=0;i<clrs.length;i++){
                        if (clrs[i].name=="spot black"){
                            return true;
                        }
                    }
                    return false;
                }
                
                • 5. Re: Error 21 (Changing color on text)
                  elDudereno Level 3

                  http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/pdf/illustrator/scrip ting/CC/Illustrator%20Scripting%20Guide.pdf

                   

                  Page 20 has this diagram

                  Screen Shot 2013-08-16 at 8.55.15 AM.png

                  Does this mean my method won't work because app.activeDocument.layers does not contain the text frames?

                  • 6. Re: Error 21 (Changing color on text)
                    elDudereno Level 3

                    This is another way to do it wrong. Again, I am getting no errors out of this code, just text that is unchanged.

                    // script.name = cmykBlackNgrayscaleToSpotBlack.jsx; 
                    // script.description = changes art color from standard CMYK Black and Grayscale tones to a Spot swatch named "spot black";
                    // script.requirements = an opened document;
                    // script.parent = CarlosCanto // 08/08/13;
                    // script.elegant = false;
                    
                    // reference http://forums.adobe.com/thread/1267562?tstart=0
                    
                    // 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;
                    createSpotBlack();
                    var sw = idoc.swatches["spot black"];
                    var fcounter = 0;
                    var scounter = 0;
                    
                    for (t=0; t<tf.length; t++)
                        var itxt = tf[t];
                        if (itxt.visible==true && itxt.locked==false && itxt.hidden==false) {
                        var chars = itxt.contents.characters;
                        for (c=0; c<chars.length; c++) {
                            var iChar = chars[c];
                            if (iChar.characterAttributes.fillColor == "CMYKColor"){
                                if (isColorBlack (fillColor)) {
                                    var fillk = Math.round(fillColor.black);
                                    cmykBlackToSpot (iChar, true, false, fillk);
                                    fcounter++;
                                }
                            }
                        }
                    }
                    
                    //apply spot to paths
                    for (j=0; j<pi.length; j++) {
                        var ipath = pi[j];
                        if (ipath.layer.visible==true && ipath.layer.locked==false && ipath.hidden==false && ipath.locked==false) {
                            var fillColor = ipath.fillColor;
                            if (fillColor.typename == "CMYKColor") {
                                if (isColorBlack (fillColor)) {
                                    var fillk = Math.round(fillColor.black);
                                    cmykBlackToSpot (ipath, true, false, fillk);
                                    fcounter++;
                                }
                            }
                            else if (fillColor.typename == "GrayColor") {
                                if (grayNotWhiteOrClear (fillColor)) {
                                    var fillk = Math.round(fillColor.gray);
                                    cmykBlackToSpot (ipath, true, false, fillk);
                                    fcounter++;
                                }
                            }
                        
                            var strokeColor = ipath.strokeColor;
                            if (strokeColor.typename == "CMYKColor") {
                                if (isColorBlack (strokeColor)) {
                                    var strokek = Math.round(strokeColor.black);
                                    cmykBlackToSpot (ipath, false, true, strokek);
                                    scounter++;
                                }
                            }
                            else if (strokeColor.typename == "GrayColor") {
                                if (grayNotWhiteOrClear (strokeColor)) {
                                    var strokek = Math.round(strokeColor.gray);
                                    cmykBlackToSpot (ipath, false, true, strokek);
                                    scounter++;
                                }
                            }
                        }
                    }
                    
                    alert(fcounter + ' Fill(s) & ' + scounter + ' stroke(s) processed');
                    
                    function cmykBlackToSpot (path, fill, stroke, k) {
                        if (fill) {
                            path.fillColor = sw.color;
                            path.fillColor.tint = k;
                        }
                        if (stroke) {
                            path.strokeColor = sw.color;
                            path.strokeColor.tint = k;
                        }
                    }
                    
                    function isColorBlack (cmykColor) {
                        var c = Math.round(cmykColor.cyan);
                        var m = Math.round(cmykColor.magenta);
                        var y = Math.round(cmykColor.yellow);
                        var k = Math.round(cmykColor.black);
                        
                        if (c==0 && m==0 && y==0 && k != 0)
                            return true
                        else
                            return false
                    }
                    function grayNotWhiteOrClear (GrayColor) {
                        var pct = Math.round(GrayColor.gray);
                    
                        if (pct != 0)
                            return true
                        else
                            return false
                    }
                    function createSpotBlack(){
                        if (!doesColorExist("spot black")){
                            // Create CMYKColor
                            var cmykColor = new CMYKColor();
                            cmykColor.cyan = 9;
                            cmykColor.magenta = 9;
                            cmykColor.yellow = 9;
                            cmykColor.black = 96;
                            // Create Spot
                            var spot = idoc.spots.add();
                            spot.color = cmykColor;
                            spot.colorType = ColorModel.SPOT;
                            spot.name = "spot black";
                        }
                    }
                    function doesColorExist(){
                        var clrs = idoc.swatches;
                        for (i=0;i<clrs.length;i++){
                            if (clrs[i].name=="spot black"){
                                return true;
                            }
                        }
                        return false;
                    }
                    redraw();
                    
                    • 7. Re: Error 21 (Changing color on text)
                      CarlosCanto Adobe Community Professional & MVP

                      Hi, I don't have a chance to try your code at the moment but

                       

                      change this

                      var chars = itxt.contents.characters;

                       

                      to this

                      var chars = itxt.characters;

                      1 person found this helpful
                      • 8. Re: Error 21 (Changing color on text)
                        CarlosCanto Adobe Community Professional & MVP

                        the change above is for the script in post # 6.

                        • 9. Re: Error 21 (Changing color on text)
                          elDudereno Level 3

                          Alright. I'm about to clock out for the weekend. I used a series of alerts to see how far through the script I was making it, deleting them as I progressed. As it stands I am down to an individual character will a fillColor.typename of CMYKColor. The alert tells me that the mix is 0.0.0.0 (which is odd because it should be 0.0.0.100) and then it fails on my isTextBlack function var c. The reason I am using that new function instead of the isColorBlack function is that it was failing on var c, and I knew that function was good because it had worked earlier with the pathItems.

                           

                          I am pretty sure this isn't working out because I don't understand the "textRange" thing, how it works how to call it etc.

                          http://forums.adobe.com/message/3623326#3623326 <- this discussion shows an example of it but I haven't been able to decipher it.

                           

                          My theory is that isColorBlack would still work if I was using the textRange instead of the ichar.

                           

                          Here is the script as it stands.

                           

                          // script.name = cmykBlackNgrayscaleToSpotBlack.jsx; 
                          // script.description = changes art color from standard CMYK Black and Grayscale tones to a Spot swatch named "spot black";
                          // script.requirements = an opened document;
                          // script.parent = CarlosCanto // 08/08/13;
                          // script.elegant = false;
                          
                          // reference http://forums.adobe.com/thread/1267562?tstart=0
                          
                          // 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;
                          createSpotBlack();
                          var sw = idoc.swatches["spot black"];
                          var fcounter = 0;
                          var scounter = 0;
                          
                          for (t=0; t<tf.length; t++) {
                              var itxt = tf[t];
                              if (itxt.hidden==false && itxt.locked==false && itxt.hidden==false) {
                                  var chars = itxt.characters;
                                  for (c=0; c<chars.length; c++) {
                                      var iChar = chars[c];
                                      if (iChar.characterAttributes.fillColor.typename == "CMYKColor") {
                                          alert (CMYKColor.cyan +" "+ CMYKColor.magenta +" "+ CMYKColor.yellow +" "+ CMYKColor.black);
                                          if (isTextBlack (fillColor)) {
                                              var fillk = Math.round(fillColor.black);
                                              cmykBlackToSpot (iChar, true, false, fillk);
                                              alert ("I win");
                                              fcounter++;
                                          }
                                      }
                                  }
                              }
                          }
                          
                          //apply spot to paths
                          for (j=0; j<pi.length; j++) {
                              var ipath = pi[j];
                              if (ipath.layer.visible==true && ipath.layer.locked==false && ipath.hidden==false && ipath.locked==false) {
                                  var fillColor = ipath.fillColor;
                                  if (fillColor.typename == "CMYKColor") {
                                      if (isColorBlack (fillColor)) {
                                          var fillk = Math.round(fillColor.black);
                                          cmykBlackToSpot (ipath, true, false, fillk);
                                          fcounter++;
                                      }
                                  }
                                  else if (fillColor.typename == "GrayColor") {
                                      if (grayNotWhiteOrClear (fillColor)) {
                                          var fillk = Math.round(fillColor.gray);
                                          cmykBlackToSpot (ipath, true, false, fillk);
                                          fcounter++;
                                      }
                                  }
                          
                                  var strokeColor = ipath.strokeColor;
                                  if (strokeColor.typename == "CMYKColor") {
                                      if (isColorBlack (strokeColor)) {
                                          var strokek = Math.round(strokeColor.black);
                                          cmykBlackToSpot (ipath, false, true, strokek);
                                          scounter++;
                                      }
                                  }
                                  else if (strokeColor.typename == "GrayColor") {
                                      if (grayNotWhiteOrClear (strokeColor)) {
                                          var strokek = Math.round(strokeColor.gray);
                                          cmykBlackToSpot (ipath, false, true, strokek);
                                          scounter++;
                                      }
                                  }
                              }
                          }
                          
                          alert(fcounter + ' Fill(s) & ' + scounter + ' stroke(s) processed');
                          
                          function cmykBlackToSpot (path, fill, stroke, k) {
                              if (fill) {
                                  path.fillColor = sw.color;
                                  path.fillColor.tint = k;
                              }
                              if (stroke) {
                                  path.strokeColor = sw.color;
                                  path.strokeColor.tint = k;
                              }
                          }
                          function isTextBlack (cmykColor) {
                              var c = Math.round(cmykColor.cyan);
                              var m = Math.round(cmykColor.magenta);
                              var y = Math.round(cmykColor.yellow);
                              var k = Math.round(cmykColor.black);
                          
                              if (c==0 && m==0 && y==0 && k !=0)
                                  return true
                              else
                                  return false
                          }
                          function isColorBlack (cmykColor) {
                              var c = Math.round(cmykColor.cyan);
                              var m = Math.round(cmykColor.magenta);
                              var y = Math.round(cmykColor.yellow);
                              var k = Math.round(cmykColor.black);
                          
                              if (c==0 && m==0 && y==0 && k !=0)
                                  return true
                              else
                                  return false
                          }
                          function grayNotWhiteOrClear (GrayColor) {
                              var pct = Math.round(GrayColor.gray);
                          
                              if (pct != 0)
                                  return true
                              else
                                  return false
                          }
                          function createSpotBlack(){
                              if (!doesColorExist("spot black")){
                                  // Create CMYKColor
                                  var cmykColor = new CMYKColor();
                                  cmykColor.cyan = 9;
                                  cmykColor.magenta = 9;
                                  cmykColor.yellow = 9;
                                  cmykColor.black = 96;
                                  // Create Spot
                                  var spot = idoc.spots.add();
                                  spot.color = cmykColor;
                                  spot.colorType = ColorModel.SPOT;
                                  spot.name = "spot black";
                              }
                          }
                          function doesColorExist(){
                              var clrs = idoc.swatches;
                              for (i=0;i<clrs.length;i++){
                                  if (clrs[i].name=="spot black"){
                                      return true;
                                  }
                              }
                              return false;
                          }
                          redraw();
                          

                          I'm also curious why the if/else in doesColorExist uses a different syntax then in isColorBlack. I tried to make them both like isColorBlack and it said my use of "else" was illegal.

                           

                          As always, thanks so much for any advice.

                          • 10. Re: Error 21 (Changing color on text)
                            elDudereno Level 3

                            Update:

                            I got changing the color of gradient stops figured out. Still working on text.

                             

                            // script.name = cmykBlackNgrayscaleToSpotBlack.jsx; 
                            // script.description = changes art color from standard CMYK Black and Grayscale tones to a Spot swatch named "spot black";
                            // script.requirements = an opened document;
                            // script.parent = CarlosCanto // 08/08/13;
                            // script.elegant = false;
                            
                            // reference http://forums.adobe.com/thread/1267562?tstart=0
                            
                            // 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;
                            createSpotBlack();
                            var sw = idoc.swatches["spot black"];
                            var fcounter = 0;
                            var scounter = 0;
                            var gcounter = 0;
                            
                            //apply spot to paths
                            for (j=0; j<pi.length; j++) {
                                var ipath = pi[j];
                                if (ipath.layer.visible==true && ipath.layer.locked==false && ipath.hidden==false && ipath.locked==false) {
                                    var fillColor = ipath.fillColor;
                                    if (fillColor.typename == "CMYKColor") {
                                        if (isColorBlack (fillColor)) {
                                            var fillk = Math.round(fillColor.black);
                                            cmykBlackToSpot (ipath, true, false, fillk);
                                            fcounter++;
                                        }
                                    }
                                    else if (fillColor.typename == "GrayColor") {
                                        if (grayNotWhiteOrClear (fillColor)) {
                                            var fillk = Math.round(fillColor.gray);
                                            cmykBlackToSpot (ipath, true, false, fillk);
                                            fcounter++;
                                        }
                                    }
                                    else if (fillColor.typename == "GradientColor") {
                                        var stops = ipath.fillColor.gradient.gradientStops;
                                        for (s=0; s<stops.length; s++) {
                                            var iStop = stops[s];
                                            var stopColor = iStop.color;
                                            if (stopColor.typename == "CMYKColor") {
                                                if (isColorBlack (stopColor)) {
                                                    var stopk = Math.round(stopColor.black);
                                                    iStop.color = sw.color;
                                                    iStop.color.tint = stopk;
                                                    gcounter++;
                                                }
                                            }
                                            if (stopColor.typename == "GrayColor") {
                                                if (grayNotWhiteOrClear (stopColor)) {
                                                    var stopk = Math.round(stopColor.gray);
                                                    iStop.color = sw.color;
                                                    iStop.color.tint = stopk;
                                                    gcounter++;
                                                }
                                            }
                                        }
                                    }
                                    var strokeColor = ipath.strokeColor;
                                    if (strokeColor.typename == "CMYKColor") {
                                        if (isColorBlack (strokeColor)) {
                                            var strokek = Math.round(strokeColor.black);
                                            cmykBlackToSpot (ipath, false, true, strokek);
                                            scounter++;
                                        }
                                    }
                                    else if (strokeColor.typename == "GrayColor") {
                                        if (grayNotWhiteOrClear (strokeColor)) {
                                            var strokek = Math.round(strokeColor.gray);
                                            cmykBlackToSpot (ipath, false, true, strokek);
                                            scounter++;
                                        }
                                    }
                                    else if (strokeColor.typename == "GradientColor") {
                                        var stops = ipath.strokeColor.gradient.gradientStops;
                                        for (s=0; s<stops.length; s++) {
                                            var iStop = stops[s];
                                            var stopColor = iStop.color;
                                            if (stopColor.typename == "CMYKColor") {
                                                if (isColorBlack (stopColor)) {
                                                    var stopk = Math.round(stopColor.black);
                                                    iStop.color = sw.color;
                                                    iStop.color.tint = stopk;
                                                    gcounter++;
                                                }
                                            }
                                            if (stopColor.typename == "GrayColor") {
                                                if (grayNotWhiteOrClear (stopColor)) {
                                                    var stopk = Math.round(stopColor.gray);
                                                    iStop.color = sw.color;
                                                    iStop.color.tint = stopk;
                                                    gcounter++;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            
                            alert(fcounter + ' Fill(s), ' + scounter + ' stroke(s) & ' + gcounter + ' Gradient Stop(s) processed.');
                            
                            function cmykBlackToSpot (path, fill, stroke, k) {
                                if (fill) {
                                    path.fillColor = sw.color;
                                    path.fillColor.tint = k;
                                }
                                if (stroke) {
                                    path.strokeColor = sw.color;
                                    path.strokeColor.tint = k;
                                }
                            }
                            function isColorBlack (cmykColor) {
                                var c = Math.round(cmykColor.cyan);
                                var m = Math.round(cmykColor.magenta);
                                var y = Math.round(cmykColor.yellow);
                                var k = Math.round(cmykColor.black);
                                
                                if (c==0 && m==0 && y==0 && k != 0)
                                    return true
                                else
                                    return false
                            }
                            function grayNotWhiteOrClear (GrayColor) {
                                var pct = Math.round(GrayColor.gray);
                            
                                if (pct != 0)
                                    return true
                                else
                                    return false
                            }
                            function createSpotBlack(){
                                if (!doesColorExist("spot black")){
                                    // Create CMYKColor
                                    var cmykColor = new CMYKColor();
                                    cmykColor.cyan = 9;
                                    cmykColor.magenta = 9;
                                    cmykColor.yellow = 9;
                                    cmykColor.black = 96;
                                    // Create Spot
                                    var spot = idoc.spots.add();
                                    spot.color = cmykColor;
                                    spot.colorType = ColorModel.SPOT;
                                    spot.name = "spot black";
                                }
                            }
                            function doesColorExist(){
                                var clrs = idoc.swatches;
                                for (var i=0;i<clrs.length;i++){
                                    if (clrs[i].name=="spot black"){
                                        return true;
                                    }
                                }
                                return false;
                            }
                            
                            • 11. Re: Error 21 (Changing color on text)
                              CarlosCanto Adobe Community Professional & MVP

                              in your script in post # 9, the problem with the text color is that you're not defining the actual character fill color the way we're doing it with the paths

                               

                              change this

                              var iChar = chars[c];
                              if (iChar.characterAttributes.fillColor.typename == "CMYKColor") {
                                   if (isTextBlack (fillColor)) { // *** fillColor is undefined *** or defined with who knows what from previous character
                              

                               

                              to

                              var iChar = chars[c];
                              var fillColor = iChar.characterAttributes.fillColor;
                              if (fillColor.typename == "CMYKColor") {
                                   if (isTextBlack (fillColor)) {
                              
                              1 person found this helpful
                              • 12. Re: Error 21 (Changing color on text)
                                elDudereno Level 3

                                I had come to a similar conclusion. I have found a way to avoid locked text, and locked layers also. I have added this to the code in post 10;

                                var la = idoc.layers;
                                
                                //check through layers for text and change
                                for (l=0; l<la.length; l++){
                                    var lyr=la[l];
                                    if (lyr.visible==true && lyr.locked==false) {
                                        var lyrtxt = lyr.textFrames; {
                                        for (lt=0; lt<lyrtxt.length; lt++)
                                            var iTxtFrm = lyrtxt[lt];
                                            if (iTxtFrm.locked==false && iTxtFrm.hidden==false) {
                                            var chars = iTxtFrm.characters;
                                            for (c=0; c<chars.length; c++) {
                                                var ltr = chars[c];
                                                if (ltr.characterAttributes.fillColor.typename == "CMYKColor"){
                                                    if (isColorBlack (ltr.characterAttributes.fillColor)) {
                                                        var fillk = Math.round(ltr.characterAttributes.fillColor.black);
                                                        cmykBlackToSpot (ltr, true, false, fillk);
                                                        fcounter++;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                

                                It works great, but only on one text frame per layer.

                                • 13. Re: Error 21 (Changing color on text)
                                  CarlosCanto Adobe Community Professional & MVP

                                  It works great, but only on one text frame per layer.

                                  looping through objects at a layer level, constrains the search to only top level objects, so if your text frames are grouped, they will be ignored.

                                   

                                  looping through objects at the document level, searches top level objects (like groups) in addition to what's inside such objects (your text if that was the case)

                                  1 person found this helpful
                                  • 14. Re: Error 21 (Changing color on text)
                                    elDudereno Level 3

                                    So I have no choice but to check for parents?

                                     

                                    I was hoping to go from the top down instead of looking up through the clutter. Dang.

                                     

                                    Thanks for the insight.

                                    • 15. Re: Error 21 (Changing color on text)
                                      CarlosCanto Adobe Community Professional & MVP

                                      at the level layer, you won't even get the chance to get to the text, much less to the parent.

                                       

                                      at the document level, yes, no choice but to traverse the parent, grand parent, etc depending how deep the groups are...there's a technique called "recursion" that tackles that.

                                      • 16. Re: Error 21 (Changing color on text)
                                        elDudereno Level 3

                                        I am really REALLY close to my final goal. I have a function that goes up a layer tree checking for locked layers. I have a function that goes up a group tree checking for locked groups and then passing to the layer function. I wrote a function that determines which one to start with, but evidently, you can't use a variable to call a function.

                                         

                                        How can I tell it to choose the proper start function? It has to be a "if" statement otherwise it just changes all the text regardless of if it has a locked or hidden parent.

                                         

                                        Here is the entire script as it stands. Issues are between lines 105 & 159

                                         

                                        // script.name = cmykBlackNgrayscaleToSpotBlack.jsx; 
                                        // script.description = changes art color from standard CMYK Black and Grayscale tones to a Spot swatch named "spot black";
                                        // 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;
                                        createSpotBlack();
                                        var sw = idoc.swatches["spot black"];
                                        var fcounter = 0;
                                        var scounter = 0;
                                        var gcounter = 0;
                                        
                                        //apply spot to paths and gradient stops
                                        for (j=0; j<pi.length; j++) {
                                            var ipath = pi[j];
                                            if (ipath.layer.visible==true && ipath.layer.locked==false && ipath.hidden==false && ipath.locked==false) {
                                                var fillColor = ipath.fillColor;
                                                if (fillColor.typename == "CMYKColor") {
                                                    if (isColorBlack (fillColor)) {
                                                        var fillk = Math.round(fillColor.black);
                                                        cmykBlackToSpot (ipath, true, false, fillk);
                                                        fcounter++;
                                                    }
                                                }
                                                else if (fillColor.typename == "GrayColor") {
                                                    if (grayNotWhiteOrClear (fillColor)) {
                                                        var fillk = Math.round(fillColor.gray);
                                                        cmykBlackToSpot (ipath, true, false, fillk);
                                                        fcounter++;
                                                    }
                                                }
                                                else if (fillColor.typename == "GradientColor") {
                                                    var stops = ipath.fillColor.gradient.gradientStops;
                                                    for (s=0; s<stops.length; s++) {
                                                        var iStop = stops[s];
                                                        var stopColor = iStop.color;
                                                        if (stopColor.typename == "CMYKColor") {
                                                            if (isColorBlack (stopColor)) {
                                                                var stopk = Math.round(stopColor.black);
                                                                iStop.color = sw.color;
                                                                iStop.color.tint = stopk;
                                                                gcounter++;
                                                            }
                                                        }
                                                        if (stopColor.typename == "GrayColor") {
                                                            if (grayNotWhiteOrClear (stopColor)) {
                                                                var stopk = Math.round(stopColor.gray);
                                                                iStop.color = sw.color;
                                                                iStop.color.tint = stopk;
                                                                gcounter++;
                                                            }
                                                        }
                                                    }
                                                }
                                                var strokeColor = ipath.strokeColor;
                                                if (strokeColor.typename == "CMYKColor") {
                                                    if (isColorBlack (strokeColor)) {
                                                        var strokek = Math.round(strokeColor.black);
                                                        cmykBlackToSpot (ipath, false, true, strokek);
                                                        scounter++;
                                                    }
                                                }
                                                else if (strokeColor.typename == "GrayColor") {
                                                    if (grayNotWhiteOrClear (strokeColor)) {
                                                        var strokek = Math.round(strokeColor.gray);
                                                        cmykBlackToSpot (ipath, false, true, strokek);
                                                        scounter++;
                                                    }
                                                }
                                                else if (strokeColor.typename == "GradientColor") {
                                                    var stops = ipath.strokeColor.gradient.gradientStops;
                                                    for (s=0; s<stops.length; s++) {
                                                        var iStop = stops[s];
                                                        var stopColor = iStop.color;
                                                        if (stopColor.typename == "CMYKColor") {
                                                            if (isColorBlack (stopColor)) {
                                                                var stopk = Math.round(stopColor.black);
                                                                iStop.color = sw.color;
                                                                iStop.color.tint = stopk;
                                                                gcounter++;
                                                            }
                                                        }
                                                        if (stopColor.typename == "GrayColor") {
                                                            if (grayNotWhiteOrClear (stopColor)) {
                                                                var stopk = Math.round(stopColor.gray);
                                                                iStop.color = sw.color;
                                                                iStop.color.tint = stopk;
                                                                gcounter++;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        for (t=0; t<tf.length; t++) {
                                            var iTxtFrm = tf[t];
                                            if (iTxtFrm.locked==false && iTxtFrm.hidden==false) {
                                                var doThisOne = meetTheParents(iTxtFrm);
                                                alert(doThisOne);
                                                if (doThisOne(iTxtFrm)){
                                                    var chars = iTxtFrm.characters;
                                                    for (c=0; c<chars.length; c++) {
                                                        var ltr = chars[c];
                                                        if (ltr.characterAttributes.fillColor.typename == "CMYKColor"){
                                                            if (isColorBlack (ltr.characterAttributes.fillColor)) {
                                                                var fillk = Math.round(ltr.characterAttributes.fillColor.black);
                                                                cmykBlackToSpot (ltr, true, false, fillk);
                                                                fcounter++;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        
                                        alert(fcounter + ' Fill(s), ' + scounter + ' stroke(s) & ' + gcounter + ' Gradient Stop(s) processed.');
                                        
                                        function meetTheParents(textFrame){
                                            if (textFrame.parent.typename = "Layer"){
                                                return "isLayerFree"
                                            } else if (textFrame.parent.typename = "GroupItem"){
                                                return "isGroupFree"
                                            } else (alert("I don't think this is supposed to happen. Ever. Something has exploded."));
                                        }
                                        
                                        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
                                            }
                                        }
                                        
                                        function cmykBlackToSpot (path, fill, stroke, k) {
                                            if (fill) {
                                                path.fillColor = sw.color;
                                                path.fillColor.tint = k;
                                            }
                                            if (stroke) {
                                                path.strokeColor = sw.color;
                                                path.strokeColor.tint = k;
                                            }
                                        }
                                        function isColorBlack (cmykColor) {
                                            var c = Math.round(cmykColor.cyan);
                                            var m = Math.round(cmykColor.magenta);
                                            var y = Math.round(cmykColor.yellow);
                                            var k = Math.round(cmykColor.black);
                                            if (c==0 && m==0 && y==0 && k != 0)
                                                return true
                                            else
                                                return false
                                        }
                                        function grayNotWhiteOrClear (GrayColor) {
                                            var pct = Math.round(GrayColor.gray);
                                            if (pct != 0)
                                                return true
                                            else
                                                return false
                                        }
                                        function createSpotBlack(){
                                            if (!doesColorExist("spot black")){
                                                // Create CMYKColor
                                                var cmykColor = new CMYKColor();
                                                cmykColor.cyan = 9;
                                                cmykColor.magenta = 9;
                                                cmykColor.yellow = 9;
                                                cmykColor.black = 96;
                                                // Create Spot
                                                var spot = idoc.spots.add();
                                                spot.color = cmykColor;
                                                spot.colorType = ColorModel.SPOT;
                                                spot.name = "spot black";
                                            }
                                        }
                                        function doesColorExist(){
                                            var clrs = idoc.swatches;
                                            for (var i=0;i<clrs.length;i++){
                                                if (clrs[i].name=="spot black"){
                                                    return true;
                                                }
                                            }
                                            return false;
                                        }
                                        function isLayerFree(layer){
                                            if (layer.locked == false && layer.visible == true){
                                                var gpa = layer.parent;
                                                isLayerFree(gpa);
                                                return true
                                                } else {
                                                return false
                                            }
                                        }
                                        

                                        As always, thank you for your time.

                                        • 17. Re: Error 21 (Changing color on text)
                                          CarlosCanto Adobe Community Professional & MVP

                                          it just dawned on me, even though the "layer" property is not listed in the text frame properties, a text frame is still a subset of the "super class" pageItem...so

                                           

                                          for page items we have

                                          if (ipath.layer.visible==true && ipath.layer.locked==false && ipath.hidden==false && ipath.locked==false) {
                                          

                                           

                                           

                                          for text, do the same

                                          if (iTxtFrm.layer.visible==true && iTxtFrm.layer.locked==false && iTxtFrm.hidden==false && iTxtFrm.locked==false) {
                                          

                                           

                                          and forget about meeting the parents

                                          • 18. Re: Error 21 (Changing color on text)
                                            elDudereno Level 3

                                            Carlos, you are right about that. I had a logic knot that was preventing it from working on text in groups AND directly on layers at the same time. I ended up using a version that didn't use your fix, even though it was viable.

                                             

                                            Here is my final script. Works like a charm. Thanks for all the help.

                                             

                                             

                                            // script.name = BlackToSpotBlack.jsx; 
                                            // script.description = changes art color from standard CMYK Black and Grayscale tones to a Spot swatch named "spot black";
                                            // 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;
                                            createSpotBlack();
                                            var sw = idoc.swatches["spot black"];
                                            var fcounter = 0;
                                            var scounter = 0;
                                            var gcounter = 0;
                                            
                                            //apply spot to paths and gradient stops
                                            for (j=0; j<pi.length; j++) {
                                                var ipath = pi[j];
                                                if (ipath.layer.visible==true && ipath.layer.locked==false && ipath.hidden==false && ipath.locked==false) {
                                                    var fillColor = ipath.fillColor;
                                                    if (fillColor.typename == "CMYKColor") {
                                                        if (isColorBlack (fillColor)) {
                                                            var fillk = Math.round(fillColor.black);
                                                            cmykBlackToSpot (ipath, true, false, fillk);
                                                            fcounter++;
                                                        }
                                                    }
                                                    else if (fillColor.typename == "GrayColor") {
                                                        if (grayNotWhiteOrClear (fillColor)) {
                                                            var fillk = Math.round(fillColor.gray);
                                                            cmykBlackToSpot (ipath, true, false, fillk);
                                                            fcounter++;
                                                        }
                                                    }
                                                    else if (fillColor.typename == "GradientColor") {
                                                        var stops = ipath.fillColor.gradient.gradientStops;
                                                        for (s=0; s<stops.length; s++) {
                                                            var iStop = stops[s];
                                                            var stopColor = iStop.color;
                                                            if (stopColor.typename == "CMYKColor") {
                                                                if (isColorBlack (stopColor)) {
                                                                    var stopk = Math.round(stopColor.black);
                                                                    iStop.color = sw.color;
                                                                    iStop.color.tint = stopk;
                                                                    gcounter++;
                                                                }
                                                            }
                                                            if (stopColor.typename == "GrayColor") {
                                                                if (grayNotWhiteOrClear (stopColor)) {
                                                                    var stopk = Math.round(stopColor.gray);
                                                                    iStop.color = sw.color;
                                                                    iStop.color.tint = stopk;
                                                                    gcounter++;
                                                                }
                                                            }
                                                        }
                                                    }
                                                    var strokeColor = ipath.strokeColor;
                                                    if (strokeColor.typename == "CMYKColor") {
                                                        if (isColorBlack (strokeColor)) {
                                                            var strokek = Math.round(strokeColor.black);
                                                            cmykBlackToSpot (ipath, false, true, strokek);
                                                            scounter++;
                                                        }
                                                    }
                                                    else if (strokeColor.typename == "GrayColor") {
                                                        if (grayNotWhiteOrClear (strokeColor)) {
                                                            var strokek = Math.round(strokeColor.gray);
                                                            cmykBlackToSpot (ipath, false, true, strokek);
                                                            scounter++;
                                                        }
                                                    }
                                                    else if (strokeColor.typename == "GradientColor") {
                                                        var stops = ipath.strokeColor.gradient.gradientStops;
                                                        for (s=0; s<stops.length; s++) {
                                                            var iStop = stops[s];
                                                            var stopColor = iStop.color;
                                                            if (stopColor.typename == "CMYKColor") {
                                                                if (isColorBlack (stopColor)) {
                                                                    var stopk = Math.round(stopColor.black);
                                                                    iStop.color = sw.color;
                                                                    iStop.color.tint = stopk;
                                                                    gcounter++;
                                                                }
                                                            }
                                                            if (stopColor.typename == "GrayColor") {
                                                                if (grayNotWhiteOrClear (stopColor)) {
                                                                    var stopk = Math.round(stopColor.gray);
                                                                    iStop.color = sw.color;
                                                                    iStop.color.tint = stopk;
                                                                    gcounter++;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            // 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(textFrame){
                                                if (textFrame.parent.typename = "Layer"){
                                                    return "isLayerFree";
                                                } else if (textFrame.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 == "CMYKColor"){
                                                        if (isColorBlack (ltr.characterAttributes.fillColor)) {
                                                            var fillk = Math.round(ltr.characterAttributes.fillColor.black);
                                                            cmykBlackToSpot (ltr, true, false, fillk);
                                                            fcounter++;
                                                        }
                                                    }
                                                    if (ltr.characterAttributes.fillColor.typename == "GrayColor"){
                                                        if (grayNotWhiteOrClear (ltr.characterAttributes.fillColor)) {
                                                            var fillk = Math.round(ltr.characterAttributes.fillColor.gray);
                                                            cmykBlackToSpot (ltr, true, false, fillk);
                                                            fcounter++;
                                                        }
                                                    }
                                                    if (ltr.characterAttributes.strokeColor.typename == "CMYKColor"){
                                                        if (isColorBlack (ltr.characterAttributes.strokeColor)) {
                                                            var strokek = Math.round(ltr.characterAttributes.strokeColor.black);
                                                            cmykBlackToSpot (ltr, false, true, strokek);
                                                            scounter++;
                                                        }
                                                    }
                                                    if (ltr.characterAttributes.strokeColor.typename == "GrayColor"){
                                                        if (grayNotWhiteOrClear (ltr.characterAttributes.strokeColor)) {
                                                            var strokek = Math.round(ltr.characterAttributes.strokeColor.gray);
                                                            cmykBlackToSpot (ltr, false, true, strokek);
                                                            scounter++;
                                                        }
                                                    }
                                                }
                                            };
                                            function cmykBlackToSpot (path, fill, stroke, k) {
                                                if (fill) {
                                                    path.fillColor = sw.color;
                                                    path.fillColor.tint = k;
                                                }
                                                if (stroke) {
                                                    path.strokeColor = sw.color;
                                                    path.strokeColor.tint = k;
                                                }
                                            }
                                            function isColorBlack (cmykColor) {
                                                var c = Math.round(cmykColor.cyan);
                                                var m = Math.round(cmykColor.magenta);
                                                var y = Math.round(cmykColor.yellow);
                                                var k = Math.round(cmykColor.black);
                                                if (c==0 && m==0 && y==0 && k != 0)
                                                    return true
                                                else
                                                    return false
                                            }
                                            function grayNotWhiteOrClear (GrayColor) {
                                                var pct = Math.round(GrayColor.gray);
                                                if (pct != 0)
                                                    return true
                                                else
                                                    return false
                                            }
                                            function createSpotBlack(){
                                                if (!doesColorExist("spot black")){
                                                    // Create CMYKColor
                                                    var cmykColor = new CMYKColor();
                                                    cmykColor.cyan = 9;
                                                    cmykColor.magenta = 9;
                                                    cmykColor.yellow = 9;
                                                    cmykColor.black = 96;
                                                    // Create Spot
                                                    var spot = idoc.spots.add();
                                                    spot.color = cmykColor;
                                                    spot.colorType = ColorModel.SPOT;
                                                    spot.name = "spot black";
                                                }
                                            }
                                            function doesColorExist(){
                                                var clrs = idoc.swatches;
                                                for (var i=0;i<clrs.length;i++){
                                                    if (clrs[i].name=="spot black"){
                                                        return true;
                                                    }
                                                }
                                                return false;
                                            }
                                            
                                            • 19. Re: Error 21 (Changing color on text)
                                              CarlosCanto Adobe Community Professional & MVP

                                              great, thanks for posting the final working version

                                              • 20. Re: Error 21 (Changing color on text)
                                                elDudereno Level 3

                                                Again Carlos, I can't thank you enough for getting me started with the original script and pointing me in the right direction as I expanded it.

                                                • 21. Re: Error 21 (Changing color on text)
                                                  CarlosCanto Adobe Community Professional & MVP

                                                  you're welcome, I could have done the text handling for you, but it seemed like you wanted to learn and do it yourself, and it worked out fine, good job. it was more fun too, right?

                                                  • 22. Re: Error 21 (Changing color on text)
                                                    elDudereno Level 3

                                                    I'm really enjoying it.

                                                     

                                                    I've noticed the script was changing unlocked PathItems in locked groups. This edit fixes that. Also, this discussion has a script that goes from a spot color to grayscale http://forums.adobe.com/thread/1283327

                                                     

                                                    // script.name = BlackToSpotBlack.jsx; 
                                                    // script.description = changes art color from standard CMYK Black and Grayscale tones to a Spot swatch named "spot black";
                                                    // 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;
                                                    createSpotBlack();
                                                    var sw = idoc.swatches["spot black"];
                                                    var fcounter = 0;
                                                    var scounter = 0;
                                                    var gcounter = 0;
                                                    
                                                    //apply spot 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 paths
                                                    function changePath(PathItem) {
                                                        var fillColor = PathItem.fillColor;
                                                        if (fillColor.typename == "CMYKColor") {
                                                            if (isColorBlack (fillColor)) {
                                                                var fillk = Math.round(fillColor.black);
                                                                cmykBlackToSpot (PathItem, true, false, fillk);
                                                                fcounter++;
                                                            }
                                                        }
                                                        else if (fillColor.typename == "GrayColor") {
                                                            if (grayNotWhiteOrClear (fillColor)) {
                                                                var fillk = Math.round(fillColor.gray);
                                                                cmykBlackToSpot (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 == "CMYKColor") {
                                                                    if (isColorBlack (stopColor)) {
                                                                        var stopk = Math.round(stopColor.black);
                                                                        iStop.color = sw.color;
                                                                        iStop.color.tint = stopk;
                                                                        gcounter++;
                                                                    }
                                                                }
                                                                if (stopColor.typename == "GrayColor") {
                                                                    if (grayNotWhiteOrClear (stopColor)) {
                                                                        var stopk = Math.round(stopColor.gray);
                                                                        iStop.color = sw.color;
                                                                        iStop.color.tint = stopk;
                                                                        gcounter++;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        var strokeColor = PathItem.strokeColor;
                                                        if (strokeColor.typename == "CMYKColor") {
                                                            if (isColorBlack (strokeColor)) {
                                                                var strokek = Math.round(strokeColor.black);
                                                                cmykBlackToSpot (PathItem, false, true, strokek);
                                                                scounter++;
                                                            }
                                                        }
                                                        else if (strokeColor.typename == "GrayColor") {
                                                            if (grayNotWhiteOrClear (strokeColor)) {
                                                                var strokek = Math.round(strokeColor.gray);
                                                                cmykBlackToSpot (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 == "CMYKColor") {
                                                                    if (isColorBlack (stopColor)) {
                                                                        var stopk = Math.round(stopColor.black);
                                                                        iStop.color = sw.color;
                                                                        iStop.color.tint = stopk;
                                                                        gcounter++;
                                                                    }
                                                                }
                                                                if (stopColor.typename == "GrayColor") {
                                                                    if (grayNotWhiteOrClear (stopColor)) {
                                                                        var stopk = Math.round(stopColor.gray);
                                                                        iStop.color = sw.color;
                                                                        iStop.color.tint = stopk;
                                                                        gcounter++;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                    // 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 == "CMYKColor"){
                                                                if (isColorBlack (ltr.characterAttributes.fillColor)) {
                                                                    var fillk = Math.round(ltr.characterAttributes.fillColor.black);
                                                                    cmykBlackToSpot (ltr, true, false, fillk);
                                                                    fcounter++;
                                                                }
                                                            }
                                                            if (ltr.characterAttributes.fillColor.typename == "GrayColor"){
                                                                if (grayNotWhiteOrClear (ltr.characterAttributes.fillColor)) {
                                                                    var fillk = Math.round(ltr.characterAttributes.fillColor.gray);
                                                                    cmykBlackToSpot (ltr, true, false, fillk);
                                                                    fcounter++;
                                                                }
                                                            }
                                                            if (ltr.characterAttributes.strokeColor.typename == "CMYKColor"){
                                                                if (isColorBlack (ltr.characterAttributes.strokeColor)) {
                                                                    var strokek = Math.round(ltr.characterAttributes.strokeColor.black);
                                                                    cmykBlackToSpot (ltr, false, true, strokek);
                                                                    scounter++;
                                                                }
                                                            }
                                                            if (ltr.characterAttributes.strokeColor.typename == "GrayColor"){
                                                                if (grayNotWhiteOrClear (ltr.characterAttributes.strokeColor)) {
                                                                    var strokek = Math.round(ltr.characterAttributes.strokeColor.gray);
                                                                    cmykBlackToSpot (ltr, false, true, strokek);
                                                                    scounter++;
                                                                }
                                                            }
                                                        }
                                                    };
                                                    function cmykBlackToSpot (path, fill, stroke, k) {
                                                        if (fill) {
                                                            path.fillColor = sw.color;
                                                            path.fillColor.tint = k;
                                                        }
                                                        if (stroke) {
                                                            path.strokeColor = sw.color;
                                                            path.strokeColor.tint = k;
                                                        }
                                                    }
                                                    function isColorBlack (cmykColor) {
                                                        var c = Math.round(cmykColor.cyan);
                                                        var m = Math.round(cmykColor.magenta);
                                                        var y = Math.round(cmykColor.yellow);
                                                        var k = Math.round(cmykColor.black);
                                                        if (c==0 && m==0 && y==0 && k != 0)
                                                            return true
                                                        else
                                                            return false
                                                    }
                                                    function grayNotWhiteOrClear (GrayColor) {
                                                        var pct = Math.round(GrayColor.gray);
                                                        if (pct != 0)
                                                            return true
                                                        else
                                                            return false
                                                    }
                                                    function createSpotBlack(){
                                                        if (!doesColorExist("spot black")){
                                                            // Create CMYKColor
                                                            var cmykColor = new CMYKColor();
                                                            cmykColor.cyan = 9;
                                                            cmykColor.magenta = 9;
                                                            cmykColor.yellow = 9;
                                                            cmykColor.black = 96;
                                                            // Create Spot
                                                            var spot = idoc.spots.add();
                                                            spot.color = cmykColor;
                                                            spot.colorType = ColorModel.SPOT;
                                                            spot.name = "spot black";
                                                        }
                                                    }
                                                    function doesColorExist(){
                                                        var clrs = idoc.swatches;
                                                        for (var i=0;i<clrs.length;i++){
                                                            if (clrs[i].name=="spot black"){
                                                                return true;
                                                            }
                                                        }
                                                        return false;
                                                    }
                                                    

                                                     

                                                    Message was edited by: elDudereno

                                                    • 23. Re: Error 21 (Changing color on text)
                                                      roofoofighter

                                                      Hi, I can't get this script to work. When I run it, nothing happens.

                                                      • 24. Re: Error 21 (Changing color on text)
                                                        elDudereno Level 3

                                                        Use the script from post 22.

                                                        If this script is run on an illustrator document with CMYK or Greyscale black or grey it will change to a spot black. Text and paths will be changed. At the very least this script should create the spot black swatch.