Last night I posted a TextExpander snippet that automated the procedure for generating Apple affiliate links for the items in iTunes and Mac App Stores. This morning I got an email from David Smith, who pointed out that affiliate links can be much shorter and easier to read if you’re willing to forgo the click tracking that LinkShare does. He linked to his own post from last week that describes the process very well. I liked David’s way of making affiliate links so much I’ve dropped last night’s snippet and made a new one.

The one advantage of the previous snippet is that it allows you to use LinkShare to track clicks as well as purchases. With the new snippet, you can track purchases only. To me, that’s a small price to pay to go from frightening looking URLs like this

http://click.linksynergy.com/fs-bin/stat?id=L4JhWyGwYTM
&offerid=146261&type=3&subid=0&tmpid=1826
&RD_PARM1=http%253A%252F%252Fitunes.apple.com%252Fus
%252Fapp%252Fnotesy-for-dropbox%252Fid386095500
%253Fmt%253D8%2526uo%253D4%2526partnerId%253D30


to nice, easy-to-read ones like this

http://itunes.apple.com/us/app/notesy-for-dropbox/
id386095500?mt=8&partnerId=30&siteID=L4JhWyGwYTM


(In both cases, I’ve inserted line breaks so you can see the whole thing without horizontal scrolling.)

As before, the links are generated through a Python script:

python:
1:  #!/usr/bin/python
2:
3:  from subprocess import check_output
4:  from sys import stdout
5:
6:  # The procedure followed here is taken from Apple's instructions for for linking:
8:
9:  # My affiliate ID.
10:  myID = 'useyourown'
11:
12:  # Get the URL from the clipboard.
13:  clipURL = check_output('pbpaste')
14:
15:  # Add my ID and the partnerId parameter to the URL. If there are already
17:  if '?' in clipURL:
18:    itemURL = '%s&partnerId=30&siteID=%s' % (clipURL, myID)
19:  else:
20:    itemURL = '%s?partnerId=30&siteID=%s' % (clipURL, myID)
21:
22:  # Write it out
23:  stdout.write(itemURL)


which you can copy and paste into a new TextExpander shell snippet:

The script is so much simpler than the LinkShare-ified version I wrote before, I’m almost embarrassed to post it. The only real logic is in Lines 17-20, which decide whether the partnerID parameter should be preceded by a & or a ?.

The one thing you’ll have to do if you want to use this snippet yourself is change Line 10 to put your own affiliate ID between the single quotation marks. You can pull your ID out of any affiliate link you might have generated in the past through Apple’s Link Maker tool. Look for a portion of the URL near the beginning that looks like

…/stat?id=L4JhWyGwYTM&offerid…


Your ID is the alphanumeric string between the = and the &.

You can, of course, use whatever abbreviation you like for the snippet; I settled on ;aal for “Apple affiliate link.”

The procedure for using this snippet is basically the same as the one I gave last night:

1. Find the item you want to link to in iTunes, the Mac App Store, or in your browser on one of Apple’s product web pages.

2. Copy the link to the item from either the popup menu (in iTunes or the App Store) or the toolbar (in your browser).

3. Switch to wherever you want to put the link and type ;aal to insert the URL.

As I said in last night’s post, I’d like to cut out Step 2 but can’t figure out how without resorting to GUI scripting, which I’m always leery of.

With this snippet, you can make affiliate links to

Update 1/7/12
If you’re using Snow Leopard or earlier, you probably have a version of Python earlier than Python 2.7, which means your subprocess library won’t have the check_output convenience function. You’ll need to use this version of the script instead:

python:
1:  #!/usr/bin/python
2:
3:  from subprocess import *
4:  from sys import stdout
5:
6:  # The procedure followed here is taken from Apple's instructions for for linking:
8:
9:  # My affiliate ID.
10:  myID = 'useyourown'
11:
12:  # Get the URL from the clipboard.
13:  clipURL = clipURL = Popen('pbpaste', stdout=PIPE).communicate()[0]
14:
15:  # Add my ID and the partnerId parameter to the URL. If there are already
17:  if '?' in clipURL:
18:    itemURL = '%s&partnerId=30&siteID=%s' % (clipURL, myID)
19:  else:
20:    itemURL = '%s?partnerId=30&siteID=%s' % (clipURL, myID)
21:
22:  # Write it out
23:  stdout.write(itemURL)


The difference are in Lines 3 and 13.

This script will work under Python 2.7, too, but Line 13 is distinctly more opaque.

Frankly, the whole subprocess module—and the popen2 module that preceded it as the Pythonic way of shelling out—stinks on ice. Anyone who wants to run shell commands from within a script already knows the shell syntax; forcing them to use an unnatural syntax, no matter how Pythonic it may be, is wasteful and stupid. Perl and Ruby are much smarter about this. I agree with this guy and will be checking out his Envoy library.