Automatically create and shorten Amazon affiliate links with Applescript
After a little finagling, I managed to update my old Applescripts that take any arbitrary Amazon URL and add in an affiliate link to one that is then automatically shortened with the is.gd service. And here it is, for the (Mac-using) world to use as you feel fit.
set link to the clipboard
if link contains "amazon.com/" then
if link contains "/o/" then
set startLength to the offset of "/o/" in link
set startLength to startLength + 8
set endCutoff to startLength + 9
set asin to text startLength thru endCutoff of link as textelse if link contains "/dp/" then
set startLength to the offset of "/dp/" in link
set startLength to startLength + 4
set endCutoff to startLength + 9
set asin to the text startLength thru endCutoff of link as textelse if link contains "/gp/product/?ASIN=" then
set startLength to the offset of "/gp/product/?ASIN=" in link
set startLength to startLength + 18
set endCutoff to startLength + 9
set asin to the text startLength thru endCutoff of link as textelse if link contains "/gp/product/" and link does not contain "?ASIN=" then
set startLength to the offset of "/gp/product/" in link
set startLength to startLength + 12
set endCutoff to startLength + 9
set asin to the text startLength thru endCutoff of link as text
end if
--asin is now set-- CHANGE YOUR AFFILIATE LINK(S) HERE BEFORE SAVING!
--
-- This can be easily modified to add in additional codes, or remove
-- the whole dialog thing and just set the variable directly if you have
-- only one link (which is probably the case)-- If you've only got one code, remove from here down to...
display dialog "Which affiliate link should be used?" buttons {"Blog - firsblo0a-20", "OTHERSITE - SOMELINK-20"} default button 1
set the button_pressed to the button returned of the result
if the button_pressed is "Blog - firsblo0a-20" then
set affiliateCode to "firsblo0a-20"
else
set affiliateCode to "OTHERSITE-20"
end if
-- here - stop removing stuff! and just use: set affiliateCode to "yourcode-20"set amazonLink to "http://www.amazon.com/gp/product/" & asin & "?tag=" & affiliateCode
set shortLink to (do shell script "curl http://is.gd/api.php?longurl=" & amazonLink)
set the clipboard to shortLinkdisplay dialog "Your shortened URL is " & shortLink & " and has been copied to the clipboard." buttons {"Woot!"} default button 1
end if
Of course, you'll want to make sure you change your affiliate link(s) appropriately. Or don't, and let me reap the benefits :) Read the script comments on what to change and where. I know the approach to grabbing the ASIN is kind of gross, but it works.
Made available under an MIT License. Questions? Hit the comments section and I'll answer as best I can.
And yes, you could swap it out easily enough to any of the other myriad url shortening tools. is.gd is probably the shortest out there, and since the API returns nothing other than the link itself (no XML markup, etc), it's also as simple to work with as computationally possible. If you don't care about affiliate links and just want to be able to send out tweets 1% faster, then just take the first line, and the fourth- and third-to-last, and tweak accordingly (it should be obviously enough). You know, for when a bookmarklet is too much work.
Just browse to any Amazon item, copy the URL to the clipboard, and run the script. Goes awesome with Quicksilver.
Now go sell stuff.
