AppleScript_ The Definitive Guide - Matt Neuburg [221]
osacompile takes as argument a text file, or some text provided on the command line, and generates a compiled script file or applet. For example:
% cat > textfile.txt
tell app "Finder"
display dialog "Hello, world!"
end
^D
%osacompile -o compiledfile.scpt textfile.txt
The result is a compiled script file compiledfile.scpt that can be opened in Script Editor, executed by a script runner, and so forth. You can avoid the intermediate text file by including the script text as part of the osacompile command; the ensuing discussion of osascript shows how. The extension on the filename supplied in the -o parameter determines the type of file that's created; the .scptd extension makes a script bundle, the .app extension makes an applet bundle, and otherwise you get a compiled script file. Further switches let you determine which fork the compiled script bytecode is saved into (the default is the resource fork; see "Compiled Script File Formats" in Chapter 3) and the characteristics of a created applet (see "Applet Options" in Chapter 27). Consult the osacompile man pages for more information.
The osascript command executes a compiled script file, a text file, or text provided from the command line. This command is the real key to bridging the gap between Unix and AppleScript from the Unix side.
Here's how to execute a text file or a compiled script file (these are the same files we made earlier in connection with osacompile):
% osascript textfile.txt
%osascript compiledfile.scpt
You can enter the script text manually after the osascript command:
% osascript
tell app "Finder"
display dialog "Hello, world!"
end
^D
To include the script text on the command line, use the -e switch. The shell's usual quotational hoops will have to be jumped through; the bash shell helps by permitting a literal string to be entered in ANSI-C form:
% osascript -e $'tell app "Finder"\rdisplay dialog "Hello, world!"\rend'
Another approach to entering a multiple-line script is to repeat the -e switch multiple times. For example:
% osascript -e 'tell app "Finder"' -e 'display dialog "Hello, world!"' -e 'end'
The result of osascript is formatted differently depending on whether you supply the -ss flag. If you include it, delimiters are used, as in the Script Editor, to show the nature of the result. If you omit it, a list of strings is flattened into a series of comma-delimited tokens:
% osascript -ss -e 'tell app "Finder" to get name of every disk'
{"feathers", "gromit", "Network"}
% osascript -e 'tell app "Finder" to get name of every disk'
feathers, gromit, Network
The same list flattening extends to a deeper level:
% osascript -e '{"Mannie", {"Moe"}}'
Mannie, Moe
Such a flattened, unquoted list does have its uses, especially in conjunction with other Unix tools. In this example (suggested to me by Chris Nebel), I find all persons who appear more than once in my Address Book:
% osascript -e 'tell app "Address Book" to get name of every person'
| tr, "\n" | sort -bf -k2 | uniq -d
Mark Anbinder
Mary Byrd
Jeff Carlson[and so on]
The line-break character represented by the keyword return is a Macintosh line-break character (\r), which can confuse the display in a Unix context. This is purely a cosmetic issue. For example (in the Terminal):
% osascript -e 'set pep to "Manny" & return & "Moe"'
Moeny
% osascript -e 'set pep to "Manny" & (ASCII character 10) & "Moe"'
Manny
Moe
What happened in the first reply is that "Moe" overprinted "Manny". If you expect that Macintosh line breaks will appear in the output, you can pipe it through tr:
% osascript -e 'set pep to "Manny" & return & "Moe"' | tr "\r" "\n"
Manny