Affiliate links with Fake

One of my regrets is not reading Gabe Weatherhead at Macdrifter earlier. Like Clark Goble and Brett Terpstra, he has one of those consistently interesting and helpful blogs for people who like to use scripting to show their Mac who’s boss.

This past weekend, he wrote a post on how to use Todd Ditchendorf’s Fake app combined with Keyboard Maestro to automate the retrieval of affiliate links to apps in the iOS and Mac App Stores. It’s a great solution to a very tedious problem, and I’d use it directly if I were a Keyboard Maestro user. (Gabe’s appearance on a recent Mac Power Users podcast nearly persuaded me to go for KM, but I’m still hanging in there with FastScripts and TextExpander.1) If you use KM, just go to Gabe’s post and follow his instructions. If not, you should still read his post for the background info I’m going to skim over, but you might find my set of scripts more appropriate for your situation.

What I ended up with are two Fake workflows, one for Mac apps and one for iOS apps, and an AppleScript that controls them. I use FastScripts to call the AppleScript, but any system you have that can run an AppleScript will do.

When you run the script, it pops up this perky little dialog box:

Affiliate link getter

Type in the name of the app, click the appropriate button, and a few seconds later the affiliate link of the top search result is on your clipboard. If you have Fake’s preferences set up to play success and failure sounds, the system “Hero” sound will play to alert you that the clipboard is ready.

Fake workflow pref panel

Here’s what the Fake workflow for getting Mac app links looks like:

Fake workflow for Mac affiliate links

The workflow for getting iOS app links is basically the same, but the “media_type” in the Step 3 is set to “iOS Apps” and the XPath for the element given in Steps 5 and 6 is different.

Here’s the AppleScript that runs the workflow:

 1:  display dialog ¬
 2:    "Which app?" default answer "" with title ¬
 3:    "Affiliate Link" buttons {"Cancel", "iOS App", "Mac App"} default button 3
 4:  
 5:  copy the result as list to {theName, theType}
 6:  
 7:  if theType is not "Cancel" then
 8:    tell application "Fake"
 9:      set variable with name "appName" to theName
10:      if theType is "Mac App" then
11:        set wfPath to (get path to library folder from user domain as text) & ¬
12:          "Scripts:Mac Affiliate Link.fakeworkflow"
13:        open wfPath
14:        delay 1
15:        run workflow with variables {appName:theName}
16:      else
17:        set wfPath to (get path to library folder from user domain as text) & ¬
18:          "Scripts:iOS Affiliate Link.fakeworkflow"
19:        open wfPath
20:        delay 1
21:        run workflow with variables {appName:theName}
22:      end if
23:      
24:    end tell
25:  end if

The AppleScript is named “Apple Affiliate Link,” and the workflows are named “Mac Affiliate Link” and “iOS Affiliate Link.” I have them all saved in my ~/Library/Scripts folder so FastScripts knows where to find them. If you save them somewhere else, you’ll have to change Lines 11-12 and 17-18 to give your location.

I have the Mac App button as the default because I tend to link more often to Mac apps than iOS apps. If you’re the opposite, switch the positions of “iOS App” and “Mac App” in Line 3.

I’ve put the three components in a zip file that you can download. The two Fake workflows won’t work immediately because I’ve used a dummy URL in Step 1 of each. Change that to the URL you use when you use Apple’s Link Maker tool, save them with the names “Mac Affiliate Link” and “iOS Affiliate Link”, and you should be able to link with abandon.