I’ve searched, and not found an answer to this problem - though there are answers to similar ones.
I’ve got an Applescript studio app that, when a button is pressed, needs to:
Perform some applescript tasks
THEN
Run tell Terminal to run a “do script”
THEN
After the do script has finished run some more applescript tasks
THEN
Run another “do script”
The script in 4 is dependent on the tasks in 3 having been completed.
Ideally, a solution that worked both for "do script"s opening up in Terminal AND "do shell script"s that run silently while the app waits would be desired.
Any idea if this is possible? It’s driving me nuts!
I’m not sure from your description if this will help, but it sounds like you need to wrap your script in a considering application responses block:
considering application responses
(*1. Perform some applescript tasks
THEN
2. Run tell Terminal to run a "do script"
THEN
3. After the do script has finished run some more applescript tasks
THEN
4. Run another "do script"*)
end condsidering
considering application responses causes AppleScript to wait until it hears back from the application. Whether it works for you in this context is dependent on what you’re doing, but I’d try it.
hmm, correct me if I am wrong … but as far as i know, ‘considering …’ has no influence on Terminal’s behaviour to always return after a ‘do script’ instantly.
Anyway - if it works - good. If not - here’s a possible workaround:
tell application "Terminal"
-- make a new window:
do script ""
-- store reference to this window
set newWin to first window whose frontmost is true
-- run the script (lsof as an example for a lengthy task) + a comman to close the window after finishing:
do script "lsof; osascript -l AppleScript -e " & quoted form of ("tell application \"Terminal\" to close (first window whose frontmost is true)") in newWin
-- wait until the window is gone:
repeat while (exists newWin)
end repeat
end tell
-- should procede here after 'do script is done:
display dialog "done"
I thought of the busy solution before but it didn’t/and still does not work for me. Probably because after logging in/opening a new terminal window it is not busy for a certain (short) amount of time. For me a ‘delay 1’ was needed to make this work - and i am not certain if it works in any case - for people using large login scripts it might fail.