Section 9.6. URL Access Scripting


9.6. URL Access Scripting

Quick: Your friend just emailed you a link to a file on the Web. What do you do now?

If you're like most people, you open Safari (or some other Web browser), paste the URL in, press Return, and wait for the file to download. Life's too short to download files that way, though.

The easier approach is using URL Access Scripting, a built-in Mac program for downloading (and uploading) files in the background. As a simple, fast, invisible program, you can think of URL Access Scripting kind of like Image Events (Section 7.3), just for the Web.

9.6.1. Downloading a File Quickly

When you want a file downloaded right this minute, the following script can help:

--Part 1: set theURL to the text returned of (display dialog ¬     "Enter the URL to download:" default answer "http://") --Part 2: set theFile to (choose file name) --Part 3: tell application "URL Access Scripting"     download theURL to theFile end tell display dialog "Your download is complete"

Once you run that script, you'll see a series of three dialog boxes:

  • The first asks what you want to download.

  • The second asks you where you want to save the downloaded file.

  • The third tells you that the file's been downloaded. (Figure 9-9 has the details.)

Lest the script confuse you, here's how it works:

  • Part 1 uses the standard display dialog command to ask you what file to download. You can specify a Web site, a specific page on a Web site, an online picture, or just about anything else you can get to using a Web browser or FTP program.

An FTP server is simply a Web site whose purpose is to transfer files. That distinguishes it from a Web server, whose job it is to transfer actual Web pages.

  • Part 2 uses the choose file name command to ask where you want to save the file. Your desktop is as good a place as any.

  • Part 3 uses URL Access Scripting to download the file you specified in part 1 and save the file in the location you specified in part 2. Once the download's finished, you see the final informative dialog box.

Due to a bug in AppleScript, the Mac OS 9 version of URL Access Scripting might launch when you run this script. If that happens, pop open a Finder window and navigate your way to Mac OS 9's System Folder (the folder named "System Folder," not "System"). Then go to the Scripting Additions URL Access Scripting subfolder, drag the URL Access Scripting file to the Trash, and empty the Trash can (Finder Empty Trash).

From now on, your scripts will always use the new, Mac OS X version of URL Access Scripting instead of the obsolete, Mac OS 9 version.

Figure 9-9. Top: Enter the URL of the file you want to download. If you want a file from a Web site, make sure you preface the URL with http://. You're not limited to just Web sites, thoughyou can download files from FTP servers (preface the URL with ftp://) or even other Macs on your network (preface the URL with afp://).Middle: Choose where you want to save the file, and give the file a name. Also, make sure you give the file an appropriate extension (.html for Web pages, .mov for QuickTime movies, and so on), or else Mac OS X won't know what program should open the file. Bottom: Once the download is finished, two things happen. First, an icon appears in whatever location you specified (left). Soon after, you see a dialog box telling you the download is complete (right).


9.6.2. Uploading a File

While URL Access Scripting is convenient for downloading files, it can be even more convenient for uploading them. That way, you don't have to go through all the Finder's red tape (Go Connect to Server, then type out the URL, choose the file to upload, drag the file to the server, and disconnect) when you want to upload a file. True, if you use URL Access Scripting, you won't get the full-fledged progress indicator that the Finder shows you as a file's being copied. Still, this AppleScript is just about the fastest way to upload something to the Internet:

--Part 1: set theFile to (choose file) --Part 2: set theServer to (choose URL) --Part 3: tell application "URL Access Scripting"     upload theFile to theServer with authentication without binhexing end tell display dialog "Your upload is complete"

Here's how the script works:

  • Part 1 uses the choose file command to let you pick the file you want to upload (Figure 9-10, top).

  • Part 2 uses choose URL (a command for locating computer servers on your network or the Internet) to ask you where you want to upload the file (Figure 9-10, middle).

If you have your own home page as part of the $99-per-year .Mac service, you can use that dialog box to upload files directly to your Web site. Simply type this URL:

http://idisk.mac.com/yourMemberName/Sites/

but replace yourMemberName with your actual .Mac member name (which you can find in System Preferences .Mac). In a snap, AppleScript uploads the file to your Web site, saving you the annoyance of having to load your iDisk (your virtual Web-storage disk) in the Finder with the Go iDisk My iDisk command.

Note, however, that due to a bug in the upload command, this script can only copy files that don't have spaces in their names to your .Mac Web site. If you need to upload files that have spaces in their names, you either have to rename the files to get rid of the spaces or load your iDisk in the Finder and copy the files by hand.

  • Part 3 does the actual deed, using URL Access Scripting to upload the file to the server. The with authentication bit tells AppleScript to present a password dialog box (Figure 9-10, bottom), and the without binhexing option tells Mac OS X not to use any special file encoding when uploading the file. In other words, this part simply copies the file you selected in part 1 to the server you selected in part 2, safely and securely.

  • Finally, the script presents a dialog box once the uploading is finished.

Congratulations: you just saved a small fortune on commercial Web-updating software.



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net