How to Use a TextExpander Snippet to Quickly Generate Amazon Affiliate Links

TextExpander is a wonderful, powerful utility for OS X and iOS. I’m not nearly the snippet maestro the other Brett is, but I use it and love it all the same.

Not long ago, a young Twitter pal named Nic asked me if I knew of TextExpander snippet for generating Amazon affiliate URLs. I didn’t, so I spent a few minutes making one.

First, I’ll cover how to use it. Then, how to works. After that, we’ll get into the caveats. Cool? Ok then.

How to create the snippet

Create a new snippet in TextExpander. At the top of the big text area where the snippet value will go, change the type to “Shell script”. Then copy and paste this code (you can copy easily by clicking “view raw” at the bottom of the box containing the code):

[gist][/gist]

Next, change the value of affcode to whatever your affiliate code is (or, if you’re feeling particularly generous, you could leave mine in there!).

Assign the snippet a shortcut at the bottom of the window. I use ‘;;amzaff’, but whatever you want is fine. When you’re done, the snippet definition in TextExpander should look like this:

Assuming neither you nor I crapped the bed, we should be ready to successfully use this thing.

How to use the snippet

Copy the URL of an Amazon product page to your clipboard. It doesn’t matter if it already has an affiliate code in the URL. Then, issue your shortcut where you’d normally type a URL (browser, email, whatever). If everything goes as planned, the shortcut should be replaced by a nicely formatted Amazon product page URL that contains your affiliate code.

For example, copy this URL to your clipboard:

http://www.amazon.com/DOMAGRON-Novelty–780984463366-Fake-Stress/dp/B0054QV2A2/ref=sr_1_cc_3?s=aps&ie=UTF8&qid=1342330133&sr=1–3-catcorr

In your browser’s address bar, delete everything and type your shortcut. It should spit out something like this:

http://www.amazon.com/DOMAGRON-Novelty–780984463366-Fake-Stress/dp/B0054QV2A2/ref=sr_1_cc_3?tag=nerdgap–20

How this thing actually works

Note: this section will cover nerdy stuff. Skip to “Caveat Expandor” if you don’t care about nerdy stuff.

This script, at a high level, does the following:

  1. Grabs whatever is currently in the system clipboard and assigns it to the variable rawurl.
  2. All of the query string parameters are removed from the URL (the ampersand and everything after it, in other words). If there are none, it works as expected.
  3. Feeds rawurl to Python’s urlparse library function. If an exception is raised, the original value of rawurl is sent to standard output (STDOUT) and appears wherever the user invoked the snippet.
  4. Assuming the urlparse stuff works, it then checks the domain of the URL to make sure it’s an Amazon URL. If it’s not, then rawurl is returned (just like in the previous step).
  5. If it passes that test, then the URL is rebuilt and the user’s affiliate code is appended.
  6. The new URL is written to standard output where TextExpander regains control and plugs it in wherever the user invoked the snippet.

Caveat Expandor

This is a very basic implementation. If I were a real programmer and had more than twenty minutes to bang on this, I’d write something a good deal more robust that checked for valid Amazon domains or maybe requested the affiliate URL using Amazon’s API. But I’m not, so I didn’t.

And, as with any random hunk of code you find on the web, I make no guarantees as to the servicability of this little hack. This works for me and for Nic (I asked), so that’s good enough for me. If it eats your kitten or something, I’m afraid I can’t do much to help you.

That said, I hope you find this useful. If you do, give me a shout on Twitter and let me know. Oh, and go buy TextExpander because it’s awesome.

Like this Post? Tell Your Pals!
Get More Great Tips