1 Reply Latest reply on Apr 9, 2009 2:44 AM by Ervin Bernhardt

    How to use AS to wait until process is finished?

    Ervin Bernhardt

      Hello,

       

      I have a little Applescript that adds page references to all items of Character style "index". I'm a complete beginner, so the script is ugly, but it almost works:

       

      ----

      tell application "Adobe InDesign CS3"
          activate
          set find what of find grep preferences to ".*"
          set applied character style of find grep preferences to "index"
          set change to of change grep preferences to nothing
          set myFoundItems to find grep
          set myCountItems to count myFoundItems
          display dialog ("Found " & myCountItems & " indexes.")
      end tell
      tell application "System Events"
          tell process "Adobe InDesign CS3"
              repeat with loopVar from 1 to myCountItems
                  keystroke "f" using {command down} -- open "Find"
                  keystroke return -- find next
                  keystroke "r" using {command down} -- add page reference
                  keystroke return -- OK
                  key code 53 -- Escape (closes "Find")
                  key code 51 -- Delete (deletes between <index> tags)
                  -- delay 2
              end repeat
          end tell
      end tell

      ----

       

      But only almost works. The problem is that always when InDesign starts thinking of something nice, the script "runs ahead" of it and deletes the entry in page reference instead, which results in "One or more required Topic Levels boxes are empty" popup. And the script goes nuts after that. Delay 2 helps, and delay 4 helps more, but besides slowing things down in unnecessary places, it also doesn't solve the problem.

       

      Is there a way to ask the script to wait until page reference is created, or to react to appearance of "One or more required Topic.." window (after which some undo's and stuff would do the trick)?

       

      BTW: I know that there is a "correct" way of adding page references by script ("set myTopic to make topic with data" etc.), but the problem with that is, that with more than 3000 index entries it mysteriously starts misplacing the markers. As far as I know, this problem has not been solved.

       

      Thanks for help, if somebody can

        • 1. Re: How to use AS to wait until process is finished?
          Ervin Bernhardt Level 1

          Sorry, I stumbled upon a solution. InDesign needs to be told to activate between each cycle. It's ugly, and could be done quicker for sure, but works well:

           

          ----

          tell application "Adobe InDesign CS3"
              set find what of find grep preferences to ".*"
              set applied character style of find grep preferences to "index"
              set change to of change grep preferences to nothing
              set myFoundItems to find grep
              set myCountItems to count myFoundItems
              set myTime to myCountItems / 60 as integer
              display dialog (("Found " & myCountItems & " indexes. Time spent will be about " & myTime) & " minutes. Sorry.")
          end tell
          repeat with loopVar from 1 to myCountItems
              tell application "Adobe InDesign CS3"
                  activate
                  set find what of find grep preferences to ".*"
                  set applied character style of find grep preferences to "index"
                  set change to of change grep preferences to nothing
              end tell
              tell application "System Events"
                  tell process "Adobe InDesign CS3"
                      keystroke "f" using {command down} -- open "Find"
                      keystroke return -- find next
                      keystroke "r" using {command down} -- add page reference
                      keystroke return -- OK
                      key code 53 -- Escape (closes "Find")
                      key code 51 -- Delete (deletes between <index> tags)
                  end tell
              end tell
          end repeat

          ----

           

          Thanks