Detecting When Gravatar Has No Image

[This information in this post is out of date. Please see Gravatar, AppleScript, and the OS X Address Book Revisited for more current information.]


We’re on our way to building an AppleScript to update all of the contacts in your Mac Address Book with pictures from the Gravatar service. This is part 3 of the series.

Part 1, Gravatar, AppleScript, and Address Book Pictures, introduced the topic and ended with a short AppleScript to construct a Gravatar URL given an e-mail address.

Then in part 2, Setting an Address Book Picture with AppleScript, we built a script showing how to successfully set a contact photo in Address Book.

Now we need to detect when the Gravatar service does not have an image on file for a given e-mail address. The problem is that Gravatar does one of several things when it doesn’t have a matching image: return a default image, return an image you specify, or generate an image in one of several styles. But in every case it returns an image.

This is all well and good if you’re displaying Gravatars on a blog comments page. The Gravatar URL ends up inside an img tag and this keeps it dead simple. It’s also nice for everyone to get some kind of avatar even if they haven’t created one.

But for our purposes we don’t need a default image that ends up being attached to a contact. Not only does it clutter our contacts, but it would make it more difficult if we later wanted our script to only update contacts with no existing picture and leave the others alone.

Detecting a no match condition would also be useful in a WordPress plugin even if a default image was used. For example, I’ve always wanted to display some sort of indicator and a link to Gravatar for those who don’t have a Gravatar yet. If we can detect that a default image was used then we can set a CSS class on the image or do other creative things.

So how do we tell if there is no image match? It’s simple with a little undocumented trick in the Gravatar URL.

There is a d= parameter where we can specify a default image to return when there is no match. If we set that to null by using two empty URL encoded quotes then instead of a default image we get nothing. For example:

http://www.gravatar.com/avatar/1bc47906dcb8f3661bae9a8448e49a8a?d=%22%22

For our Address Book script we need to save the image to a file anyway so we just check for the existence of the file or a zero size. We can skip that entry or take other action if there is nothing there.

In the case of a WordPress plugin it might be less convenient because we may have to hit the Gravatar servers twice; once to check for a no match condition, then again to get the image with whatever default we actually want. Of course, if we use a local default file then it would still only be one call to the Gravatar service but that rules out using one of the cool generated defaults.

(If any of the Gravatar folks are listening, it would be great to have an option in the URL that means I’m using this for further processing and not in an img tag then also pass back some sort of status telling if the image is a default.)

Here’s a demonstration AppleScript that makes use of the above technique to download a Gravatar for a given e-mail address and take action only if there was a matched image:

(Updated with a small bug fix on 03/25/2009.)
(Updated on 04/23/2009 to remove my testing e-mail address and display the downloaded file path.)


-- Download a Gravatar image and see if it's a match or default.
-- 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=" & "%22%22" 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.
do shell script "curl " & grav_url & " -m 10 -o " & grav_POSIX_file

tell application "System Events"
	if exists file grav_file then
		tell me to display dialog "Yes, we have a matching Gravatar downloaded to " & grav_POSIX_file & "." buttons {"Okay"} default button 1
		delete file grav_file
	else
		tell me to display dialog "Sorry, there was no matching Gravatar." buttons {"Okay"} default button 1
	end if
end tell

Well, we’re almost to our goal of a script to automatically update all of our Address Book contacts with Gravatars. I’ll put it all together next time in part 4.

7 thoughts on “Detecting When Gravatar Has No Image”

  1. I’m working on a Cocoa class that gives easy Gravatar access, and your tip of using d=%22%22 has been extremely helpful! I haven’t seen that mentioned explicitly anywhere on the net.

    Thanks!

  2. Yeah, great find here on the double quotes. Without it, gravatar would be useless for sites that don’t want G’s all over the place just to help out a handful of users.

  3. @Mike, Gravatar does have a lot of good options for what you want to show on a Web page when a user doesn’t have a Gravatar. It doesn’t have to be the blue “G”.

    This technique is really only needed when you want to have software do some further processing on it.

Leave a Reply