<?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; applescript</title>
	<atom:link href="http://smithsrus.com/category/applescript/feed/" rel="self" type="application/rss+xml" />
	<link>http://smithsrus.com</link>
	<description>Technology, home automation, usability, and whatever else amuses me.</description>
	<lastBuildDate>Tue, 16 Apr 2013 00:38:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Gravatar, AppleScript, and the OS X Address Book Revisited</title>
		<link>http://smithsrus.com/gravatar-applescript-and-the-os-x-address-book-revisited/</link>
		<comments>http://smithsrus.com/gravatar-applescript-and-the-os-x-address-book-revisited/#comments</comments>
		<pubDate>Tue, 11 May 2010 00:19:56 +0000</pubDate>
		<dc:creator>Doug Smith</dc:creator>
				<category><![CDATA[applescript]]></category>
		<category><![CDATA[Gravatar]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://smithsrus.com/?p=160</guid>
		<description><![CDATA[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&#8217;ve learned a few things since then, plus the Gravatar service has [...]]]></description>
				<content:encoded><![CDATA[<p>A while back I did a series of posts (<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>, <a href="http://smithsrus.com/detecting-when-gravatar-has-no-image/">part 3</a>, and <a href="http://smithsrus.com/filling-mac-address-book-pictures-with-gravatars/">part 4</a>) 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.</p>

<p>I&#8217;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&#8217;s time for an update to my now obsolete posts.<span id="more-160"></span></p>

<h3>Reference Implementation One: Gravatar to an AppleScript Variable</h3>

<p>The most common use case of Gravatar in AppleScript is probably to grab someone&#8217;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&#8217;s an example of looking up a Gravatar and storing the raw picture data in a variable.</p>

<p>Here&#8217;s the script: (<a href="applescript://com.apple.scripteditor?action=new&#038;script=--%20Download%20a%20Gravatar%20image%20for%20the%20given%20e-mail%20address%20and%20store%20in%20a%20variable%20as%20a%20TIFF%20image.%20Display%20error%20if%20a%20matching%20gravatar%20doesn%27t%20exist.%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--%20Calculate%20an%20MD5%20for%20the%20e-mail%20address.%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%0D%0D--%20Construct%20the%20Gravatar%20URL.%0Dset%20grav_url%20to%20quoted%20form%20of%20%28%22http%3A//gravatar.com/avatar/%22%20%26%20md5_email%20%26%20%22%3Fd%3D404%22%20as%20text%29%0D%0D--%20Get%20the%20Gravatar%20image%20with%20a%20timeout%20of%2010%20seconds.%0Dtry%0D%09set%20pict_data%20to%20do%20shell%20script%20%22curl%20-fsS%20%22%20%26%20grav_url%20%26%20%22%20-m%2010%22%20as%20TIFF%20picture%20without%20altering%20line%20endings%0D%09tell%20me%20to%20display%20dialog%20%22Yes%2C%20we%20have%20a%20matching%20Gravatar.%22%20buttons%20%7B%22Okay%22%7D%20default%20button%201%0Don%20error%0D%09tell%20me%20to%20display%20dialog%20%22Sorry%2C%20there%20was%20no%20matching%20Gravatar.%22%20buttons%20%7B%22Cancel%22%7D%20default%20button%201%0Dend%20try%0D">Open in Script Editor</a>)</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- 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.</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;">-- 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 <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.</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;?d=404&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;">-- Get the Gravatar image with a timeout of 10 seconds.</span>
<span style="color: #ff0033; font-weight: bold;">try</span>
	<span style="color: #ff0033; font-weight: bold;">set</span> pict_data <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;curl -fsS &quot;</span> <span style="color: #000000;">&amp;</span> grav_url <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot; -m 10&quot;</span> <span style="color: #ff0033;">as</span> TIFF picture <span style="color: #ff0033; font-weight: bold;">without</span> altering line endings
	<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;Yes, we have a matching Gravatar.&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;">on</span> <span style="color: #ff0033; font-weight: bold;">error</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;Sorry, there was no matching Gravatar.&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>
<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">try</span></pre></td></tr></table></div>


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

<h3>Reference Implementation Two: Gravatar to a File</h3>

<p>There may be some cases where it&#8217;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.</p>

<p>Here&#8217;s the script: (<a href="applescript://com.apple.scripteditor?action=new&#038;script=--%20Download%20a%20Gravatar%20image%20for%20the%20given%20e-mail%20address%20and%20store%20in%20a%20temporary%20file.%20Display%20error%20if%20a%20matching%20gravatar%20doesn%27t%20exist.%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%22doug%40smithsrus.com%22%0D%0D--%20Calculate%20an%20MD5%20for%20the%20e-mail%20address.%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%0D%0D--%20Construct%20the%20Gravatar%20URL.%0Dset%20grav_url%20to%20quoted%20form%20of%20%28%22http%3A//gravatar.com/avatar/%22%20%26%20md5_email%20%26%20%22%3Fd%3D404%22%20as%20text%29%0D%0D--%20Make%20a%20file%20name%20in%20which%20to%20temporarily%20save%20the%20Gravatar.%0Dset%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%0Dset%20grav_POSIX_file%20to%20quoted%20form%20of%20POSIX%20path%20of%20grav_file%0D%0D--%20Download%20the%20Gravatar%20image%20to%20the%20temporary%20file%20with%20a%20timeout%20of%2010%20seconds.%0Dtry%0D%09do%20shell%20script%20%22curl%20-fsS%20%22%20%26%20grav_url%20%26%20%22%20-m%2010%20-o%20%22%20%26%20grav_POSIX_file%0D%09tell%20me%20to%20display%20dialog%20%22Yes%2C%20we%20have%20a%20matching%20Gravatar%20downloaded%20to%20%22%20%26%20grav_POSIX_file%20%26%20%22.%22%20buttons%20%7B%22Delete%20the%20File%22%7D%20default%20button%201%0D%09%0D%09tell%20application%20%22System%20Events%22%0D%09%09delete%20file%20grav_file%0D%09end%20tell%0Don%20error%0D%09tell%20me%20to%20display%20dialog%20%22Sorry%2C%20there%20was%20no%20matching%20Gravatar.%22%20buttons%20%7B%22Cancel%22%7D%20default%20button%201%0Dend%20try%0D%0D">Open in Script Editor</a>)</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- 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.</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;">-- 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 <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.</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;?d=404&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: #ff0033; font-weight: bold;">try</span>
	<span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;curl -fsS &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
	<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;Yes, we have a matching Gravatar downloaded to &quot;</span> <span style="color: #000000;">&amp;</span> grav_POSIX_file <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;.&quot;</span> <span style="color: #0066ff;">buttons</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">&quot;Delete the File&quot;</span><span style="color: #000000;">&#125;</span> default button <span style="color: #000000;">1</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: #0066ff;">delete</span> <span style="color: #0066ff;">file</span> grav_file
	<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;">on</span> <span style="color: #ff0033; font-weight: bold;">error</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;Sorry, there was no matching Gravatar.&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>
<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">try</span></pre></td></tr></table></div>


<h3>Filling Address Book with Gravatars</h3>

<p>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 <a href="http://momo.brauchtman.net/2009/03/missing-link-update-contact-image-in-macs-address-book-using-gravatar/">Moritz Haarmann for his Gravatar Address Book plugin</a>, 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.</p>

<p>Rather than list the full script here, you can just <a href="http://smithsrus.com/dl/Gravatar-to-Address-Book-Pictures.zip">download the application</a>. 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.</p>

<p>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!</p>
]]></content:encoded>
			<wfw:commentRss>http://smithsrus.com/gravatar-applescript-and-the-os-x-address-book-revisited/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
