nathan’s blog - kickass stuff from and for the interwebs
Filed under

tutorial

 

Everybody Stand Back. I [kinda] Know Regular Expressions!

Building Snow Leopard Services with Automator & Ruby

Yesterday, Justin Williams posted a quick how-to on using Automator in Mac OS X Snow Leopard to create a service that helps you build clean Amazon affiliate links.

Justin's instructions create a workflow that requires you to manually select just the item's ASIN code, then uses AppleScript to concatenate the rest of the short URL together. He notes: 

"The workflow on this isn’t perfect given that you can’t just copy in the entire URL, but given AppleScript’s lack of regular expression functionality, this was the best route I could think of taking."

I thought to myself, "hey, that'd be super-easy to do in Ruby". After a quick perusal of the available Automator actions, I discovered that you can select a Ruby, Python or Perl interpreter with the "Run Shell Script" action. Oh snap!
[Note: apparently the Ruby/Python/Perl support in Automator isn't new. It's there in 10.5 too]

As Justin noted, your typical Amazon URL is way too long and has lots of icky cruft in it. Take this link to the new Beatles Box Set, for example:

http://www.amazon.com/gp/product/B002BSHWUU/ref=s9_simz_gw_s0_p15_i1?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-2&pf_rd_r=0SA8PWTFHCQY477B6WQ8&pf_rd_t=101&pf_rd_p=470938631&pf_rd_i=507846/ref=nosim/nathos-20

I know. Eww. In contrast, the shortest possible link for an item basically has Amazon's base URL, the item's ASIN code, and an affiliate ID:

http://www.amazon.com/dp/B002BSHWUU/ref=nosim/nathos-20

With a head start from a guy at Microsoft (!) and some quick expression testing with Rubular, I had a working regex and the start of an improved "Create Amazon Affiliate Link" service:

To recreate this service on your own, you'll need to launch Automator and select the "Service" template for your workflow.

You may have noticed that your 'Services' menu (under the Application menu) is far less cluttered than in Mac OS X 10.5. This is because Snow Leopard now uses data detectors to determine if a given service applies to the current app or selection. In this case, we only want our service to be available if a URL has been selected. To do so, set the "Service receives selected" option at the top of the workflow to "URLs". I also suggest that you restrict your service to Safari, so go ahead select it from the adjacent drop-down menu.

Next, scroll to or search for the "Run Shell Script" action, and drag it into your workflow. Set the "Shell:" to "/usr/bin/ruby" and then type or paste in the following:

Change the value of 'affiliate_id' (shown here as my ID, 'nathos-20') to your own Amazon affiliate ID. Don't forget the '-20' at the end. Also, make sure the appropriate lines are properly indented.

This hunk of code features a fancy regular expression that extracts the ASIN code from any Amazon URL. It then concatenates the base Amazon URL, the ASIN, and an affiliate ID together into a final short URL. Lastly, it checks to see if the URL you processed actually contains an ASIN. If it does, the shortened URL is returned. If not, an error message is returned instead.

So far, so good. To send our URL back to the user, drag the "Copy to Clipboard" action below the shell script. That's it! Basic workflow complete.

Since the workflow can take a few seconds to run, I think it's a good idea to add some notifications to the process. If you have Growl installed, you can drag the "Show Growl Notification" action to the end of your workflow. I set my title to "Amazon Affiliate Link Created" and my description to "A new affiliate link has been copied to the clipboard."

If you want an audible notificaiton, drag the "Run AppleScript" action to the end of your workflow. You can enter something as simple as "beep" to play your default system sound:

or something fancier like "say "Affiliate Link Created"" to have your Mac talk to you:

Save your service as "Create Amazon Affiliate Link", then give it a try in Safari. Navigate to a product on Amazon.com, press Cmd-L to highlight the URL, then right-click the text and select "Create Amazon Affiliate Link". Alternatively you can activate via the application menu (Safari -> Services -> Create Amazon Affiliate Link).

Lastly, a very cool feature in Snow Leopard is the ability to assign keyboard shortcuts to your custom services. To do so, open the Keyboard System Preferences pane and click on "Keyboard Shortcuts". Scroll down to "Internet" and you'll find our new service. Assign a custom keyboard shortcut and you're up and running! (You may need to restart Safari for your new keyboard shortcut to work)

If you want to download this workflow 'pre-assembled', you can download my copy here. Unzip and place it in your ~/Library/Services folder. Double-click the workflow to open it in Automator and customize it to your liking. Enjoy!

[in case you're wondering, the title of this post is from a classic XKCD comic]

Loading mentions Retweet
Filed under  //   Amazon   Automator   Mac OS X   Ruby   tutorial  

Comments [0]