7 Replies Latest reply on Aug 17, 2008 12:27 PM by (Simon_Fenwick)

    [JS] How do you set change GREP preferences to a custom color swatch?

    Level 1
      Hi All

      I am having difficulty with setting the 'changeGrepPreferences' to a specific color swatch in an InDesign CS3 document.
      The code below is meant to search within text styled with the Character Style "Hyperlink" and change any spaces (which import from Word as black text, i.e. have a black underscore) to the font color of the Character Style (which is a spot color that I've named 'Hyperlinks grey'). I think I have cracked it except for the changeGrepPreferences line; but I'll let the experts be the judge of that.

      app.findGrepPreferences = null;
      app.changeGrepPreferences = null;
      app.findGrepPreferences.appliedCharacterStyle = 'Hyperlink';
      app.findGrepPreferences.fillColor = 'Black';
      app.findGrepPreferences.findWhat = ' ';
      app.changeGrepPreferences.fillColor = app.activeDocument.swatches.itemByName('Hyperlinks grey');
      app.changeGrepPreferences.changeTo = '$0';
      app.activeDocument.changeGrep();

      Any help will be greatly received.
      Simon
        • 1. Re: [JS] How do you set change GREP preferences to a custom color swatch?
          Level 1
          Hi Simon,

          There is a bug in finding/changing colors in CS3 that has not yet been fixed--you can only find/change colors that exist in the set of application default colors. This is bad, and I've complained about it loudly, but to no avail.

          There is a workaround, however--create the color in the application defaults and use it to set the find or change preferences:

          var myColor = app.colors.add({name:"Hyperlinks grey"});
          app.changeGrepPreferences.fillColor = myColor;

          ...and then, after you do your find/change operation:

          app.colors.item("Hyperlinks grey").remove();

          CS2 had it right--set the find/change using colors on the document, not the application. Somewhere along the line, CS3 got it wrong.

          Thanks,

          Ole
          • 2. Re: [JS] How do you set change GREP preferences to a custom color swatch?
            Level 1
            Hi Ole

            Thanks for the quick response. Your workaround works well up to a point. However, because the color 'Hyperlinks grey' already exists in the document I'm working on, when you add a new swatch it creates 'Hyperlinks grey 2'. I really need the text to be colored with the original color: in our production workflow we use a system called Odystar to create a Web and a Print PDF. For a print PDF, Odystar first looks for text in the spot color 'Hyperlinks grey' and turns it to black and removes the underscore. It means that hyperlinks look like hyperlinks for a Web PDF, but not for a print PDF.

            Also, the line of code that removes the created color only works if no replacements were made. When replacements have been made Hyperlinks grey 2 stays in the swatch. And if you ran the script again in which a replacement was made, it would make Hyperlinks grey 3 etc etc.

            So... do you have any further tricks up your sleeve?

            Thanks again for you help so far.
            With kind regards
            Simon
            • 3. Re: [JS] How do you set change GREP preferences to a custom color swatch?
              Level 1
              Hi Ole

              I have managed to get it working with a slight tweak to your workaround. Instead of creating a color with the same name as one in the Document, I create a temporary color:

              var myColor = app.colors.add({name:'Temp grey'});

              Then I delete it from the application as you suggest, and then from the document, replacing it with the color I want:

              app.colors.item('Temp grey').remove();
              app.activeDocument.colors.item('Temp grey').remove('Hyperlinks grey');

              So, thanks for the pointer mate
              All the best
              Simon
              • 4. Re: [JS] How do you set change GREP preferences to a custom color swatch?
                Level 1
                Hi Simon,

                re: "However, because the color 'Hyperlinks grey' already exists in the document I'm working on, when you add a new swatch it creates 'Hyperlinks grey 2'."

                Adding the swatch to the application defaults shouldn't add it to an existing document.

                You can test for the existence of a color using something like this:

                try{
                myColor = app.colors.item("Hyperlinks grey");
                myColor.name;
                }
                catch(myError){
                myColor = app.colors.add({name:"Hyperlinks grey"});
                }

                If the color does not already exist, then myColor.name will throw an error, which will be handled by the catch statement.

                Thanks,

                Ole
                }
                • 5. Re: [JS] How do you set change GREP preferences to a custom color swatch?
                  Level 1
                  Hi Ole

                  Well unless I have missed something, I can only disagree with you in that the script did add 'Hyperlinks grey 2' to the document, even though there was no color called 'Hyperlinks grey' in the application prior to or after running the script. But I am very much the novice at JS, so I may well have missed something.

                  Anyway, I did get my script to work with your help as I mentioned in my previous message, and I have since improved it by catching an error thrown when no replacements in the GREP find/change were made:

                  if (activeDocument.colors.item('Temp grey') !== null)
                  activeDocument.colors.item('Temp grey').remove('Hyperlinks grey');

                  Thanks again for your help
                  With kind regards
                  Simon
                  • 6. Re: [JS] How do you set change GREP preferences to a custom color swatch?
                    sstanleyau Level 4
                    On 15/8/08 11:22 PM, "Simon Fenwick" <member@adobeforums.com> wrote:<br /><br />>  the script did add 'Hyperlinks grey 2' to the document, even though there was<br />> no color called 'Hyperlinks grey' in the application prior to or after running<br />> the script<br /><br />That suggests you added a color to the document rather than the application.<br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au>
                    • 7. Re: [JS] How do you set change GREP preferences to a custom color swatch?
                      Level 1
                      Yes, it does look that way.

                      I believe that Ole's workaround works exactly as he suggests. That is, it adds a color to the application and later removes it. However, the GREP find/change might add the color to the document when it makes a replacement, and if there a color there with the same name (but different CMYK values since they are not defined by the script), than it creates a version of that color name in the document.

                      Make sense?

                      Simon