Macos – How to automatically start two SSH sessions in two Terminal tabs

macosterminal.app

In my Mac, how do I write a script that can do this?

  1. Open "terminal".
  2. In that terminal, "ssh myserver"
  3. Open a new "tab" inside the terminal.
  4. In that tab, "ssh myserver2"

Then, how would I run this script?

Best Answer

Open /Applications/Utilities/AppleScript Editor.app and enter the following:

tell application "Terminal"
    activate
    tell application "System Events"
        keystroke "t" using command down # new tab
        keystroke "ssh myserver"
        key code 36 # press enter
        keystroke "t" using command down # new tab
        keystroke "ssh myserver2"
        key code 36 # press enter
    end tell
end tell

Save as script or application. Anytime you execute, Terminal will be brought to front (started if necessary), and two new tabs will be created for your SSH sessions.

In this answer I outlined how to check whether a window is already open (without an application running) to prevent opening one tab/window too many.