<?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/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>David R Poindexter &#187; tutorial</title>
	<atom:link href="http://davidrpoindexter.com/category/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://davidrpoindexter.com</link>
	<description>Technology. Design. Dysfunction.</description>
	<lastBuildDate>Thu, 20 May 2010 19:27:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Bit.ly URL shortening with Mac OS X Snow Leopard Services and Applescript</title>
		<link>http://davidrpoindexter.com/tutorial/bit-ly-url-shortening-with-mac-os-x-snow-leopard-services-and-applescript/</link>
		<comments>http://davidrpoindexter.com/tutorial/bit-ly-url-shortening-with-mac-os-x-snow-leopard-services-and-applescript/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 20:54:34 +0000</pubDate>
		<dc:creator>mtheoryx</dc:creator>
				<category><![CDATA[tutorial]]></category>
		<category><![CDATA[applescript]]></category>
		<category><![CDATA[automator]]></category>
		<category><![CDATA[bitly]]></category>
		<category><![CDATA[workflow]]></category>

		<guid isPermaLink="false">http://davidrpoindexter.com/?p=221</guid>
		<description><![CDATA[If you&#8217;ve seen the nifty browser shortcuts and extensions from Bit.ly then you&#8217;re familiar with the service, and how convenient it is. But there&#8217;s a catch&#8230; they only work in your browser. Read on to see how you, too, can have the same convenience from any application on Mac OS X Snow Leopard. For those [...]]]></description>
			<content:encoded><![CDATA[<p></p><p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/post_head1.png" class="image-link"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/post_head1-thumb.png" height="143" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>If you&#8217;ve seen the nifty browser shortcuts and extensions from Bit.ly then you&#8217;re familiar with the service, and how convenient it is. But there&#8217;s a catch&#8230; they only work in your browser.</p>
<p style="clear: both">Read on to see how you, too, can have the same convenience from any application on Mac OS X Snow Leopard.</p>
<p style="clear: both">For those with no inclination to follow along, I&#8217;ve provided the workflow below. Just download this, unzip it, and place it in <em>~/username/Library/Services</em>. To use it, just select a URL in any application, right-click, and select &#8220;bitly&#8221;. The shortened URL will be on your clipboard in .22 seconds, ready for you to paste anywhere you like.</p>
<p style="clear: both"><strong><em><a href="http://davidrpoindexter.com/wp-content/freebies/bitly.workflow.zip" title="Download for free!!" target="_blank">Grab the Service &#8212; Freebie!!</a></em></strong></p>
<p>  <span id="more-221"></span>
<p style="clear: both">To start, we&#8217;re going to create a workflow with Automator. Locate it in Applications > Automator.app</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_4.38.04_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_4-thumb.38.04_PM.png" height="353" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>At the dialog that appears when creating a new workflow, choose &#8220;Service&#8221; This will be placed in <em>~/username/Library/Services</em>, and will be available by right-clicking (or control-click) > Services > thisWorkflow.</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_4.39.51_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_4-thumb.39.51_PM.png" height="97" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>Go ahead and rename your workflow now by choosing File > Save as&#8230; > bitly</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_4.38.36_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_4-thumb.38.36_PM.png" height="120" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>You&#8217;ll notice that by default, the top parameter reads that the services takes selected text in any application. We&#8217;re keeping that. </p>
<p style="clear: both">This means, contextually, the service doesn&#8217;t apply unless you have some text selected. Additionally, this will work in <strong>ANY </strong>application, unlike browser extensions and JavaScript bookmarks.</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_4.43.22_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_4-thumb.43.22_PM.png" height="313" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>The first thing we&#8217;ll do, for testing purposes, is add an action called &#8220;Get Specified Text&#8221; located under the Text category.</p>
<p style="clear: both">The reason we want to do this is that we&#8217;re going to build in some exception handling, and we need some text to test. As it stands, running the workflow will throw an error because you can&#8217;t simulate selected text in the Automator application.</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_4.47.11_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_4-thumb.47.11_PM.png" height="313" width="379" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>To start, just paste in any valid http or https URL. I&#8217;m going to use <em>http://google.com/</em> for this example.</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_4.49.33_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_4-thumb.49.33_PM.png" height="313" width="379" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>The next action we add is really the meat of the workflow. Under the Utilities category, drag in the &#8220;Run AppleScript&#8221; action. Feel free to delete all of the dummy text at this point.</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5.22.28_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5-thumb.22.28_PM.png" height="448" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>I&#8217;ll go ahead and paste in the script we&#8217;re going to use, and then I&#8217;ll run through what it does, line by line, skipping the comment header at the top.</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_4.54.49_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_4-thumb.54.49_PM.png" height="25" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>The first line simply takes the input from the previous action&#8217;s output, and brings it into our AppleScript. You&#8217;ll notice the little arrow at the bottom of the &#8220;Get Specified Text&#8221; action connected to the rounded tab at the top of the &#8220;Run AppleScript&#8221; action. That means there is output from the first action <strong> flowing </strong>into the input of the next action &#8212; hence it&#8217;s a <strong><em>workflow.</em><br /></strong></p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_4.55.49_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_4-thumb.55.49_PM.png" height="28" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>The following line takes the input, casts it to a string class, and assigns it to another variable for our use.</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5.01.59_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5-thumb.01.59_PM.png" height="27" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>We next choose to ignore the case of the string. This is generally a good idea when you&#8217;re going to test a string, unless case is important. We only want to test for the presence of &#8220;http&#8221; or &#8220;https&#8221; at the start of the string, to ensure it&#8217;s actually a URL. Feel free to get creative with additional Regular Expressions. For example, you can make sure there are no spaces or strange characters, and if so, URL-encode them. Moving on&#8230;</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5.23.14_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5-thumb.23.14_PM.png" height="68" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>Here we&#8217;re doing a comparison. If our conditions are not met, we don&#8217;t want to utilize the bitly service. In my testing, the system sound is triggered as well. We don&#8217;t want to copy that to the clipboard though, which is why we&#8217;re not &#8220;returning&#8221; anything. We&#8217;ll get to that&#8230;</p>
<p style="clear: both">Now, after the else is what fires when our conditions are met. First, we&#8217;re going to build a string. The string parts are outlined as:</p>
<ul style="clear: both">
<li><strong>set curlCMD to </strong>:<br />This just sets the string to a variable.</li>
<li><strong>&#8220;curl </strong>:<br />This is the command line program we&#8217;ll use to call bit.ly&#8217;s API</li>
<li><strong>&#8211;stderr /dev/null</strong> :<br />If we get an error back, route it to nowhere. You can add exception handling for this later if you&#8217;d like.</li>
<li><strong>\&#8221;http://bit.ly/api</strong> :<br />This is the URL of bit.ly&#8217;s api. Notice the backslash before the double quote. This is known as character escaping. It&#8217;s important here, so just put it in ;p</li>
<li><strong>?url=&#8221;</strong> :<br />This is the start of the query string &#8212; do a Google search sometime and look at the URL in your browser. You&#8217;ve been using query strings for so long that you forgot what they were.</li>
<li><strong>&#038; input &#038; &#8220;\&#8221;"</strong> :<br />Here we concatenate the string of input we received in our script right onto the end, and add the closing double quotes.
</li>
</ul>
<p style="clear: both">Right, so now you understand what the URL is about. Now, remember, we have just built a command here as a string &#8212; we haven&#8217;t actually executed the command. That&#8217;s next&#8230;</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5.10.26_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5-thumb.10.26_PM.png" height="31" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>Remember your order of operations from your math classes? You work on the part in the parenthesis first. This executes our command string on the Mac OS X command line. When we call this (<em>hint</em>: you can do it right in your browser too), bit.ly returns the shortened URL. We take this result, and assign it to the tinyURL variable.</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5.14.27_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5-thumb.14.27_PM.png" height="68" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>Now, some housekeeping. Close your if statement, end your ignorning block, and end running the AppleScript. This completes your second Automator action in this workflow.</p>
<p style="clear: both">Now that we have input that we want, we have created a short URL from that input, we need to return the input. Let&#8217;s go ahead and do some testing before our last steps.</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5.15.40_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5-thumb.15.40_PM.png" height="148" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>If you click the &#8220;Results&#8221; button at the bottom of your action window, you&#8217;ll see a drawer pop out. Now, go to the very top and click &#8220;Run&#8221; and see what happens.</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5.16.01_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5-thumb.16.01_PM.png" height="144" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>See? You got a bit.ly shortened URL back. This is good, but it&#8217;s not a &#8220;real&#8221; test&#8230;</p>
<p style="clear: both">Remember the very first action? We specified the URL to use as input, and we made sure we put a good URL in there. Technically, we don&#8217;t know if the input is being tested correctly against our conditions, so we&#8217;ll do that next.</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5.17.56_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5-thumb.17.56_PM.png" height="102" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>Change your URL in the &#8220;Get Specified Text&#8221; action to &#8220;<em>ftp://google.com/</em>&#8220;</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5.26.52_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5-thumb.26.52_PM.png" height="140" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>Now run your workflow and look at the results. You should hear an alert, and see an error diaglog that you have to dismiss. Now that we know our test is working, change the URL back to &#8220;<em>http://google.com/</em>&#8221; and we&#8217;ll finish up the workflow.</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5.29.34_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5-thumb.29.34_PM.png" height="134" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>Under the Utilities category, drag in the &#8220;Copy to Clipboard&#8221; action. It expects text as input, and that&#8217;s just what we&#8217;ll give to it. Click the Results button on the &#8220;Copy to Clipboard&#8221; action. What you see is exactly what would have been placed on your clipboard if this workflow were live. Let&#8217;s make it live now&#8230;</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5.31.22_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5-thumb.31.22_PM.png" height="376" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>Delete the &#8220;Get Specified Text&#8221; action by clicking the X in the upper right corner. You&#8217;ll notice that now you output from the very first action (the one that gets selected text in any application) and it&#8217;s going in as input into our &#8220;Run AppleScript&#8221; action.</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5.33.58_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5-thumb.33.58_PM.png" height="201" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>Save your workflow, and place it in the <em>~/username/Library/Services</em> folder. On OS X 10.6 Snow Leopard, this adds the workflow as a contextual service to the Services menu of all applications, or when you right click.</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5.38.51_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5-thumb.38.51_PM.png" height="521" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>Now, go into any application, select the text for a URL. Right-click (or control-click) and select Services > bitly.</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5.39.39_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5-thumb.39.39_PM.png" height="211" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>Now, wait for half a second (we are contacting bit.ly&#8217;s web API, so it&#8217;s not instant), and then just paste whatever&#8217;s on your clipboard into your browser. Go to the site, and you&#8217;ll see, it&#8217;s the URL that you originally copied. Neat, huh?</p>
<p style="clear: both"><a href="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5.41.54_PM.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://davidrpoindexter.com/wp-content/uploads/2009/10/Screen_shot_2009-10-26_at_5-thumb.41.54_PM.png" height="343" width="380" style=" text-align: center; display: block; margin: 0 auto 10px;" /></a>You can make the deal <em>even sweeter</em> by assigning a keyboard shortcut to your new service. Go to System Preferences > Keyboard > Keyboard Shortcuts > Services and find your &#8220;bitly&#8221; services. Pick whatever shortcut you want, but steer clear of any application- or system-level shortcuts to avoid collisions. I chose Control-Command-B for mine.</p>
<p style="clear: both">Practical applications? The biggest one I can think of is links in IMs and emails. Any links longer than 70 characters gets broken in Apple&#8217;s Mail.app.</p>
<p style="clear: both">If you enjoyed this article, I would like to hear your wishlist for similar articles. What would you like to learn about? What would you like to see broken down into visual step-by-step instructions? What are you interested in exploring? Let me know in the comments section below.</p>
<p>  <br class="final-break" style="clear: both" /></p>
]]></content:encoded>
			<wfw:commentRss>http://davidrpoindexter.com/tutorial/bit-ly-url-shortening-with-mac-os-x-snow-leopard-services-and-applescript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
