Last week in TidBITS-480, I looked at new capabilities in Mac OS 8.6; this week, I’d like to focus on one new feature I think deserves special attention: URL Access.
Low-Level Power — URL Access is a new system component that enables programs to transfer information to and from the Internet using HTTP or FTP. Around the time Apple introduced Game Sprockets in 1996, several Macintosh Internet developers pushed the idea of "Apple Internet Sprockets" which would give any application basic Internet connectivity. Why is this idea interesting? Before URL Access, any program wanting to access the Internet had to write its own protocol handlers to interact with Web servers, upload to FTP sites, and whatnot, so every developer re-invented the wheel for every Internet application or utility. Wouldn’t it be great if the Mac OS could provide basic Internet functions to any program that wanted them? The project came to life under the codename SubWoofer but was shelved by Apple in the sweeping re-organizations of 1996 and 1997. SubWoofer was adopted by Leonard Rosenthol and other developers, and this year Apple finally decided to roll it into the Mac OS itself.
URL Access provides applications with the capability to upload and download information using HTTP (optionally using 40-bit RSA encryption) and FTP, to access local files using the "file://" URL scheme, to gather various data about URLs, and more. URL Access doesn’t provide all the capabilities needed by a high-end Web browser – especially ones that need to run on versions of the Mac OS prior to 8.6 – but it’s a perfect platform for Internet utilities and custom applications. For instance, Sherlock 2.1 now does all its Internet communication through URL Access. Over time, you can expect more Internet tools to use URL Access to communicate with the Internet, rather than writing their own protocol handlers.
Scriptability — Apple took URL Access one step further by making it accessible to AppleScript and other OSA scripting environments, like UserLand Frontier.
The Mac OS 8.6 Scripting Additions folder contains a tiny application called URL Access Scripting, which enables any OSA script to transfer data to and from the Internet. This simple AppleScript script downloads the TidBITS home page to a file you specify.
set newFile to (new file) tell application "URL Access Scripting" activate download "http://www.tidbits.com/" to newFile with progress quit end tell
This script (which works only under Mac OS 8.6) prompts you to create a new file, then displays a progress dialog as URL Access Scripting connects to our Web server, downloads the page, and saves it to the file you specified.
URL Access Scripting’s interface is rough around the edges: it was originally intended to be a background-only application with no windows, dialogs, or menus, so Apple didn’t give URL Access Scripting any menus – or even a Quit command. However, URL Access Scripting needs to display interface elements like progress and authentication dialogs, so Apple lets it run in the foreground but didn’t provide a proper menubar, which can confuse both users and scripters. Remember that your scripts must tell URL Access Scripting to quit when they’re finished.
Folder Actions — URL Access Scripting offers many capabilities – decoding files, posting form data, handling authentication – and opens up new possibilities for integrating AppleScript into your workflow. Here’s an example of an AppleScript script you can attach to a folder as a Folder Action. Whenever you drop items into the folder, URL Access automatically uploads them to the FTP directory you specify in the second line of the script, replacing any files with the same names. (Remember that Folder Actions trigger when the visible contents of a folder change, so be sure your FTP folder is open, or attached to the bottom of your screen as a pop-up window.)
on adding folder items to thisFolder after receiving fileList set ftpURL to "ftp://ftp.example.com/YourDirectory/" repeat with i in fileList tell application "Finder" to set thisFileType to the file type of i try tell application "URL Access Scripting" activate if thisFileType is "TEXT" then upload i to ftpURL replacing yes with progress and authentication else — delete the return between "binhexing" and "and" in the next line upload i to ftpURL replacing yes with progress, binhexing and authentication end if end tell on error errMsg number errNum display dialog (errNum as string) & ": " & errMsg end try end repeat tell application "URL Access Scripting" to quit end adding folder items to
You’ll notice this script will binhex non-text files it uploads to an FTP site (taking advantage of another of URL Access’s built-in features), but there are plenty of ways to add more intelligence. For instance, if you drop multiple files into this FTP folder, you’ll be prompted for a username and password for each file. You could integrate your username and password into the script directly by using a URL in the following form, although it’s not a secure solution:
A better idea would be to have the script prompt once for the appropriate name and password, then build an appropriate URL for URL Access Scripting. Similarly, this script could be expanded to handle folders dropped into your FTP folder, to test for the presence of a resource fork to determine whether binhexing is necessary, and to offer error handling in case a problem occurs. You could also add a "removing folder items" script to delete items from the FTP site when they’re removed from the folder.
Even More Elaborate — An important thing to notice with all the possible elaborations on the script above is that the enhancements don’t involve URL Access. Taking advantage of URL Access Scripting would still only require a few lines of code, although the rest of the script would become larger to offer more features, flexibility, and intelligence.
Since getting online stock quotes is commonly bandied about as an example of customized Internet functionality, I’ve thrown together an AppleScript script using URL Access Scripting that pulls stock prices from Yahoo’s finance site. Just type a ticker symbol, and the script sends the appropriate query, grabs the resulting HTML, parses it, and displays a dialog with the pricing information.
Again, you’ll notice handling URL Access Scripting is easy: just one line to grab the appropriate Web page, and another to tell URL Access Scripting to quit. The rest of the script is concerned with getting the ticker symbol from the user (capitalizing it if necessary), creating and destroying a temporary file for the Web page, and parsing the results returned from Yahoo’s server. This script could certainly be smarter – for instance, it should handle connection errors and deal with unknown ticker symbols – but, again, those changes don’t involve URL Access Scripting, just the intelligence wrapped around it.
If you want more examples of using URL Access, check out the scripts Apple showed off at this year’s World Wide Developer Conference – they set your desktop picture to images from a variety of Web cams around the world.
Meet a Schedule — Another way to utilize URL Access Scripting is to have scripts run automatically at pre-selected times. Although AppleScript doesn’t have a built-in scheduler, you can use Sophisticated Circuits’ iDo Script Scheduler. A demo version is available for free from Apple, both on its own and as part of Mac OS 8.6’s AppleScript Extras, which is also on the Mac OS 8.6 CD-ROM. The iDo Script Scheduler enables you set up three scripts that run automatically; the forthcoming full version should handle an unlimited number of scripts and offer additional capabilities. Several other scheduling utilities are available on their own or as parts of other products; I’ve been using Late Night Software’s $25 Scheduler for years, and Unix aficionados should check out Chris Johnson’s cron for Macintosh.
Script Happy — At long last, integrating the Internet into your custom scripts and workflow is simple and built directly into the Mac OS. Although URL Access can’t do everything that’s possible with lower-level TCP/IP scripting tools like Mango Tree Software’s TCP/IP Scripting Addition or BIAP’s NetEvents, it provides a wide range of common functionality that’s easy to use. Even if you don’t have much familiarity with AppleScript, a little effort can dramatically improve your use of the Internet.