Gravatar, AppleScript, and the OS X Address Book Revisited

A while back I did a series of posts (part 1, part 2, part 3, and part 4) on using Gravatars in AppleScript to update pictures in your Mac Address Book. Those address pictures then sync to your iPhone, iPod Touch, or iPad.

I’ve learned a few things since then, plus the Gravatar service has a new option that removes the need for a workaround I had to come up with. So it’s time for an update to my now obsolete posts.

Reference Implementation One: Gravatar to an AppleScript Variable

The most common use case of Gravatar in AppleScript is probably to grab someone’s Gravatar image based on an e-mail address then pass it into some other application. Many Mac applications pass image data internally as a TIFF. So here’s an example of looking up a Gravatar and storing the raw picture data in a variable.

Here’s the script:


-- Download a Gravatar image for the given e-mail address and store in a variable as a TIFF image. Display error if a matching gravatar doesn't exist.
-- Released under GPL.
-- by Doug Smith, https://smithsrus.com

-- The e-mail address to look up.
set email to "someone@somewhere.com"

-- Calculate an MD5 for the e-mail address.
set md5_email to do shell script "md5 -q -s `echo " & email & " | tr '[:upper:]' '[:lower:]'`"

-- Construct the Gravatar URL.
set grav_url to quoted form of ("http://gravatar.com/avatar/" & md5_email & "?d=404" as text)

-- Get the Gravatar image with a timeout of 10 seconds.
try
	set pict_data to do shell script "curl -fsS " & grav_url & " -m 10" as TIFF picture without altering line endings
	tell me to display dialog "Yes, we have a matching Gravatar." buttons {"Okay"} default button 1
on error
	tell me to display dialog "Sorry, there was no matching Gravatar." buttons {"Cancel"} default button 1
end try

As you can see, we end up calling some shell commands because Applescript doesn’t have all of the needed functionality. That’s no big deal because it only uses capabilities built into OS X and should work on any Mac.

Reference Implementation Two: Gravatar to a File

There may be some cases where it’s more useful to download the Gravatar image to a file for later use instead of loading it into a variable. With just a few modifications we can simply save the jpeg file as served up by Gravatar.

Here’s the script:


-- Download a Gravatar image for the given e-mail address and store in a temporary file. Display error if a matching gravatar doesn't exist.
-- Released under GPL.
-- by Doug Smith, https://smithsrus.com

-- The e-mail address to look up.
set email to "someone@somewhere.com"

-- Calculate an MD5 for the e-mail address.
set md5_email to do shell script "md5 -q -s `echo " & email & " | tr '[:upper:]' '[:lower:]'`"

-- Construct the Gravatar URL.
set grav_url to quoted form of ("http://gravatar.com/avatar/" & md5_email & "?d=404" as text)

-- Make a file name in which to temporarily save the Gravatar.
set grav_file to (path to temporary items) & "gravatar" & md5_email & ".jpg" as text
set grav_POSIX_file to quoted form of POSIX path of grav_file

-- Download the Gravatar image to the temporary file with a timeout of 10 seconds.
try
	do shell script "curl -fsS " & grav_url & " -m 10 -o " & grav_POSIX_file
	tell me to display dialog "Yes, we have a matching Gravatar downloaded to " & grav_POSIX_file & "." buttons {"Delete the File"} default button 1
	tell application "System Events"
		delete file grav_file
	end tell
on error
	tell me to display dialog "Sorry, there was no matching Gravatar." buttons {"Cancel"} default button 1
end try

Filling Address Book with Gravatars

My goal for starting this whole project was to be able to populate my Mac Address Book with Gravatar pictures. Those then get automatically synced to my iPhone, which is really nice when receiving calls. A hat tip to Moritz Haarmann for his Gravatar Address Book plugin, which is what started me thinking about this in the first place. His plugin only works on one record at a time so I wanted to expand the concept to mass update selected records or the whole Address Book.

Rather than list the full script here, you can just download the application. You can open it in Script Editor if you want to see how it works. Be sure to back up your Address Book file, just in case. And if you experience errors or odd behavior, try quitting and relaunching Address Book, which often fixes little problems with it.

Be sure to let me know how you like it and if it works well for you. It would be interesting to know what percentage of addresses everyone is finding have Gravatars available now. Enjoy!

8 thoughts on “Gravatar, AppleScript, and the OS X Address Book Revisited”

  1. Hi Doug – I love it! I got 178 out 0f 768 contacts with Gravatars, although I have a lot of duplicates and mess in my Address Book, so don’t know how many uniques there were.

    Thanks!
    Beau

    1. Thanks for the link, Martijn. That script makes a nice companion to what I’ve done. My script does ask your preference of skipping those with existing images so that’s another way to avoid touching images you’ve already added by hand. I usually just let mine all update so I get the newest images when my contacts update them on Gravatar, though.

Leave a Reply