Simpler Affiliate Links
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:
:::Applescript
tell application "Keyboard Maestro Engine"
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.