5 Replies Latest reply on Aug 9, 2010 11:44 AM by Tom Tomasko

    [JS CS3] Can't add a swatch to some documents

    Tom Tomasko Level 1

      Hello,

       

      I have the following script:

       

      var myDoc = app.activeDocument;
      colorIt(myDoc, "lime",cmykRatios =[40,0,80,0]);
      
      function colorIt(docRef,colorName,cmykRatios){
          docRef.colors.add({colorValue:cmykRatios, colorModel:ColorModel.process, colorSpace:ColorSpace.cmyk,name:colorName}); 
          }//end colorIt
      

       

      For some documents this script works as it should. But for others I get the error message in ESTK "An incorrect number of color values was given."

       

      The error message is telling me that I cannot add CMYK swatches. If I change the colorSpace to ColorSpace.rgb and have only three values in the array cmykRatios, then the script works in the document that before would not accept it.

       

      But then trying to add an RGB swatch to the documents that previously accepted the new CMYK swatch gives me the very same error message:  "An incorrect number of color values was given."

       

      Obviously there is some difference between these documents but I cannot see one. Why would one ID file accept CMYK colors but not RGB colors and vice versa? Any clues as to what might be wrong here?

       

      Thanks,

      Tom

        • 1. Re: [JS CS3] Can't add a swatch to some documents
          Marijan Tompa [tomaxxi] Level 4

          Hey Tom!

           

          I just tested your script, and it's working fine on new documents. Then, I created swatch with same name but in RGB space, and received same error. Are you sure that you don't have swatch with same name already in document? But, on the other side, why would it work after you change script to RGB. I added one row into your script, so you can test it.

           

          var myDoc = app.activeDocument;
          colorIt(myDoc, "lime",cmykRatios =[40,0,80,0]);
          
          function colorIt(docRef,colorName,cmykRatios){
              if(app.activeDocument.swatches.itemByName(colorName).isValid != true){
                  docRef.colors.add({colorValue:cmykRatios, colorModel:ColorModel.process, colorSpace:ColorSpace.cmyk,name:colorName});
              }
          }
          

           

          --

          tomaxxi

          http://indisnip.wordpress.com/

          • 2. Re: [JS CS3] Can't add a swatch to some documents
            Tom Tomasko Level 1

            Upon further investigation it seems the difference between the documents is this:

             

            If the document does not have any CMYK swatches before invoking the script, then it will not accept a CMYK swatch added by the script.

             

            If the document does not have any RGB swatches before invoking the script, then it will not accept a RGB swatch added bvy the script.

             

            Since I will not know beforehand what is what in terms of what swatches there are, I'd like to find out how to deal with this.

             

            Tom

             

            [wrote this before seeing reply above]

            • 3. Re: [JS CS3] Can't add a swatch to some documents
              [Jongware] Most Valuable Participant

              You got me baffled for a while, that is, until I thought of checking your parameters!

               

              There is no 'colorSpace' property in Color, it's called 'space'. Setting unknown properties is not an error (they are added to the object, I think), so your default color space is never changed from what it was when adding the new color.

              • 4. Re: [JS CS3] Can't add a swatch to some documents
                Tom Tomasko Level 1

                isValid does not work for CS3. However,

                 

                app.activeDocument.swatches.itemByName(colorName)==null

                 

                does the same thing.

                 

                If the swatch does not already exist and there are only RGB swatches (besides the four standards of None, Paper, Black and Registration), then I cannot add a CMYK swatch with the script. But if there is at least one CMYK or no added colors to a new document, then the script works.

                 

                Tom

                • 5. Re: [JS CS3] Can't add a swatch to some documents
                  Tom Tomasko Level 1

                  Jongware,

                   

                  Your observation appears correct! I plugged in your edit into my larger script and now it consistently colorizes the text frames I want to make a bright lime green.

                   

                  Thanks!

                   

                  Tom