wait for a process in OS X

Ken Ray kray at sonsothunder.com
Thu Jun 7 16:25:11 EDT 2007


On Thu, 7 Jun 2007 13:11:10 -0600, Chris Sheffield wrote:

> This is probably a stupid question. And I swear I've done this in the 
> past, but I can't for the life of me remember how I did it.
> 
> I need to launch an application from within a script, but I need my 
> script to wait for the application to close before continuing. I've 
> tried launch, open process, and even the shell() function, but in 
> each case the script just continues running. How can I do this? Is my 
> only option to launch it, then wait in a loop and continually check 
> the running processes using shell()? If so, I suppose that's fine. 
> Just kind of a hassle.

Well "open process" is probably better than the shell - open the 
process (*not* "for neither"), and then go into a loop where you check 
"the openProcesses" as soon as the app quits, the openProcesses will be 
empty and you can move on. I'd put in a small "wait with messages" in 
the loop just to make things a bit cleaner:

open process "MyApp"
repeat 
  wait 100 milliseconds with messages
  if the openProcesses is empty then exit repeat
end repeat

of course, you may want to insert a "bail out" condition after a 
certain amount of time waiting - here's an example that waits for 2 
minutes for the launched app to quit before reporting an error:

put the milliseconds into tMS
open process "MyApp"
repeat 
  wait 100 milliseconds with messages
  if the openProcesses is empty then 
    put "Success" into tResult
    exit repeat
  end if
  if (the milliseconds - tMS) > (2*60*1000) then
    put "Bailed" into tResult
    exit repeat
  end if
end repeat
if tResult is "Bailed" then
  answer warning "Timed out waiting for the app to close."
  exit to top
end if

(or something like that...)


Ken Ray
Sons of Thunder Software, Inc.
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/



More information about the use-livecode mailing list