AppleScript_ The Definitive Guide - Matt Neuburg [12]
An interesting variation on the theme of combining specialities arises when one of the specialized applications isn't on your computer. This is feasible because AppleScript can send messages to remote applications (for details, see Chapter 23). For this example , we'll have the remote application be a web service. AppleScript supplies a built-in way to talk to a web service implementing an XML-RPC or SOAP interface.
Tip
A good clearinghouse for finding and exploring SOAP-enabled web services is http://www.xmethods.net.
Suppose that in creating an email message, we'd like to append a random quotation in place of our normal signature. There are random quotation generators on the Internet, so we can incorporate one of these into a script that creates an email message. The example uses Entourage to create the email message. (I wanted to use Apple's Mail application, but at the time of this writing, scripting of signature creation and modification was hopelessly broken.)
try
tell application "http://www.boyzoid.com/comp/randomQuote.cfc"
set returnValue to call soap ¬
{method name:"GetQuote", parameters:{HTMLformat:false}}
end tell
set L to |item| of returnValue
repeat with anItem in L
if |key| of anItem is "AUTHOR" then
set auth to value of anItem
end if
if |key| of anItem is "QUOTE" then
set quot to value of anItem
end if
end repeat
set s to "-- " & return & quot & return & " -- " & auth
on error what
set s to "No signature today, sorry."
end try
tell application "Microsoft Entourage"
set sig to signature id 1
set oldSig to (content of sig)
set content of sig to s
tell (make new draft window)
set signature type to other
set other signature choice to sig
end tell
set content of sig to oldSig
end tell
The script starts by calling a web service to generate a random quote. The result comes back as a record containing a list of two records (see Chapter 13); we then parse that structure to generate a signature. If anything goes wrong, there's no harm done; a dummy signature is used instead. A new Entourage outgoing message must use an existing signature, so we modify an existing signature, create the new message and apply the modified signature, and finally restore the signature's original text. Figure 1-1 shows the result of running the script.
Figure 1-1. Mail message with a quotation supplied by a web service
For yet another example of combining specialties, see "Internally Scriptable Application" in Chapter 2, where a database supplies the email address for a new message in an email client program.
Chapter 2. Where to Use AppleScript
AppleScript, because it is implemented at system level, is omnipresent. Nevertheless, you do not use AppleScript at just any time and any place. AppleScript, like Archimedes' lever, may permit you to move the earth; but first, like Archimedes, you need a place to stand.
The various contexts and milieus in which AppleScript code can be executed may be conveniently grouped into a few general categories; this chapter presents these categories, along with some examples. In other words, this chapter describes the main places where you can use AppleScript. You'll discover that AppleScript is lurking, ready and at your command, in many corners of your computer.
The taxonomy presented here is somewhat artificial, and my names for the various kinds of context in which AppleScript can be used are mostly made up, but I don't think this