5 Replies Latest reply on Apr 2, 2011 6:12 AM by Muppet Mark-QAl63s

    [AS] Find a string of words in text

    mikeytttt

      Hello - hopefully someone can help as I'm now stuck on this. I want to be able to find a specific string of words within a text frame, though I want to be able to do this without using the Find Text Preferences approach.

       

      I can get it to work if i am just finding one word, but if there is more than one word it doesn't work.

       

      For example this works (given a text frame with text in that contains the word Example):

       

      tell application "Adobe Indesign CS5"

      tell active document

      set mybox to selected

        tell mybox

         set myfind to object reference of every word whose contents = "Example"

        end tell

      end tell

      end tell

       

       

      However the same does not work (searching for "The Example", which is two words)

       

      tell application "Adobe Indesign CS5"

      tell active document

      set mybox to selected

        tell mybox

         set myfind to object reference of every word whose contents = "The Example"

        end tell

      end tell

      end tell

       

       

      What i can't figure out is the correct phrasing for finding a string of words, i guess i should not be looking for the object reference of WORD, but nothing else i've tried seems to work. Many thanks for any help

        • 1. Re: [AS] Find a string of words in text
          John Hawkinson Level 5

          For performance reasons, you are probably better off leveraging the FindChange to find your words.

          But in this case, you want to look for lines, paragraphs, or stories that contain your substring. You can't test for equality, since an entire line is (probably) not equal to your substring. You have to test to see if those larger units contain the substring you are looking for.

          • 2. Re: [AS] Find a string of words in text
            Muppet Mark-QAl63s Level 4

            I think what John has said is correct… I only had a quick look in the dictionary terms. Without find/change the closest you may get is line or paragraph after that you would need to manipulate the line/para contents further… Word by definition I would think can't contain space but is encapsulated in space bar first/last…

             

            tell application "Adobe InDesign CS5"

            tell active document

            tell story 1

            set myfind to object reference of every word whose contents = "Example"

            end tell

            show text first item of myfind

            end tell

            end tell

             

            This would show the line you can then break down further…

             

            tell application "Adobe InDesign CS5"

            tell active document

            tell story 1

            set myfind to object reference of every line whose contents contains "For Example"

            end tell

            show text first item of myfind

            end tell

            end tell

            1 person found this helpful
            • 3. Re: [AS] Find a string of words in text
              mikeytttt Level 1

              Yes, it seems like that is the only way to do it.

               

              I was really asking as in Quark scripting you can specify searching for string of words by saying something along the lines of

               

              ...every text whose is "The Example"

               

              but there is not an option to find a string in such an simple way in InDesign.

               

              Thanks for all your help.

              • 4. Re: [AS] Find a string of words in text
                Mayhem SWE Level 2

                You could use the offset command in the AppleScript Standard Additions. Some experimenting gave me this:

                 

                findStringInString("second", "first second third")
                
                on findStringInString(findWhat, findWhere)
                     set indexStart to offset of findWhat in findWhere
                     if indexStart is 0 then
                          return null
                     else
                          set indexEnd to indexStart + (count of findWhat) - 1
                          return a reference to characters from indexStart to indexEnd of findWhere
                     end if
                end findStringInString
                

                 

                Of course, the offset command will only operate on AppleScript strings. To get it working with InDesign text references, you'd have to extract the contents, find the desired range of characters, then create a new reference back to the original target. Something like this:

                 

                tell application id "com.adobe.InDesign"
                     -- select a text frame in InDesign whose contents include the string "find"
                     set findResult to my findStringInTarget("find", a reference to first item of selection)
                     if findResult is not null then
                          -- if the string is found, replace it with "replace"
                          set contents of findResult to "replace"
                     end if
                end tell
                
                on findStringInTarget(findWhat, findWhere)
                     tell application id "com.adobe.InDesign"
                          set findWhereContent to contents of first text of findWhere
                          set indexStart to offset of findWhat in findWhereContent
                          if indexStart is 0 then
                               return null
                          else
                               set indexEnd to indexStart + (count of findWhat) - 1
                               return object reference of text from character indexStart to character indexEnd of findWhere
                          end if
                     end tell
                end findStringInTarget
                

                 

                While this was kind of an entertaining exercise, prolonged exposure to AppleScript only results in frustration. It seems easy at first glance, but as soon as you try to do something complicated the weirdnesses just start piling up. Please do your own sanity a favor and switch to JavaScript!

                1 person found this helpful
                • 5. Re: [AS] Find a string of words in text
                  Muppet Mark-QAl63s Level 4

                  I try to use both but AppleScript's biggest disappointment is it's lack of a native regular expressions… Hated JavaScript at first but I am starting to warm to it now…