Sending commands and strings to Terminal.app with Applescript

Petruza picture Petruza · Dec 8, 2009 · Viewed 140.5k times · Source

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.

Answer

CRP picture CRP · Sep 7, 2010

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