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.
The mechanism involved is the applescript URL protocol. The href attribute of the link's <a> tag must begin like this:
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.
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:
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 <a> 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):
<a href="applescript://com.apple.scripteditor?action=new&script= tell%20application%20%22Finder%22%0D %09get%20name%20of%20every%20disk%0D end%20tell">click me</a>
A web page is not the only place you can put an applescript link; certain other contexts will permit links that specify non-HTTP protocols. For instance, you can include such a link in a PDF document; this would allow the reader to click in the PDF in order to capture in the Script Editor some code that you have embedded into the link; this device is extensively used in the Take Control electronic book series as a way of letting the reader obtain and run a utility script with no need to copy and paste from the PDF book (see http://www.takecontrolbooks.com). Similarly, you can include such a link in a QuickTime movie (for an example, see http://brennan.young.net/Comp/LiveStage/GenerateASpath.html) and in various other contexts.