4 Replies Latest reply on Jul 29, 2008 3:18 AM by (T._van_Gerwen)

    CS3 find/change text color

      Hi everybody,

      First time posting here, i've done some applescripting with id, which seems to go fine so far, but now i've run into this seemingly simple problem which i'm not able to tackle, and i can't find a scripting guide or example that shows how to do simple things like this. If anybody has some suggestions, or a working example to change text color with find/change, i would be most grateful.

      I've tried a lot of minor variations to this script, but the code below shows clearly what i'm trying to accomplish (also, it doesn't work).

      Obviously the document i'm using has a swatch named "red" but i keep getting "Expected string, swatch or nothing, but received nothing."

      tell application "Adobe InDesign CS3"

      set theDocument to active document

      set find text preferences to nothing
      set change text preferences to nothing

      set find what of find text preferences to "wordtosearchfor"
      set fill color of change text preferences to swatch "red" of theDocument

      tell theDocument
      change text
      end tell

      end tell
        • 1. Re: CS3 find/change text color
          Level 1
          Hi T. van Gerwen,

          This is a known bug in CS3--searching for a color won't work unless the color exists as an application default color. So, as a workaround, you have to tell the InDesign application create the color, then search the document for that color. When you're done, you can delete the color from the application.

          This *might* have gotten fixed in 5.0.3, but I don't know--haven't had time to test it. I doubt it.

          Thanks,

          Ole
          • 2. Re: CS3 find/change text color
            Level 1
            Thanks, so it's a bug :) i have 5.0.3, and it's not fixed.

            By the way i am not searching for a color, the idea is to search for a text string, and assign that a color, which works perfectly using the application's find/change dialog, so should expectedly be scriptable.

            I now added the following lines to the script, it now first creates a named swatch in the document (instead of the swatch being already saved with the document), but i keep getting the message "Expected string, swatch or nothing, but received nothing." :

            tell theDocument
            set myColor to make color with properties {name:"testcolor", model:process, color value:{0, 100, 100, 0}}
            end tell

            set fill color of change text preferences to myColor of theDocument

            Am i missing something? Any working examples are appreciated, note that the idea is to NOT use character styles.

            Thanks again for any help.
            • 3. Re: CS3 find/change text color
              Level 1
              Hi T. van Gerwen,

              Here's a script example that shows the workaround. The part you're interested in is the stuff in the "mySnippet" handler.

              --ChangeTextColor.applescript
              
              --An InDesign CS3 Applescript
              --
              --Shows a workaround for changing the text color. A bug in InDesign CS3
              --prevents you from setting the fill color/stroke color in find/change text
              --preferences to a color in a document; instead, you have to refer to a color
              --in the application.
              main()
              on main()
              mySetup()
              mySnippet()
              end main
              on mySetup()
              tell application "Adobe InDesign CS3"
                set myDocument to make document
                tell myDocument
                 set horizontal measurement units of view preferences to points
                 set vertical measurement units of view preferences to points
                 try
                  set myColor to color "Red"
                 on error
                  set myColor to make color with properties {name:"Red", color value:{0, 100, 100, 0}}
                 end try
                 tell page 1
                  set myTextFrame to make text frame with properties {geometric bounds:{72, 72, 288, 288}, contents:"This is an example text frame. Change the color of the word InDesign to red."}
                 end tell
                end tell
              end tell
              end mySetup
              on mySnippet()
              tell application "Adobe InDesign CS3"
                set find text preferences to nothing
                set change text preferences to nothing
                --This is what you should be able to do:
                --set fill color of change text preferences to color "Red" of document 1
                --This is the workaround:
                try
                 set myColor to color "Red"
                 set myColorExists to true
                on error
                 set myColor to make color with properties {name:"Red", color value:{0, 100, 100, 0}}
                 set myColorExists to false
                end try
                set fill color of change text preferences to myColor
                --End of the workaround.
                set find what of find text preferences to "InDesign"
                set myDocument to document 1
                tell myDocument
                 change text
                end tell
                --Delete the color if it did not already exist in the application.
                if myColorExists is false then
                 delete myColor
                end if
                set find text preferences to nothing
                set change text preferences to nothing
              end tell
              end mySnippet

              Thanks,

              Ole
              • 4. Re: CS3 find/change text color
                Level 1
                Hello,

                It works! Thanks for the example, here's the script i was trying to make, and which now also works ofcourse. It finds and replace text between tags:

                ------------------------
                -- TVGD CUSTOM SCRIPT
                -- START COLORIZE TAGGED TEXT
                -- assigns a color to tagged text, then deletes the tags
                -- place at end of script that fills the document, so that all text gets searched

                tell application "Adobe InDesign CS3"


                --CONFIGURATION

                --set document
                set theDocument to active document

                --set opening and closing tag for grep search
                --this may differ from text search because of escape characters for grep
                set openingTagGrep to "\\[C\\]" as string
                set closingTagGrep to "\\[/C\\]" as string

                --set opening and closing tag for text search
                set openingTagText to "[C]" as string
                set closingTagText to "[/C]" as string

                --set search and replace values
                set grepCode to "(" & openingTagGrep & ")(.*?)(" & closingTagGrep & ")" as string

                -- set color name and cmyk values
                -- the color name gets a random number attached to avoid naming conflicts
                set newColorName to "Cyan" & (random number from 1 to 1000000) as string
                set newColorValueC to 100 as integer
                set newColorValueM to 0 as integer
                set newColorValueY to 0 as integer
                set newColorValueK to 0 as integer



                -- FIND TAGGED TEXT AND APPLY COLOR

                --assign color to tagged text and delete tags
                set find grep preferences to nothing
                set change grep preferences to nothing

                set find what of find grep preferences to grepCode

                -- create the color in the application instead of in the document
                -- this is a workaround for a bug in indesign, which does not understand calls to colors in the document
                try
                set newColor to color newColorName
                on error
                set newColor to make color with properties {name:newColorName, color value:{newColorValueC, newColorValueM, newColorValueY, newColorValueK}}
                end try

                set fill color of change grep preferences to newColor

                tell theDocument
                change grep
                end tell

                -- delete the color from the application to keep the swatch list clean
                try
                delete newColor
                end try

                set find grep preferences to nothing
                set change grep preferences to nothing


                -- DELETE TAGS

                --delete the opening tags
                set find text preferences to nothing
                set change text preferences to nothing

                set change to of change text preferences to ""

                set find what of find text preferences to openingTagText
                tell theDocument
                change text
                end tell

                set find what of find text preferences to closingTagText
                tell theDocument
                change text
                end tell

                set find text preferences to nothing
                set change text preferences to nothing


                end tell

                -- END COLORIZE TAGGED TEXT
                -----------------------