[This information in this post is out of date. Please see Gravatar, AppleScript, and the OS X Address Book Revisited for more current information.]
I’ve always been a fan of adding photos to the people in my Mac Address Book application. It’s a nice personal touch when the photo appears on e-mail messages from people I know. And now that I have an iPhone, those photos sync to the phone and show up in my contacts and when I get calls.
But wouldn’t it be nice if we could add photos to our contacts without doing them all manually? Well, that’s where Gravatar and a little AppleScript will do the job.
For those not familiar with Gravatar, it’s a free avatar service from the nice folks who make WordPress. You register your e-mail address with the service and upload an avatar picture to go with it. Your avatar then automatically shows up on all sorts of blogs and forums where you post or comment, and can be put to use for other services as well.
We should be able to use AppleScript to step through our contacts, grab the e-mail address, retrieve the Gravatar, and add the picture to the contact. The Gravatar implementor’s guide has useage instructions and lots of examples, but nothing for AppleScript. So let’s start with an AppleScript in the same style as the other implementer’s guide examples. (I won’t be covering how Gravatars work since it is so well documented. See How the URL is constructed for details.)
AppleScript doesn’t have built-in commands for calculating md5, converting text to lowercase, and url encoding, which we need. Instead of writing all of that code, we can cheat a little and call the equivalent commands from the OS X shell.
Here’s the basic process:
- Change the e-mail address to lowercase.
- md5 encode the e-mail address.
- url encode the default image URL.
- Slap it all together to make a Gravatar image URL.
And here is the script to do it:
-- Make a Gravatar URL from an e-mail address. -- Released under GPL. -- by Doug Smith, https://smithsrus.com -- The e-mail address to look up. set email to "firstname.lastname@example.org" -- Set a few defaults. property grav_default : "http://somewhere.com/homestar.jpg" property grav_size : 40 -- Encode the elements as needed. set md5_email to do shell script "md5 -q -s `echo " & email & " | tr '[:upper:]' '[:lower:]'`" set encode_default to do shell script "echo " & grav_default & " | perl -MURI::Escape -lne 'print uri_escape($_)'" -- Construct the Gravatar URL. set grav_url to "http://gravatar.com/avatar/" & md5_email & "?d=" & encode_default & "&s=" & grav_size as text
This is getting a bit long so I’m going to break it into multiple parts.
In part 2 I’ll cover how to set an Address Book picture from AppleScript and how to detect if Gravatar doesn’t have an image for an address instead of ending up with a default image we don’t want. Both of those are a little tricky so it will likely fill that post. Part 3 will put it all together to automatically update pictures for the whole address book.
I changed my mind and decided to make the parts into smaller posts covering some of the individual tasks. I’m hoping this will make it easier for others to find these solutions for use in their own projects.
Part 2: Setting an Address Book Picture with AppleScript
Part 3: Detecting When Gravatar Has No Image to Match an E-mail Address
Part 4: Putting it all together with a Script to Update Address Book Contacts with Gravatar Images.
I have some ideas to take it further that may end up in a couple more posts. Please stay tuned and let me know what you think. I’ll construct the remaining posts based on feedback.