AddThis Social Bookmark Button

Print
O'Reilly Book Excerpts: Mac OS X Panther Hacks

Hacking Mac OS X Panther

Editor's note: Rael Dornfest, coauthor of Mac OS X Panther Hacks, has selected these three hacks from the book for your sampling pleasure. The first two detail how to find anyone in your Address Book who has an Amazon Wish List, and how to build a GUI to your Unix scripts with a bit of Perl or Python; the third is just for fun. Enjoy.

Moderate Hack #15

Fulfill Wishes with Address Book


This hack will find anyone in your Address Book that has an Amazon Wish List, but it's up to you buy them something!

The Macintosh operating system has always been a rather scriptable environment. Many applications support scriptable events that let you automate tasks or integrate separate programs. With Mac OS X, there are even more opportunities for scripting, now that the ultimate scriptable environment, Unix, is under the hood. But why limit yourself to the Desktop? The Mac's script-friendly environment can integrate your Desktop application with web applications such as Amazon.

This hack is a quick example that loosely integrates the Mac Address Book with Amazon. Using AppleScript and an underlying Unix tool called curl, this script finds people in your Address Book that also have an Amazon Wish List. This script might help you find a gift for someone you know, or it might just give you a different perspective on someone by finding what they're interested in. Most importantly, though, it shows how Desktop applications can become smarter by integrating with web applications.

Mac OS X Panther Hacks

Related Reading

Mac OS X Panther Hacks
100 Industrial Strength Tips & Tools
By Rael Dornfest, James Duncan Davidson

The Code

Open the Script Editor (Applications→AppleScript), enter the following code, and save the script with a suitably snappy name, like Get Wish Lists:

(*
Find Wish Lists in Address Book

The script loops through people in your Address Book, checking Amazon
to see if they have a Wish List. If their Wish List is found, you
have the option to view it in your default browser.

by Paul Bausch
*)

-- set some variables for the curl command

set userAgent to "Mozilla/5.0 (Macintosh; U; PPC Mac OSX; en-us)"
set curlCommand to "curl -i -b -A -L \"" & userAgent & "\" "

-- open Address Book and loop through people

tell application "Address Book"
  repeat with thisPerson in the people
    set thisName to name of thisPerson
    repeat with thisAddress in email of thisPerson
      set thisEmail to value of thisAddress

      -- build the URL that will search for the Wish List

      set baseURL to "http://www.amazon.com/gp/registry/search.html"
      set thisURL to baseURL & "/?type=wishlist\\&field-name=" & thisEmail

      -- use curl to fetch the search page

      set thisWishPage to do shell script curlCommand & thisURL

      -- if the search returns what appears to be a match, prompt the user

      if thisWishPage contains "&id=" then
        set theAction to display dialog thisName & ¬
" has an Amazon wishlist." buttons {"View", "Ignore"}
        if button returned of theAction is "View" then

          -- Find the ID on the page

          set beginID to (offset of "&id=" in thisWishPage) + 4
          set endID to (offset of "'s Wish List" in thisWishPage) - 1
          set thisWishID to get text beginID thru endID of thisWishPage

          if thisWishPage contains "&id=" thenset beginID to 1
            set endID to (offset of "\">" in thisWishID) - 1
          set thisWishID to get text beginID thru endID of thisWishID

          -- Open the default browser to the Wishlist page

          tell me to open location "http://www.amazon.com/o/registry/" ¬
& thisWishID
        end if
      end if
    end repeat
  end repeat
end tell

The script starts by setting some variables for curl that will be used later.

TIP: If you'd like learn more about curl and what these settings mean, open a Terminal window (Applications→Utilities→Terminal) and type man curl. You'll get the complete documentation that explains what all of these command switches do. Or, you can read "Downloading Files from the Command Line" [Mac OS X Hacks, Hack #61].

Then, the script loops through all of the entries in the Address Book, looking for email addresses. It uses each individual email address to build a URL that queries Amazon for a Wish List associated with that address. This is where the script needs to step out of its AppleScript confines to the larger world of Unix commands. With the do shell script command, curl contacts Amazon to see if this person has a Wish List.

If a Wish List is found, the display dialog command brings up a window like the one in Figure 2-6, with two options: View or Ignore.


Figure 2-6. Script prompt

Clicking View tells the script to open the Wish List with the default browser. Because this command takes place within the tell application "AddressBook" block, the context-switching tell me command needs to precede AppleScript's open location function.

Running the Code

You can run the code directly from the Script Editor by clicking the Run button.

To always have it a click away from your Address Book, move the script to the Library/Scripts/Address Book Scripts folder. This way, it'll be available from your Address Book's Scripts menu. Run the script at any time by selecting Script Menu→Get Wish Lists from the Address Book application's menu bar.

Paul Bausch

Pages: 1, 2, 3

Next Pagearrow