Amazon Affiliate Links via TextExpander

I've been using a few different apps over the years to apply Amazon affiliate codes to products I link here and sometimes on the podcasts. The thing about those apps, however, particularly the Mac apps, is that they are all different flavors of horrible.

So a few weeks ago I decided to roll my own with a little automation. These types of automations are usually pretty easy to build because you *just know* someone else has already done it. In this case, I used as a starting point Dr. Drang's AppleScript, who was inspired by Gabe Weatherhead.

Here is Drang's Script:

set myTag to “INSERT_YOUR_AMAZON_AFFIL_HERE” tell application "Safari" to set theURL to the URL of the front document set cmd to "echo '" & theURL & "' | perl -pe 's#^.\*/(?:dp|gp/product)/([^/?]+).\*$#$1#'" set itemID to do shell script cmd set aLink to "http://www.amazon.com/gp/product/" & itemID & "?tag=" & myTag

The problem with Drang’s script is that it is too clever for me. Particularly this line:

tell application "Safari" to set theURL to the URL of the front document

I actually use a JavaScript plugin in Safari to grab links and I don’t want the script to automatically grab a link from the front Safari window. I’d rather it just use the current link in the clipboard. So I changed that line to something easier.

set theURL to the clipboard

So this gets to my modified script:

set myTag to "macs03-20" set theURL to the clipboard set cmd to "echo '" & theURL & "' | perl -pe 's#^.*/(?:dp|gp/product)/([^/?]+).*$#$1#'" set itemID to do shell script cmd set aLink to "http://www.amazon.com/gp/product/" & itemID & "?tag=" & myTag

Once I nailed down the script, I added it to TextExpander as an AppleScript snippet. Here’s an image of the snippet

Click to enlarge

Finally, if you’d like to just download the AppleScript, here you go. One note, you’ll need to replace “macs03-20” (my affiliate link), with your own Amazon affiliate link.

Download the Script