Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have a script wait until the last script is complete

I have a script that has two apple scripts that open a new tab and do a command. I want the second apple script to wait until the first apple script's command line action is completed. I would rather not have to only to a sleep for x long, before it continues.

Is there a way that I can do this?

Here is what I have:

 osascript -e 'tell application "Terminal" to activate' \
 -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' \
 -e 'tell application "Terminal" to do script "cd '$current_dir'" in selected tab of the front window' \
 -e 'tell application "Terminal" to do script "./my_script arg1" in selected tab of the front window'

 ------ Wait until this process is finished -------

 osascript -e 'tell application "Terminal" to activate' \
 -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' \
 -e 'tell application "Terminal" to do script "cd '$current_dir'" in selected tab of the front window' \
 -e 'tell application "Terminal" to do script "./my_script different_arg1" in selected tab of the front window'
like image 476
SirRupertIII Avatar asked Jun 20 '13 14:06

SirRupertIII


1 Answers

You can check the busy property of a window or tab:

tell application "Terminal"
    set w to do script "sleep 1"
    repeat
        delay 0.1
        if not busy of w then exit repeat
    end repeat
end tell
osascript -e 'on run {a}
    tell application "Terminal"
        activate
        tell application "System Events" to keystroke "t" using command down
        do script "cd " & quoted form of a & "; sleep 1" in window 1
        repeat
            delay 0.1
            if not busy of window 1 then exit repeat
        end repeat
        tell application "System Events" to keystroke "t" using command down
        do script "cd " & quoted form of a & "; sleep 1" in window 1
    end tell
end run' /tmp
like image 139
Lri Avatar answered Sep 19 '22 14:09

Lri