<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Smiths R Us &#187; avatar</title>
	<atom:link href="http://smithsrus.com/tag/avatar/feed/" rel="self" type="application/rss+xml" />
	<link>http://smithsrus.com</link>
	<description>Technology, home automation, usability, and whatever else amuses me.</description>
	<lastBuildDate>Mon, 19 Jul 2010 17:38:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Filling Mac Address Book Pictures with Gravatars</title>
		<link>http://smithsrus.com/filling-mac-address-book-pictures-with-gravatars/</link>
		<comments>http://smithsrus.com/filling-mac-address-book-pictures-with-gravatars/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 15:09:04 +0000</pubDate>
		<dc:creator>Doug Smith</dc:creator>
				<category><![CDATA[Gravatar]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[applescript]]></category>
		<category><![CDATA[avatar]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Macintosh]]></category>

		<guid isPermaLink="false">http://smithsrus.com/?p=151</guid>
		<description><![CDATA[[This information in this post is out of date. Please see Gravatar, AppleScript, and the OS X Address Book Revisited for more current information.] This is a multi-part tutorial series on using Gravatar from AppleScript to update your Mac Address Book pictures automatically. These pictures then sync to your iPhone for Gravatar goodness on the [...]]]></description>
			<content:encoded><![CDATA[<p><em>[This information in this post is out of date. Please see <a href="http://smithsrus.com/gravatar-applescript-and-the-os-x-address-book-revisited/">Gravatar, AppleScript, and the OS X Address Book Revisited</a> for more current information.]</em><br />
<hr />
<p>This is a multi-part tutorial series on using <a href="http://gravatar.com/">Gravatar</a> from <a href="http://www.apple.com/applescript/">AppleScript</a> to update your Mac Address Book pictures automatically. These pictures then sync to your iPhone for Gravatar goodness on the go.</p>
<p>I&#8217;ve had some AppleScripts to work with Gravatars and Address Book hanging around in rough form for quite a while. But I was recently prompted to clean them up and write these tutorials when <a href="http://ma.tt/2009/03/gravatar-in-gmail/">Matt Mullenweg posted about Gravatar on his blog</a> saying that &#8220;we need a better way to get Gravatars into your Apple address book / iPhone.&#8221; I hope that the documentation and tools from this project will be used for other creative Gravatar integration projects.</p>
<p>We&#8217;re about to put all of what we learned in <a href="http://smithsrus.com/gravatar-applescript-and-address-book-pictures/">part 1</a>, <a href="http://smithsrus.com/setting-an-address-book-picture-with-applescript/">part 2</a>, and <a href="http://smithsrus.com/detecting-when-gravatar-has-no-image/">part 3</a> together to make our working script.<span id="more-151"></span> Be sure to check out the previous parts for the details of how it all works.</p>
<p>Here is the process the script will be doing:</p>
<ol>
<li>Get a list of only the selected contacts from Address Book. This way we can control which contacts get their pictures updated if we don&#8217;t want them all.</li>
<li>Ask the user if we should overwrite any existing pictures or skip those contacts.</li>
<li>Step through each of those contacts and get all of the e-mail addresses associated with each.</li>
<li>Step through each e-mail address and download its Gravatar to a temporary file. If there is more than one address with a Gravatar we&#8217;ll keep it simple and just let it set each one in turn and we&#8217;ll end up with last one found. This could be improved in the future to favor a particular one if needed.</li>
<li>Update the Address Book picture, delete the temporary file, then repeat the process with the next contact.</li>
</ol>
<p>Here&#8217;s the script: (<a href="applescript://com.apple.scripteditor?action=new&#038;script=--%20Set%20selected%20Address%20Book%20contact%20pictures%20from%20Gravatar.%0D--%20Released%20under%20GPL.%0D--%20by%20Doug%20Smith%2C%20http%3A//smithsrus.com%0D%0D%0D--%20Grab%20the%20selected%20records%20and%20see%20how%20many%20there%20are.%0Dtell%20application%20%22Address%20Book%22%0D%09set%20selected_contacts%20to%20selection%0D%09set%20count_selected%20to%20number%20of%20items%20in%20selected_contacts%0D%09%0D%09--%20Bail%20out%20if%20there%20are%20no%20records%20selected.%20Otherwise%20ask%20about%20how%20to%20deal%20%0D%09--%20with%20contacts%20having%20existing%20pictures.%0D%09if%20count_selected%20%3C%201%20then%0D%09%09tell%20me%20to%20display%20dialog%20%22You%20must%20first%20select%20some%20Address%20Book%20contacts.%22%20buttons%20%7B%22Cancel%22%7D%20default%20button%201%20cancel%20button%201%0D%09else%0D%09%09if%20count_selected%20%3D%201%20then%0D%09%09%09set%20plural%20to%20%22%22%0D%09%09else%0D%09%09%09set%20plural%20to%20%22s%22%0D%09%09end%20if%0D%09%09tell%20me%20to%20set%20user_result%20to%20display%20dialog%20%22We%27re%20about%20to%20try%20updating%20%22%20%26%20count_selected%20%26%20%22%20selected%20Address%20Book%20contact%20picture%22%20%26%20plural%20%26%20%22%20from%20Gravatar.%20Would%20you%20like%20to%20overwrite%20existing%20pictures%20or%20skip%20those%20contacts%3F%22%20buttons%20%7B%22Cancel%22%2C%20%22Overwrite%20Existing%22%2C%20%22Skip%20Existing%22%7D%20cancel%20button%201%20default%20button%201%20with%20icon%20caution%0D%09%09set%20overwrite%20to%20%28button%20returned%20of%20user_result%20contains%20%22Overwrite%22%29%0D%09end%20if%0D%09%0D%09--%20Step%20through%20each%20contact%20and%20find%20Gravatars.%0D%09with%20timeout%20of%20600%20seconds%0D%09%09repeat%20with%20one_contact%20in%20selected_contacts%0D%09%09%09if%20not%20%28%28image%20of%20one_contact%20exists%29%20and%20not%20overwrite%29%20then%0D%09%09%09%09%0D%09%09%09%09--%20Step%20through%20the%20contact%27s%20e-mail%20addresses.%0D%09%09%09%09set%20all_emails%20to%20emails%20of%20one_contact%0D%09%09%09%09repeat%20with%20one_email_record%20in%20all_emails%0D%09%09%09%09%09set%20one_email_address%20to%20value%20of%20one_email_record%0D%09%09%09%09%09%0D%09%09%09%09%09set%20grav_file%20to%20%28my%20get_gravatar%28my%20trim_email%28one_email_address%29%29%29%0D%09%09%09%09%09%0D%09%09%09%09%09--%20If%20we%20have%20a%20Gravatar%20image%20add%20it%20to%20the%20contact.%0D%09%09%09%09%09if%20grav_file%20is%20not%20%22%22%20then%0D%09%09%09%09%09%09set%20image%20of%20one_contact%20to%20my%20get_pict_data%28grav_file%29%0D%09%09%09%09%09end%20if%0D%09%09%09%09end%20repeat%0D%09%09%09end%20if%0D%09%09end%20repeat%0D%09end%20timeout%0D%09tell%20me%20to%20display%20dialog%20%22Done.%20You%20may%20need%20to%20select%20a%20different%20record%20to%20force%20Address%20Book%20to%20refresh%20the%20screen%20before%20changes%20show.%22%20buttons%20%7B%22Okay%22%7D%20default%20button%201%0Dend%20tell%0D%0D%0D--%20Trim%20junk%20off%20the%20end%20of%20the%20e-mail%20address%20field.%20Assume%20space%20separated.%0Don%20trim_email%28email_address%29%0D%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20%22%20%22%0D%09return%20item%201%20of%20email_address%0Dend%20trim_email%0D%0D%0D--%20Look%20up%20a%20Gravatar%20from%20an%20e-mail%20address%20and%20save%20to%20a%20file.%0Don%20get_gravatar%28email_address%29%0D%09--%20Calculate%20an%20MD5%20for%20the%20e-mail%20address.%0D%09set%20md5_email%20to%20do%20shell%20script%20%22md5%20-q%20-s%20%60echo%20%22%20%26%20email_address%20%26%20%22%20%7C%20tr%20%27%5B%3Aupper%3A%5D%27%20%27%5B%3Alower%3A%5D%27%60%22%0D%09%0D%09--%20Construct%20the%20Gravatar%20URL.%20Max%20Address%20Book%20picture%20size%20stored%20is%20218%20pixels.%0D%09set%20grav_url%20to%20quoted%20form%20of%20%28%22http%3A//gravatar.com/avatar/%22%20%26%20md5_email%20%26%20%22%3Fs%3D218%26d%3D%22%20%26%20%22%2522%2522%22%20as%20text%29%0D%09%0D%09--%20Make%20a%20file%20name%20in%20which%20to%20temporarily%20save%20the%20Gravatar.%0D%09set%20grav_file%20to%20%28path%20to%20temporary%20items%29%20%26%20%22gravatar%22%20%26%20md5_email%20%26%20%22.jpg%22%20as%20text%0D%09set%20grav_POSIX_file%20to%20quoted%20form%20of%20POSIX%20path%20of%20grav_file%0D%09%0D%09--%20Download%20the%20Gravatar%20image%20to%20the%20temporary%20file%20with%20a%20timeout%20of%2010%20seconds.%0D%09do%20shell%20script%20%22curl%20%22%20%26%20grav_url%20%26%20%22%20-m%2010%20-o%20%22%20%26%20grav_POSIX_file%0D%09%0D%09tell%20application%20%22System%20Events%22%0D%09%09if%20exists%20file%20grav_file%20then%0D%09%09%09return%20grav_file%0D%09%09else%0D%09%09%09return%20%22%22%0D%09%09end%20if%0D%09end%20tell%0Dend%20get_gravatar%0D%0D--%20Read%20the%20picture%20data%20into%20a%20variable%20in%20TIFF%20format%20then%20delete%20the%20file.%0Don%20get_pict_data%28grav_file%29%0D%09tell%20application%20%22System%20Events%22%0D%09%09set%20file_ref%20to%20open%20for%20access%20grav_file%0D%09%09set%20pict_data%20to%20read%20file_ref%20as%20%22TIFF%22%0D%09%09close%20access%20file_ref%0D%09%09%0D%09%09delete%20file%20grav_file%0D%09%09return%20pict_data%0D%09end%20tell%0Dend%20get_pict_data">Open in Script Editor</a>)</p>

<div class="wp_syntax"><div class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Set selected Address Book contact pictures from Gravatar.</span>
<span style="color: #808080; font-style: italic;">-- Released under GPL.</span>
<span style="color: #808080; font-style: italic;">-- by Doug Smith, http://smithsrus.com</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">-- Grab the selected records and see how many there are.</span>
<span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;Address Book&quot;</span>
	<span style="color: #ff0033; font-weight: bold;">set</span> selected_contacts <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">selection</span>
	<span style="color: #ff0033; font-weight: bold;">set</span> count_selected <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">number</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">in</span> selected_contacts
&nbsp;
	<span style="color: #808080; font-style: italic;">-- Bail out if there are no records selected. Otherwise ask about how to deal </span>
	<span style="color: #808080; font-style: italic;">-- with contacts having existing pictures.</span>
	<span style="color: #ff0033; font-weight: bold;">if</span> count_selected <span style="color: #000000;">&lt;</span> <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">then</span>
		<span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">me</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">display dialog</span> <span style="color: #009900;">&quot;You must first select some Address Book contacts.&quot;</span> <span style="color: #0066ff;">buttons</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">&quot;Cancel&quot;</span><span style="color: #000000;">&#125;</span> default button <span style="color: #000000;">1</span> cancel button <span style="color: #000000;">1</span>
	<span style="color: #ff0033; font-weight: bold;">else</span>
		<span style="color: #ff0033; font-weight: bold;">if</span> count_selected <span style="color: #000000;">=</span> <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">then</span>
			<span style="color: #ff0033; font-weight: bold;">set</span> plural <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;&quot;</span>
		<span style="color: #ff0033; font-weight: bold;">else</span>
			<span style="color: #ff0033; font-weight: bold;">set</span> plural <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;s&quot;</span>
		<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
		<span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">me</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #ff0033; font-weight: bold;">set</span> user_result <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">display dialog</span> <span style="color: #009900;">&quot;We're about to try updating &quot;</span> <span style="color: #000000;">&amp;</span> count_selected <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot; selected Address Book contact picture&quot;</span> <span style="color: #000000;">&amp;</span> plural <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot; from Gravatar. Would you like to overwrite existing pictures or skip those contacts?&quot;</span> <span style="color: #0066ff;">buttons</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">&quot;Cancel&quot;</span>, <span style="color: #009900;">&quot;Overwrite Existing&quot;</span>, <span style="color: #009900;">&quot;Skip Existing&quot;</span><span style="color: #000000;">&#125;</span> cancel button <span style="color: #000000;">1</span> default button <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">with</span> icon caution
		<span style="color: #ff0033; font-weight: bold;">set</span> overwrite <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #000000;">&#40;</span>button returned <span style="color: #ff0033; font-weight: bold;">of</span> user_result <span style="color: #ff0033;">contains</span> <span style="color: #009900;">&quot;Overwrite&quot;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">-- Step through each contact and find Gravatars.</span>
	<span style="color: #ff0033; font-weight: bold;">with</span> <span style="color: #ff0033; font-weight: bold;">timeout</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #000000;">600</span> seconds
		<span style="color: #ff0033; font-weight: bold;">repeat</span> <span style="color: #ff0033; font-weight: bold;">with</span> one_contact <span style="color: #ff0033; font-weight: bold;">in</span> selected_contacts
			<span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #ff0033;">not</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>image <span style="color: #ff0033; font-weight: bold;">of</span> one_contact <span style="color: #0066ff;">exists</span><span style="color: #000000;">&#41;</span> <span style="color: #ff0033;">and</span> <span style="color: #ff0033;">not</span> overwrite<span style="color: #000000;">&#41;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
&nbsp;
				<span style="color: #808080; font-style: italic;">-- Step through the contact's e-mail addresses.</span>
				<span style="color: #ff0033; font-weight: bold;">set</span> all_emails <span style="color: #ff0033; font-weight: bold;">to</span> emails <span style="color: #ff0033; font-weight: bold;">of</span> one_contact
				<span style="color: #ff0033; font-weight: bold;">repeat</span> <span style="color: #ff0033; font-weight: bold;">with</span> one_email_record <span style="color: #ff0033; font-weight: bold;">in</span> all_emails
					<span style="color: #ff0033; font-weight: bold;">set</span> one_email_address <span style="color: #ff0033; font-weight: bold;">to</span> value <span style="color: #ff0033; font-weight: bold;">of</span> one_email_record
&nbsp;
					<span style="color: #ff0033; font-weight: bold;">set</span> grav_file <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #000000;">&#40;</span><span style="color: #ff0033; font-weight: bold;">my</span> get_gravatar<span style="color: #000000;">&#40;</span><span style="color: #ff0033; font-weight: bold;">my</span> trim_email<span style="color: #000000;">&#40;</span>one_email_address<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
&nbsp;
					<span style="color: #808080; font-style: italic;">-- If we have a Gravatar image add it to the contact.</span>
					<span style="color: #ff0033; font-weight: bold;">if</span> grav_file <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033;">not</span> <span style="color: #009900;">&quot;&quot;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
						<span style="color: #ff0033; font-weight: bold;">set</span> image <span style="color: #ff0033; font-weight: bold;">of</span> one_contact <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #ff0033; font-weight: bold;">my</span> get_pict_data<span style="color: #000000;">&#40;</span>grav_file<span style="color: #000000;">&#41;</span>
					<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
				<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">repeat</span>
			<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
		<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">repeat</span>
	<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">timeout</span>
	<span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">me</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">display dialog</span> <span style="color: #009900;">&quot;Done. You may need to select a different record to force Address Book to refresh the screen before changes show.&quot;</span> <span style="color: #0066ff;">buttons</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">&quot;Okay&quot;</span><span style="color: #000000;">&#125;</span> default button <span style="color: #000000;">1</span>
<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">tell</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">-- Trim junk off the end of the e-mail address field. Assume space separated.</span>
<span style="color: #ff0033; font-weight: bold;">on</span> trim_email<span style="color: #000000;">&#40;</span>email_address<span style="color: #000000;">&#41;</span>
	<span style="color: #ff0033; font-weight: bold;">set</span> AppleScript<span style="">'</span>s <span style="color: #0066ff;">text</span> <span style="color: #0066ff;">item</span> <span style="color: #0066ff;">delimiters</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot; &quot;</span>
	<span style="color: #ff0033; font-weight: bold;">return</span> <span style="color: #0066ff;">item</span> <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">of</span> email_address
<span style="color: #ff0033; font-weight: bold;">end</span> trim_email
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">-- Look up a Gravatar from an e-mail address and save to a file.</span>
<span style="color: #ff0033; font-weight: bold;">on</span> get_gravatar<span style="color: #000000;">&#40;</span>email_address<span style="color: #000000;">&#41;</span>
	<span style="color: #808080; font-style: italic;">-- Calculate an MD5 for the e-mail address.</span>
	<span style="color: #ff0033; font-weight: bold;">set</span> md5_email <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;md5 -q -s `echo &quot;</span> <span style="color: #000000;">&amp;</span> email_address <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot; | tr '[:upper:]' '[:lower:]'`&quot;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">-- Construct the Gravatar URL. Max Address Book picture size stored is 218 pixels.</span>
	<span style="color: #ff0033; font-weight: bold;">set</span> grav_url <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #000000;">&#40;</span><span style="color: #009900;">&quot;http://gravatar.com/avatar/&quot;</span> <span style="color: #000000;">&amp;</span> md5_email <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;?s=218&amp;d=&quot;</span> <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;%22%22&quot;</span> <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">text</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">-- Make a file name in which to temporarily save the Gravatar.</span>
	<span style="color: #ff0033; font-weight: bold;">set</span> grav_file <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #000000;">&#40;</span><span style="color: #0066ff;">path to</span> temporary <span style="color: #0066ff;">items</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;gravatar&quot;</span> <span style="color: #000000;">&amp;</span> md5_email <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;.jpg&quot;</span> <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">text</span>
	<span style="color: #ff0033; font-weight: bold;">set</span> grav_POSIX_file <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">POSIX path</span> <span style="color: #ff0033; font-weight: bold;">of</span> grav_file
&nbsp;
	<span style="color: #808080; font-style: italic;">-- Download the Gravatar image to the temporary file with a timeout of 10 seconds.</span>
	<span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;curl &quot;</span> <span style="color: #000000;">&amp;</span> grav_url <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot; -m 10 -o &quot;</span> <span style="color: #000000;">&amp;</span> grav_POSIX_file
&nbsp;
	<span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;System Events&quot;</span>
		<span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #0066ff;">exists</span> <span style="color: #0066ff;">file</span> grav_file <span style="color: #ff0033; font-weight: bold;">then</span>
			<span style="color: #ff0033; font-weight: bold;">return</span> grav_file
		<span style="color: #ff0033; font-weight: bold;">else</span>
			<span style="color: #ff0033; font-weight: bold;">return</span> <span style="color: #009900;">&quot;&quot;</span>
		<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
	<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">tell</span>
<span style="color: #ff0033; font-weight: bold;">end</span> get_gravatar
&nbsp;
<span style="color: #808080; font-style: italic;">-- Read the picture data into a variable in TIFF format then delete the file.</span>
<span style="color: #ff0033; font-weight: bold;">on</span> get_pict_data<span style="color: #000000;">&#40;</span>grav_file<span style="color: #000000;">&#41;</span>
	<span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;System Events&quot;</span>
		<span style="color: #ff0033; font-weight: bold;">set</span> file_ref <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">open</span> for access grav_file
		<span style="color: #ff0033; font-weight: bold;">set</span> pict_data <span style="color: #ff0033; font-weight: bold;">to</span> read file_ref <span style="color: #ff0033;">as</span> <span style="color: #009900;">&quot;TIFF&quot;</span>
		<span style="color: #0066ff;">close</span> access file_ref
&nbsp;
		<span style="color: #0066ff;">delete</span> <span style="color: #0066ff;">file</span> grav_file
		<span style="color: #ff0033; font-weight: bold;">return</span> pict_data
	<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">tell</span>
<span style="color: #ff0033; font-weight: bold;">end</span> get_pict_data</pre></div></div>

<p>There you have it. I have some ideas to take it further but I&#8217;ll let everyone test what we have and offer feedback before continuing. I&#8217;ll also make an easier to use application download for everyone once we&#8217;ve done enough testing to work out the kinks.</p>
]]></content:encoded>
			<wfw:commentRss>http://smithsrus.com/filling-mac-address-book-pictures-with-gravatars/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Gravatar, AppleScript, and Address Book Pictures</title>
		<link>http://smithsrus.com/gravatar-applescript-and-address-book-pictures/</link>
		<comments>http://smithsrus.com/gravatar-applescript-and-address-book-pictures/#comments</comments>
		<pubDate>Sat, 21 Mar 2009 22:46:36 +0000</pubDate>
		<dc:creator>Doug Smith</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[applescript]]></category>
		<category><![CDATA[avatar]]></category>
		<category><![CDATA[Gravatar]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mac]]></category>

		<guid isPermaLink="false">http://smithsrus.com/?p=147</guid>
		<description><![CDATA[[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&#8217;ve always been a fan of adding photos to the people in my Mac Address Book application. It&#8217;s a nice personal touch when the photo appears on e-mail messages from people [...]]]></description>
			<content:encoded><![CDATA[<p><em>[This information in this post is out of date. Please see <a href="http://smithsrus.com/gravatar-applescript-and-the-os-x-address-book-revisited/">Gravatar, AppleScript, and the OS X Address Book Revisited</a> for more current information.]</em><br />
<hr />
<p>I&#8217;ve always been a fan of adding photos to the people in my Mac Address Book application. It&#8217;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.</p>
<p>But wouldn&#8217;t it be nice if we could add photos to our contacts without doing them all manually? Well, that&#8217;s where <a href="http://gravatar.com">Gravatar</a> and a little AppleScript will do the job.<span id="more-147"></span></p>
<p>For those not familiar with Gravatar, it&#8217;s a free avatar service from the nice folks who make <a href="http://wordpress.org">WordPress</a>. 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.</p>
<p>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 <a href="http://en.gravatar.com/site/implement">Gravatar implementor&#8217;s guide</a> has useage instructions and lots of examples, but nothing for AppleScript. So let&#8217;s start with an AppleScript in the same style as the other implementer&#8217;s guide examples. (I won&#8217;t be covering how Gravatars work since it is so well documented. See <a href="http://en.gravatar.com/site/implement/url">How the URL is constructed</a> for details.)</p>
<p>AppleScript doesn&#8217;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.</p>
<p>Here&#8217;s the basic process:</p>
<ol>
<li>Change the e-mail address to lowercase.</li>
<li>md5 encode the e-mail address.</li>
<li>url encode the default image URL.</li>
<li>Slap it all together to make a Gravatar image URL.</li>
</ol>
<p>And here is the script to do it: (<a href="applescript://com.apple.scripteditor?action=new&#038;script=--%20Make%20a%20Gravatar%20URL%20from%20an%20e-mail%20address.%0D--%20Released%20under%20GPL.%0D--%20by%20Doug%20Smith%2C%20http%3A//smithsrus.com%0D%0D--%20The%20e-mail%20address%20to%20look%20up.%0Dset%20email%20to%20%22someone%40somewhere.com%22%0D%0D--%20Set%20a%20few%20defaults.%0Dproperty%20grav_default%20%3A%20%22http%3A//somewhere.com/homestar.jpg%22%0Dproperty%20grav_size%20%3A%2040%0D%0D--%20Encode%20the%20elements%20as%20needed.%0Dset%20md5_email%20to%20do%20shell%20script%20%22md5%20-q%20-s%20%60echo%20%22%20%26%20email%20%26%20%22%20%7C%20tr%20%27%5B%3Aupper%3A%5D%27%20%27%5B%3Alower%3A%5D%27%60%22%0Dset%20encode_default%20to%20do%20shell%20script%20%22echo%20%22%20%26%20grav_default%20%26%20%22%20%7C%20perl%20-MURI%3A%3AEscape%20-lne%20%27print%20uri_escape%28%24_%29%27%22%0D%0D--%20Construct%20the%20Gravatar%20URL.%0Dset%20grav_url%20to%20%22http%3A//gravatar.com/avatar/%22%20%26%20md5_email%20%26%20%22%3Fd%3D%22%20%26%20encode_default%20%26%20%22%26s%3D%22%20%26%20grav_size%20as%20text%0D">Open in Script Editor</a>)</p>

<div class="wp_syntax"><div class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Make a Gravatar URL from an e-mail address.</span>
<span style="color: #808080; font-style: italic;">-- Released under GPL.</span>
<span style="color: #808080; font-style: italic;">-- by Doug Smith, http://smithsrus.com</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- The e-mail address to look up.</span>
<span style="color: #ff0033; font-weight: bold;">set</span> email <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;someone@somewhere.com&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- Set a few defaults.</span>
<span style="color: #ff0033; font-weight: bold;">property</span> grav_default : <span style="color: #009900;">&quot;http://somewhere.com/homestar.jpg&quot;</span>
<span style="color: #ff0033; font-weight: bold;">property</span> grav_size : <span style="color: #000000;">40</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- Encode the elements as needed.</span>
<span style="color: #ff0033; font-weight: bold;">set</span> md5_email <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;md5 -q -s `echo &quot;</span> <span style="color: #000000;">&amp;</span> email <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot; | tr '[:upper:]' '[:lower:]'`&quot;</span>
<span style="color: #ff0033; font-weight: bold;">set</span> encode_default <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;echo &quot;</span> <span style="color: #000000;">&amp;</span> grav_default <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot; | perl -MURI::Escape -lne 'print uri_escape($_)'&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- Construct the Gravatar URL.</span>
<span style="color: #ff0033; font-weight: bold;">set</span> grav_url <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;http://gravatar.com/avatar/&quot;</span> <span style="color: #000000;">&amp;</span> md5_email <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;?d=&quot;</span> <span style="color: #000000;">&amp;</span> encode_default <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;&amp;s=&quot;</span> <span style="color: #000000;">&amp;</span> grav_size <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">text</span></pre></div></div>

<p>This is getting a bit long so I&#8217;m going to break it into multiple parts.</p>
<p><del datetime="2009-03-22T22:34:54+00:00">In part 2 I&#8217;ll cover how to set an Address Book picture from AppleScript and how to detect if Gravatar doesn&#8217;t have an image for an address instead of ending up with a default image we don&#8217;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.</del></p>
<p>I changed my mind and decided to make the parts into smaller posts covering some of the individual tasks. I&#8217;m hoping this will make it easier for others to find these solutions for use in their own projects.</p>
<p>Part 2: <a href="http://smithsrus.com/setting-an-address-book-picture-with-applescript/">Setting an Address Book Picture with AppleScript</a><br />
Part 3: <a href="http://smithsrus.com/detecting-when-gravatar-has-no-image/">Detecting When Gravatar Has No Image to Match an E-mail Address</a><br />
Part 4: Putting it all together with a <a href="http://smithsrus.com/filling-mac-address-book-pictures-with-gravatars/">Script to Update Address Book Contacts with Gravatar Images</a>.</p>
<p>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&#8217;ll construct the remaining posts based on feedback.</p>
]]></content:encoded>
			<wfw:commentRss>http://smithsrus.com/gravatar-applescript-and-address-book-pictures/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)

Served from: smithsrus.com @ 2010-07-31 03:53:42 -->