Amazon Affiliate Links And Textexpander

Amazon Affiliate Links And Textexpander

Since switching to Chrome as my primary browser, but still occasionally using Safari, I’ve created a complicated situation with many of my TextExpander scripts, all of which assume Safari is my active browser.

I haven’t used affiliate links on this site in a very long time. However, I figure I might was well crank that baby up. I don’t plan on retiring from the massive amount of money this site will generate, but the random freebie from Amazon is still a welcome thing.

So let’s look at creating an affiliate link from Amazon. If you google that, you’ll go blind, so let’s just look at the link in it’s most simple format:

http://www.amazon.com/gp/product/B004CLYJ2I?tag=YourAmazonAffiliateID

That’s the link we want to create. However, Amazon links come in two forms (as far as I’ve been able to determine). One uses dp in the link and the other uses both gp and product, like the example link above. Unfortunately, in your browser, most links look something like this:

http://www.amazon.com/gp/product/B004CLYJ2I/ref=s9_simh_gw_p422_d0_i1?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-2&pf_rd_r=1XHN51E08SSE6DEC1916&pf_rd_t=101&pf_rd_p=1389517282&pf_rd_i=507846

I tried several variations of converting that mess with AppleScript and finally punted to Perl because (1) it’s easier, (2) it works, and (3) any port in a storm. Here’s the Perl script:

!/usr/bin/perl -w

use strict; use Getopt::Long;

my $amazonID = ‘MyAmazonID’;

my ($URL); GetOptions( “url=s” => $URL, );

unless ($URL) { print “itemID NOT FOUND”; exit; }

my $itemID;

if ($URL =~ /\/gp\/product\/(.)\/?/) { $itemID = $1; } elsif ($URL =~ /\/dp\/(.)\//) { $itemID = $1; } else { $itemID = “itemID NOT FOUND”; }

print ‘http://www.amazon.com/gp/product/’ . $itemID . ‘?tag=’ . $amazonID;

This script will handle whichever version of URL that’s thrown at it. Be sure to change MyAmazonID to your Amazon affiliate id. I saved mine in ~/Dropbox/bin/parse-amazon.pl so that it’ll be available on every computer I use. Be sure to make it executable:

$ chmod +x ~/Dropbox/bin/parse-amazon.pl

Now we move to our AppleScript. After doing some googling, I found a solution to determining which browser is running.

if appIsRunning(“Safari”) then tell application “Safari” to set theURL to URL of current tab of window 1 else tell application “Google Chrome” to set theURL to URL of active tab of first window end if

set cmd to “/Users/userid/Dropbox/bin/parse-amazon.pl -u “ & theURL set itemID to do shell script cmd

itemID

on appIsRunning(appName) tell application “System Events” to (name of processes) contains appName end appIsRunning

At the end of the script, we have a function that determines if, whatever we pass it, is currently running. It returns true or false. At the first of the script, we call our function, asking if Safari is running. If not, we assume Chrome is running. Regardless of which is running, we copy the URL from the current tab.

Then we call our Perl script. You’ll need to modify this line (set cmd to) to point to where ever you stored that Perl script.

Take this AppleScript and load it into TextExpander. Set the content to AppleScript. Give it a label and an abbreviation, and you’re good to go.

The downside to this approach is that if both Safari and Chrome are running, the script will pull the URL of the foremost Safari tab. In other words, it defaults to Safari. You can change that if you want. Or, if you end up with some weird output, quit Safari and re-enter the TextExpander abbreviation.

Note: There is some weirdness with this script. I recommend firing up both Chrome and Safari, then running the script. Quit out of each, then you should be good to go. I have no idea why this is, but it worked for me. I had to do it on both Lion and Mountain Lion. Yeah, weird.