Sending commands and strings to Terminal.app with Applescript

I want to do something like this:

tell application "Terminal"
  activate
  do script "ssh [email protected]"
  -- // write user's password
  -- // write some linux commands to remote server
end tell

For example to log in to the server, enter the password, and then login to mysql and select a DB.
I type that every day and it would be really helpful to bundle it into a script.

Also, is there a reference of what commands, properties, functions, etc. do applications (Terminal, Finder, etc) have available to use within Applescript? thanks!

EDIT: Let me clear this up: I don't want to do several 'do script' as I tried and doesn't work. I want to open a Terminal window, and then emulate a human typing in some characters and hitting enter. Could be passwords, could be commands, whatever, just sending chars to the Terminal which happens to be running ssh. I tried keystroke and doesn't seem to work.


First connect to the server and wait for 6 seconds (you can change that) and then execute whatever you need on the remote server using the same tab

tell application "Terminal"
   set currentTab to do script ("ssh user@server;")
   delay 6
   do script ("do something remote") in currentTab
end tell

As EvanK stated each do script line will open a new window however you can run
two commands with the same do script by separating them with a semicolon. For example:

tell application "Terminal"
    do script "date;time"
end tell

But the limit appears to be two commands.

However, you can append "in window 1" to the do script command (for every do script after the first one) to get the same effect and continue to run as many commands as you need to in the same window:

tell application "Terminal"
    do script "date"
    do script "time" in window 1
    do script "who" in window 1
end tell


Note that I just used the who, date, and time command as an example...replace with whatever commands you need.


Here's another way, but with the advantage that it launches Terminal, brings it to the front, and creates only one window.

I like this when I want to be neatly presented with the results of my script.

tell application "Terminal"
    activate
    set shell to do script "echo 1" in window 1
    do script "echo 2" in shell
    do script "echo 3" in shell
end tell

How about this? There's no need for key codes (at least in Lion, not sure about earlier), and a subroutine simplifies the main script.

The below script will ssh to localhost as user "me", enter password "myPassw0rd" after a 1 second delay, issue ls, delay 2 seconds, and then exit.

tell application "Terminal"
    activate
    my execCmd("ssh me@localhost", 1)
    my execCmd("myPassw0rd", 0)
    my execCmd("ls", 2)
    my execCmd("exit", 0)
end tell
on execCmd(cmd, pause)
    tell application "System Events"
        tell application process "Terminal"
            set frontmost to true
            keystroke cmd
            keystroke return
        end tell
    end tell
    delay pause
end execCmd

You don't need to "tell" Terminal to do anything. AppleScript can do shell scripts directly.

set theDir to "~/Desktop/"
do shell script "touch " & theDir &"SomeFile.txt"

or whatever ...