Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 254
Petruza Avatar asked Dec 08 '09 22:12

Petruza


People also ask

Does Apple still use AppleScript?

AppleScript is a scripting language created by Apple Inc. that facilitates automated control over scriptable Mac applications. First introduced in System 7, it is currently included in all versions of macOS as part of a package of system automation tools.

Does AppleScript have script commands?

In AppleScript, the do shell script command is used to execute command-line tools. This command is implemented by the Standard Additions scripting addition included with OS X. The Terminal app in /Applications/Utilities/ is scriptable and provides another way to execute command-line tools from scripts.


1 Answers

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 
like image 150
CRP Avatar answered Sep 21 '22 23:09

CRP