AppleScript_ The Definitive Guide - Matt Neuburg [21]
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 = < 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). Hyperlinks The mechanism involved is the applescript URL protocol. The href attribute of the link's tag must begin like this: applescript://com.apple.scripteditor? The specification of Script Editor's bundle identifier is apparently a security measure; it is required, but it is also superfluous, because applescript URLs cannot be made to target any other script editor application by changing this value. Tip applescript URLs can be made to target a desired application (or applet) by means of a preference set by the user at system level. Apple provides no interface for setting this preference; but the freeware RCDefaultApp preference pane is an excellent way to do it (http://www.rubicode.com/Software/RCDefaultApp/ ). The next component of the URL is one of the following three expressions: action=new& action=insert& action=append& They signify, respectively, that AppleScript code should be inserted in a new Script Editor window, placed at the insertion point in the currently frontmost Script Editor window, or appended to the end of the currently frontmost Script Editor window. (If no Script Editor window is currently open, all three have the same effect.) Finally, the AppleScript code itself appears, in this format: script=theCode As this is a URL, illegal characters in theCode must be URL-encoded using a percent sign and the character's ASCII value in hexadecimal; for example, a space must be encoded as %20, a quote must be encoded as %22, and a return character must be encoded as %0D. At http://www.apple.com/applescript/scripteditor/12.html, Apple provides a utility script that URL-encodes text that has been copied to the clipboard, and embeds it in an applescript protocol tag. Naturally, the script is provided as a link that uses, itself, the applescript protocol! Thus, for example, this script: tell application "Finder" get name of every disk end tell could be included in a web page as a link from the words "click me" using the following HTML (ignore the line breaks and other formatting, which are used here for clarity but would not be present in real life):
You can embed AppleScript code in an HTML hyperlink . The user can't actually execute AppleScript code by clicking such a link (the ability to do so would constitute a serious security hole). Rather, when the user clicks that link, the code is displayed in a script editor, ready to execute if the user desires.