13.4. Running AppleScripts from UnixSo 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:
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
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"'
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. |