3 Replies Latest reply on Sep 5, 2010 9:55 PM by chris Harges

    Smart Find & Repalce

    chris Harges Level 1

      I'm trying to write a script that will work its way through a story and replace strings only after finding a trigger string earlier in the story.


      For example, if the script finds a word "AAAA" in the story, it will look for the next instance of the word "ZZZ" and replace it with with some text. Then it will look for "BBBB" and replace the "ZZZ" that follows that with different text."


      I can get a script to find "AAAA" in a story and get its object reference and then find "BBBB" and get its object reference. What I'd like to do is have the script now look for "ZZZ" only in the text between the object reference of "AAAA" and the object reference of "BBBB" because I know there is only one instance of "ZZZ" in that section of text.


      After setting the respective object references to the variables "firstMarker" and "secondMarker," I can't figure out how to do something like this:


      set myText to text from firstMarker to secondMarker

      Is this possible?
      Sample script follows:

      tell application "Adobe InDesign CS4"

           tell document 1

                tell story id 158319

                     set finderVar to ""

                     repeat with a from 5 to count of words

                          set finderVar to text of word a of text of it

                          set finderVar to finderVar as string

                               if finderVar = "AAAA" then

                                    set firstMarker to object reference of word a

                                    repeat with s from 1 to 500

                                         set endVar to text of word s of text of it

                                         set endVar to endVar as string

                                         if endVar = "BBBB" then

                                              set secondMarker to object reference of word s    

                                              set myText to text from firstMarker to secondMarker

                                              return myText

                                         end if

                                    end repeat

                               end if

                          display dialog finderVar

                     end repeat

                end tell

           end tell

      end tell

        • 1. Re: Smart Find & Repalce
          [Jongware] Most Valuable Participant

          Does Applescript have a property "index" for your found text? In JS, I can get a list of *all* "ZZZZ"s and only change the one with an index as close as (but not less) to the "AAAA" index I found.

          • 2. Re: Smart Find & Repalce



            I think simply use replace function can do it easily. e.g.


            story.replace("AAAA ZZZ", "AAAA TEST1");

            story.replace("BBBB ZZZ", "BBBB TEST2");


            you can combine with regular expression for advance search.




            • 3. Re: Smart Find & Repalce
              chris Harges Level 1

              The problem magic3000_chris, unless I'm missing something, is that there's an indeterminate amount of text between the key strings ("AAAA" etc.) and the find/replace strings ("ZZZ"). And I can't use a regular expression at least not in the way I could imagine it with a positive lookbehind (find "ZZZ" that follows an indeterminate amount of text and "AAAA") because the positive look behind expression won't allow for a variable-length wildcard.


              It does, though, look like Jongware's approach will work. Trying it now.