My affiliate linking scripts were starting to get out of hand with different versions in Keyboard Maestro, Pythonista, Editorial, Drafts, Workflow, and TextExpander on the Mac. It was difficult to know what scripts worked where and if they were up to date. I decided to expunge them all but one.

The one would have to work equally well on iOS and OS X as I use them equally and interchangeably. Until now this was not possible as none of those apps was on both devices, but with TextExpander getting JavaScript support, I thought it might now be possible.1

Code explanation

  • Line 3: Stores the affiliate code to a variable.
  • Line 4: Stores the clipboard to a variable.
  • Line 5: Defines the regular expression and stores it to a variable. My choice of regex looks for the first string of all capital letters or numbers following a slash, which seems to always be the Amazon (ASIN).
  • Line 6: Runs the match for the regular expression, returning null if it isn’t found and an array of it is.
  • Line 8-13: Evaluates the result of the regex search. If it failed, it returns the original text. If it succeeds, it stores ASIN to a variable and creates the new link.
  • Line 15: Writes the new URL to the clipboard. This isn’t necessary for the expansion of the affiliate link by TextExpander as the result of the script is what is expanded, not the clipboard, but I like having the link in the clipboard in case I need to paste it somewhere else immediately.
  1. Dr. Drang had written about converting his script over to JavaScript for TextExpander, and at first I thought I could just steal his, but this was a dead end. Since he used JavaScript for Automation in his snippet it would not work in iOS and his regex didn’t work with the URLs I was getting.