Waiting for an Application to launch

I use an AppleScript to implement “Open Terminal Here” functionality on my Macs, and just now I noticed on the Intel machine it had stopped working properly if Terminal wasn’t already running. For some reason, on PPC it still sends the message through, but on x86 it doesn’t. So, I had to use the following code to get it to work if Terminal wasn’t already running, which can be generalised to any application: repeat while "Terminal" is not in name of processes     delay 0.5 end repeat The whole script, which can be saved as an application bundle, set to not show in dock, and then placed in the Toolbar:

 1     -- when the toolbar script icon is clicked
 2     --
 3     on run
 4      tell application "Finder"
 5          activate
 6          try
 7              set this_folder to (the target of the front window) as alias
 8              --display dialog POSIX path of this_folder
 9          on error
10              set this_folder to startup disk as alias
11          end try
12          my process_item(this_folder)
13      end tell
14     end run
15     
16     
17     
18     -- This handler processes folders dropped onto the toolbar script icon
19     --
20     on open these_items
21      repeat with i from 1 to the count of these_items
22          set this_item to item i of these_items
23          my process_item(this_item)
24      end repeat
25     end open
26     
27     
28     -- this subroutine processes does the actual work
29     --
30     on process_item(this_item)
31      tell application "System Events"
32          try
33              get process "Terminal"
34              
35              tell application "Terminal"
36                  activate
37                  do script "cd " & (quoted form of POSIX path of this_item)
38              end tell
39          on error -- Terminal Not running, launch and run in first window.
40              launch application "Terminal"
41              -- May need to wait until Terminal finishes launching
42              repeat while "Terminal" is not in name of processes
43                  delay 0.5
44              end repeat
45              tell application "Terminal"
46                  activate
47                  -- So we don't create a new window: in window 1
48                  do script "cd " & (quoted form of POSIX path of this_item) in window 1
49              end tell
50          end try
51      end tell
52     end process_item
53