O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Mac OS X Panther Hacks
By Rael Dornfest, James Duncan Davidson
June 2004
More Info

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 Code
[Discuss (0) | Link to this hack]

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 , with two options: View or Ignore.

Figure 1. 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.


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.