Previous Page
Next Page

1.6. Combining Specialties

Different applications are good at different things. Most users don't perform all tasks in a single application. For example, in a word processor, you wouldn't expect to perform extensive editing of pictures: a document might include pictures, but you'd create and edit them in some other program, and then incorporate them into the word processing document. That's how it should be, and that's how users typically like it, especially on Mac OS X where (in contrast to previous systems) there is no significant penalty to running several applications at the same time. "Swiss Army knife" programs that try to be all things to all users generally seem bloated with unnecessary features (such as Microsoft Word, with its Photoshop-like "graphics enhancement" features).

When it comes to assisting applications to combine their separate specialties, AppleScript really shines. Thanks to AppleScript, data can be moved back and forth between applications so that each can operate upon it in the appropriate manner. The result is a workflow in which multiple applications are coordinated, often without the intervention or even the awareness of the user.

Take, for example, SpamSieve . This superb application uses Bayesian algorithms to distinguish between spam and nonspam email messages with astonishing accuracyfar better than those email client programs, such as Entourage and Apple Mail, that include spam filtering of their own. But SpamSieve is not itself an email client. So in order to filter out spam as your email client application receives it, that email client application and SpamSieve must cooperate. The email client receives the mail messages, and hands them over to SpamSieve for evaluation; if SpamSieve marks a message as spam, it tells the email client application, which can then take appropriate action (such as moving the message into a Spam folder). Whenever you check for new mail messages, the two-way communication between your email client and SpamSieve takes place automatically, seamlessly, swiftly, invisiblyand entirely through AppleScript. Thus AppleScript effectively incorporates SpamSieve's brain and its special kind of intelligence into your email application; the two specialties (receiving email messages and storing them in folders, on the one hand, and knowing what is spam and what isn't, on the other) are combined.

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.

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.


Previous Page
Next Page