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.
- 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.