Section 13.4. Running AppleScripts from Unix


13.4. Running AppleScripts from Unix

So far in this chapter, you've spent a lot of time running Unix programs from AppleScript. You've commanded Terminal directly, and even run Unix programs in the background with do shell script. What you haven't done, however, is run AppleScript commands from Unix.

If you're new to the world of Unix, you might not understand how useful this feature can be. Nevertheless, this AppleScript-from-Unix bridge can save you time in several ways:

  • If you run an AppleScript in Terminal, you don't have to launch Script Editor. Speed freaks, take note.

  • You can mix your AppleScript commands with other Unix programs very easily. For instance, you could present a dialog box (using AppleScript), and thendepending on what you typed in the dialog boxrun one of several Unix programs.

  • You can send AppleScript commands over a network. Using a "Secure Shell" with the ssh command, Unix lets you run programs on other computers on your networkor anywhere in the world. You can also take advantage of this feature to send AppleScript commands to remote computers. See www.macdevcenter.com/pub/a/mac/2004/07/09/inside_ssh_pt1.html for details on using ssh.

The key to all these tricks is the Unix program osascript. You provide it the path of a script to run, and Unix obediently runs that script, like so:

osascript "/Library/Scripts/Finder Scripts/Switch to Finder.scpt"

That, of course, would run the script from Library Scripts Finder Scripts Switch to Finder.scpt (which brings the Finder forward and hides the windows from other programs).

You're not limited to just running script files, however; you can run individual script commands as well. All you have to do is append -e to osascript, and follow up with the AppleScript command you want to run. For example, you could type this in Terminal if you wanted to display a dialog box in the Finder:

osascript -e 'tell application "Finder" to display dialog "Quail Eggs"'

When using osascript, you should surround the entire AppleScript command in single-quotes, but all the AppleScript strings should be in double-quotes, as shown in the previous code.

You can even string together several AppleScript commands with osascript, by using more than one -e flag (make sure you type the following text all on one line):

osascript -e 'tell application "TextEdit"' -e 'activate' -e 'make new window at  front' -e 'end tell'

That, of course, would be the equivalent of running this script in Script Editor:

tell application "TextEdit"     activate     make new window at front end tell

A big advantage to running your AppleScript commands in Terminal, of course, is that you don't have to launch Script Editor. But that's only the beginning; you can integrate osascript with other Unix programs, as explained on the following pages.



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net