After opening multiple terminal windows and running commands in them, it becomes difficult to send another command to a specific terminal window.

How can we set an alias for each terminal window so we can specifically target it later? Heres a code snippet of what I am trying to do (apparently the code doesn't work)

tell application "Terminal"


    do script "./start-1.sh" in window 1
    // then set an alias for this window: 'start1'

    do script "./start-2.sh" in window 2
    // then set an alias for this window: 'start2'

    do script "/monitor.sh" in window 3
    // then set an alias for this window 'monitor'

    // send command to a specific window using its alias
    do script "shutdown -n 0" in window 'monitor'

end tell

What you really need to save are the tabs—those can be used again in a do script action (even if they are just one per window).

This script:

tell application "Terminal"
    set tab1 to (do script "echo foo")
    do script "echo bar" in tab1
    do script "echo baz" -- in new window
    do script "echo back again" in tab1
end tell

will produce this in one window:

$ echo foo
$ echo bar
$ echo back again
back again

and this in another:

$ echo baz

One non-applescript solution to your problem may be to open the Terminal Preferences... select Settings. Then, under the Window tab select the Command Key check box. This will add a command key to the title bar to instantly switch to that window (so Window titles would have ⌘1, ⌘2, etc. appended to the window title.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .