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):
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:
In your browser’s address bar, delete everything and type your shortcut. It should spit out something like this:
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:
- Grabs whatever is currently in the system clipboard and assigns it to the variable
- 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.
urlparselibrary function. If an exception is raised, the original value of
rawurlis sent to standard output (STDOUT) and appears wherever the user invoked the snippet.
- Assuming the
urlparsestuff works, it then checks the domain of the URL to make sure it’s an Amazon URL. If it’s not, then
rawurlis returned (just like in the previous step).
- If it passes that test, then the URL is rebuilt and the user’s affiliate code is appended.
- The new URL is written to standard output where TextExpander regains control and plugs it in wherever the user invoked the snippet.
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.