Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [211]

By Root 1547 0
Your AppleScript command is translated into an Apple event, which in turn is translated into XML. The XML is shoved into the post argument of an HTTP request, and the request is sent across the Internet. (Your script waits synchronously for a reply.) When the reply comes back, the XML is extracted from the reply and interpreted as AppleScript data (usually a record)—and that's the result of your command. You specify the server in a tell block, but instead of saying the name of an application, you provide a URL string. Within the tell block, you use one of two commands: either the call xmlrpc command or the call soap command. (These terms are resolved only when the target is an http URL; otherwise, they are illegal.)

The target can be expressed in this form:

tell application "http://www.xxx.com/someApplication"

or you can say this, which decompiles to the previous syntax:

tell application "someApplication" of machine "http://www.xxx.com"

The syntax for call xmlrpc is as follows:

call xmlrpc {method name: methodName, parameters: parameterList}

The syntax for call soap is as follows:

call soap {method name: methodName, ¬

method namespace uri: uri, ¬

parameters: parameterRecord, ¬

SOAPAction: action}

You can omit an item of the parameter record (such as method namespace uri or parameters) if it isn't applicable.

Now let's test these commands. There's a copy of UserLand Frontier on the Internet that is intended for users to test with XML-RPC and SOAP requests. By default, we access this server's XML-RPC functionality through a URL whose path is /rpc2. The server includes some simple test verbs, one of which is examples.getStateName. We can call this verb using AppleScript, as follows:

tell application "http://superhonker.userland.com/rpc2"

call xmlrpc ¬

{method name:"examples.getStateName", ¬

parameters:30} -- "New Jersey"

end tell

Frontier is also a SOAP server. We can call the SOAP equivalent of the same verb using AppleScript, as follows:

tell application "http://superhonker.userland.com"

call soap ¬

{method name:"getStateName", ¬

SOAPAction:"/examples", ¬

parameters:{statenum:30}} -- "New Jersey"

end tell

If you happen to have a copy of Frontier or Radio UserLand, you can test all this on your own machine, without using the Internet. These programs run the very same server on port 8080. So with Frontier or Radio UserLand running, you can substitute "http://localhost:8080/rpc2" and "http://localhost:8080" as the application URLs for the tell block.

If you tell AppleScript to look for a dictionary in an application that is a web URL, AppleScript won't actually look there, but will assume that this application is a SOAP or XML-RPC server. We can use this as a trick to treat the target as a variable when doing a SOAP call over the Internet. This example, based on a script distributed by Apple, shows how to write a general SOAP-calling handler. The handler generalSOAP contains no hard-coded information at all, except the application URL named in the terms block—but this URL is a fake, merely to satisfy the compiler that the call soap command is legal. In the last line of this script, we test our general SOAP-calling handler by handing it some parameters; in this case, we are fetching the current Apple stock price over the Internet:

on generalSOAP(u, m, s, a, p)

using terms from application "http://www.apple.com/placebo"

tell application u

return call soap ¬

{method name:m, ¬

method namespace uri:s, ¬

parameters:p, ¬

SOAPAction:a}

end tell

end using terms from

end generalSOAP

generalSOAP("http://services.xmethods.net:80/soap", ¬

"getQuote", "urn:xmethods-delayed-quotes", ¬

"", {Symbol:"AAPL"}) -- 74.93

For another example of call soap, see "Combining Specialties" in Chapter 1. In general, call xmlrpc and call soap are not difficult to use, but you'll have to study the documentation for the service you're trying to call, and it may take a little trial and error to get the parameters just right.

Warning

Some SOAP servers are not compatible with the XML syntax used by the Apple

Return Main Page Previous Page Next Page

®Online Book Reader