2.6. UnixMac OS X, under the hood, is Unix . It is possible to use AppleScript from the Unix command line in the Terminal and from shell-related environments such as Perl and Ruby scripts, by means of the osascript command. osascript can execute a compiled script file or can compile and execute a string (indicated by the -e switch). You can enter script text directly at the command line by typing osascript and a return character, then typing the text, and finally signalling the end of the text with Control-D. There isn't much likelihood you'd want to do this, but at least it proves that osascript is working, and the code looks exactly like normal AppleScript: % osascript -ss tell app "Finder" get name of every disk end tell ^D {"feathers", "gromit", "Network"} (The -ss flag causes the result to appear in the familiar way that AppleScript usually formats a list of strings.) Use of a literal string on the command line raises some difficulties of escaping characters parallel to those we've seen earlier in this chapter; there are various solutions, depending on what shell you're using. In a language such as Perl, you can take advantage of the language's "here document " facility, which makes it easy to enter a multiple-line script without having to escape any quotes. Once again, the code looks exactly like normal AppleScript: #!/usr/bin/perl $s = <<DONE; tell app "Finder" get name of every disk end DONE print `osascript -e '$s'`; Chapter 25 contains full details about calling AppleScript from Unix, as well as communication in the reverse direction (through AppleScript's do shell script command). |