I have a new saying: “If you want something done right, just pique Dr.Drang’s interest.” There’s been constant improvement to this workflow and it’s interesting to see it evolve.
Here’s a bonus macro that creates Amazon affiliate links.[1] For this one, just copy the product link from Amazon and run the macro to extract the product item and create an affiliate link.[2]
Here’s the workflow:
- Visit an Amazon product page
- Copy the page link
- Switch back to a document I want the link inserted in
- Run the macro to have the affiliate link pasted in
Here’s the AppleScript:
try
set oldDelims to AppleScript's text item delimiters -- save their current state
set myClip to (process tokens "%Variable%tempClip%")
if myClip does not contain "/dp/" and myClip does not contain "/product/" then
return "Did Not Find An Amazon Link"
else
if myClip contains "/dp/" then
set AppleScript's text item delimiters to "/dp/"
end if
if myClip contains "/product/" then
set AppleScript's text item delimiters to "/product/"
end if
set tempString to text item 2 of myClip
set AppleScript's text item delimiters to "/"
set tempString to text item 1 of tempString
return tempString
end if
set AppleScript's text item delimiters to oldDelims -- restore them
on error
set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong
end try
end tell
Notes
When I’m logged into Amazon, the product page link has a different structure. The AppleScript needs to determine what kind of link I’ve copied. From what I can tell, Amazon product links have either a path that includes “/product/” or “/dp/” right before the product id. That’s what the AppleScript if-else statements are determining.
This macro also has an example of the Keyboard Maestro flow-control actions. If the AppleScript returns a failure, then KM will display a popup message rather than inserting text.
EDIT: I just realized, after using this macro for so long, that I forgot to include a try-catch block to reset AppleScript’s text delimiters. That’s just dumb.


I admire the bravery of anyone who tries to do significant text manipulation in AppleScript. I wimped out and made a call to Perl:
http://www.leancrew.com/all-this/2011/03/automating-amazon-associates/
Sadly, I can’t use this TextExpander snippet anymore, as Amazon shut down its Associates program in Illinois when some tax laws changed.
I know you’re not asking for advice, but I can’t help myself. One thing you might consider is having AppleScript get the URL of the frontmost browser tab for you. For Safari, it’s
tell application “Safari” to set myURL to URL of front document
and for Chrome, it’s
tell application “Google Chrome”
set frontIndex to active tab index of front window
set myURL to URL of tab frontIndex of front window
end tell
(I feel certain the Chrome code is going to get mangled. Do you allow HTML or Markdown in your comments?)
This’ll turn your 4-step process into a 3-step process. More important, it gives you the opportunity to use your clipboard for other things after you’ve found the product page. As long as you keep the product tab frontmost in your browser, you can go back to your text editor and do all kinds of editing with the clipboard before running the script to insert the link.
Ha! I knew you couldn’t resist. I have several macros that do just that and grab the frontmost window url. For some reason I haven’t modified this one to work that way. In fact, I’m thinking about dropping that method all together and just dumping links straight into MD and do the conversions later.
Often, I’ll sketch out a post and gather links when I’m on my iPhone or iPad during a lunch break. I can work in Simplenote and then generate the affiliate links when I’m back at home on my Mac. In that case I just copy the MD link and run the macro.