|
|
Web Services with AppleScript and Perlby Randal L. Schwartz and Apple Developer Connection09/24/2002 Prior to Mac OS X, Perl scripters were forced to telnet to a shell prompt on a remote Unix box to get full access to their favorite language. But now, with its BSD underpinnings and a terminal window, Mac OS X has the same version of Perl you'd find on any Unix system. Of course, Apple's own scripting language, AppleScript, has been around for years. And a recent release of the language added the ability to use SOAP services, which makes it a nice complement to Perl. You can't create a SOAP web service with AppleScript, but you can have it act as a SOAP web client and use Perl to create the service. So it becomes a simple matter of passing structured data between Perl and AppleScript, with the Perl service running either on the same machine or remotely. This is much nicer than firing off a new Perl process with the A Perl-built SOAP web service also has full access to the Mac OS X platform from the Unix perspective and can make use of the thousands of pre-written routines from the Comprehensive Perl Archive Network (CPAN). In fact, a SOAP web service in Perl can even use other SOAP web services to repackage, filter, or summarize data. (Beware that a SOAP web service runs with the full permissions of the user who started the process. Think twice before letting people connect to your web service remotely. Think ten times before starting it as root or an admin user.) Creating a Simple ApplicationTo show off the connection between an AppleScript and a Perl server, I created a simple task to keep the mechanics to a minimum: fetching headlines from a news site. Sites that provide news items frequently include a Rich Site Summary (RSS) file with headlines and links to the detailed stories. These RSS feeds can then be used by other sites to provide a sidebar of relevant stories. The AppleScript can drive the process, but it can't fetch a URL directly or parse the resulting XML, so I delegated that to the Perl program. The AppleScript applet will give an RSS URL to the Perl program (running as a daemon) via the SOAP protocol, and the Perl script will take care of the fetching, RSS parsing, and repackaging of the essential information. Then the AppleScript will reformat the data as HTML and hand it to a browser to be viewed.
There are probably better ways of doing this. For example, it would be trivial to have Perl simply return the repackaged HTML, or put the HTML into a file and return the name of that temp file or even launch the browser directly. But this approach allows me to return the structured data in a nice way. And by keeping the Perl side to a minimum, AppleScripters can use what they know to provide alternate views of the data, letting the Perl hackers concentrate on fetching the data. |
|
|
|
|
||||||||