7 Replies Latest reply on Feb 9, 2003 6:02 AM by (Dave_Saunders)

    Dealing with failed search

    Level 1
      What is the best way to detect that a search has failed to find anything?

      I've been using:

      set find preferences to nothing
      set change preferences to nothing
      repeat
      set myResult to search myStory for "^p^p" replacing with "^p"
      try
      get myResult
      on error
      exit repeat
      end try
      end repeat

      But I'm sometime crashing ID 2.0.2 using this technique (although I can't reproduce it in trivial documents).

      The problem is that you can't test myResult because it becomes undefined if nothing is found.
        • 1. Re: Dealing with failed search
          Level 1
          I still am unable to reproduce this problem with a simple document, but having imported a four page rtf document and then running:

          tell application "InDesign 2.0.2"
          set find preferences to nothing
          set change preferences to nothiing
          set mystory to parent story of selection
          search mystory for "^p" replacing with "^p^p"
          search mystory for " ^p" replacing with "^p"
          search mystory for "^p^p" replacing with "^p"
          end tell

          I get a crash every time on the third search. (Would that the first and third were unnecessary, but the styles get screwed up if you don't do that.)

          Dave
          • 2. Re: Dealing with failed search
            Level 1
            One more point of clarification: the third search does not fail if I comment out the first two. It only fails if it follows the previous two. Note also that in the document in question, the second one actually does nothing because there are no instances of " ^p".

            Dave
            • 3. Re: Dealing with failed search
              Level 1
              Does setting the fill color of Find Preferences work? It doesn't for me in this sequence:

              set find preferences to nothing
              set change preferences to nothing
              set point size of find preferences to 16
              set fill color of find preferences to swatch "[Black]" of document 1
              set applied paragraph style of change preferences to paragraph style of "LSTITLE" of document 1
              search myStory for "" replacing with ""

              This is changing all the 16 point text to LSTITLE, regardless of color.

              Dave
              • 4. Re: Dealing with failed search
                Level 1
                I just realized I've not been using the correct syntax!

                It should be 'search in myStory" not just 'search myStory'

                However, that doesn't change the crash problem.

                Dave
                • 5. Re: Dealing with failed search
                  Dave --

                  I haven't had a problem w/ system crashes, but I have experienced inconsistent results if I try to string several searches one-after-the-other in a script. I eventually broke them out into separate scripts, & manually run them, w/ no problem. A little more hassle, but trying to de-bug the sucker was killing me.

                  I don't know if you can get a separate script to run the scripts?

                  Sorry I don't have anything better, but I felt sorry for you, sitting in your corner talking to yourself. [grin]

                  Jay
                  • 6. Re: Dealing with failed search
                    Level 1
                    Jay,

                    Turns out that use a single list of Find Attributes as part of the Search statement is superior to setting the individual preferences.

                    Mind you, I found this out after I had finagled the preferences approach into working for me.

                    Thanks for caring.

                    Dave
                    • 7. Re: Dealing with failed search
                      Level 1
                      As an update, I've discovered that after importing said four-page rtf file (I actually have 30-some to process), running:

                      search in myStory for "^p^p" replacing with "^p"

                      crashes ID 2 every time. So, it occurred to me that I could nonetheless do this:

                      repeat
                      set myResult to search in myStory for "^p^p"
                      try
                      get myResult
                      on error
                      -- there weren't any
                      exit repeat
                      end try
                      repeat with n from count of myResult to 1 by -1
                      set contents of item n of myResult to return
                      end repeat
                      end repeat

                      Dave