Automation Documentation: Hooking Alfred Workflows

An update was made by the Alfred developers shortly after I posted this. While the original URL structure appears to still work at the time of writing this note, a small but official change has been made to the URL scheme (the addition of navigateto/. I have updated the script in this post to reflect this.

Last year I wrote several posts about using Hookmark (neé Hook) to link to automation documentation. At the time I was a little frustrated in that I had several complex Alfred workflows that I was unable to use Hookmark for to link back and forth between the workflow and the associated notes. However, Vitor of Running with Crayons replied recently to my feature request on the Alfred forums, and with the latest Alfred beta (v5.1 build 2130), I now have a working solution.

Adding a Script for Alfred Workflows to Hookmark

Adding scripts to Hookmark requires a pro subscription - specifying your own custom scripts being part of the automation feature set that is available to such subscribers. Moreover, adding an integration script for Alfred workflows ia a little bit trickier than usual as Alfred isn’t actually the app we want to add our script to. We actually want to add it to the Alfred Preferences app which is embedded inside the Alfred app.

  1. Open Hookmark.
  2. Press ⌘, to open Hookmark’s preferences window.
  3. Select the Scripts section.
  4. Select the + in the bottom left to add a new entry to the list of apps.
  5. Press ⇧⌘G to open the standard Finder path entry window.
  6. Enter in the following path: /Applications/Alfred 5.app/Contents/Preferences/Alfred Preferences.app

This should give you an empty set of script options for Alfred Preferences (com.runningwithcrayons.Alfred-Preferences).

Add the Get Address Script

The script to add is for the Get Address. With this we’ll build a full Markdown link for accessing the workflow.

The script utilises three scripting languages. AppleScript is the language used by Hookmark, so the main ‘wrapper’ script is written in AppleScript and effectively just runs the two other scripts and outputs the final result.

The first embedded script is a JavaScript for Automation (JXA) script, which reads the latest workflow ID from a JSON array in a new history listing file maintained by the Alfred app. JavaScript is a lot easier to work with JSON content than native AppleScript, so farming out to this other language made things much easier.

The second script is a shell script, and this time it is reading information from a PLIST (property list) file. It uses the workflow ID to identify the correct PLIST file and then gets the name of the workflow from it. Furnished with both the name and the workflow ID, the script then carries out a simple string concatenation to generate the desired Markdown link; which is then output by the main AppleScript script.

Copy and paste the script below into your Get Address section, click on Save, and Hookmark will be ready to link up your Alfred workflows.

set strWFID to do shell script "osascript -l \"JavaScript\" -e \"
	// Initialise
	let objApp = Application.currentApplication();
	objApp.includeStandardAdditions = true;
	ObjC.import('stdlib');

	// Return the latest workflow ID from Alfred's history file
	JSON.parse(objApp.read(Path($.getenv('HOME') + '/Library/Application Support/Alfred/history.json'))).preferences.workflows[0];\"
"

set strWFLink to do shell script "
	# Get Name
	WFNAME=$(defaults read \"$HOME/Library/Application Support/Alfred/Alfred.alfredpreferences/workflows/" & strWFID & "/info.plist\" \"name\")

	# Build URL to Open
	WFLINK=\"alfredpreferences://navigateto/workflows>workflow>" & strWFID & "\"

	# Output Markdown Link
	echo \"[$WFNAME]($WFLINK)\"
"

strWFLink

Conclusion

Once again, my main aim was to be able to bidirectionally link to my relevant automation documentation within one of my Obsidian vaults. But where I utilise a web API, I can now link to API documentation, and for my workflows that are associated with a particular project (e.g. configuration of a local web site/server), I can link to relevant files, folders and site pages. Being able to link up my automations and documentation like this is a big time saver for me by helping to ensure everything is accessible. The less accessible my documentation is, the less likely I am to maintain it effectively, so this just smooths away that friction.

Hopefully, you can see a similar use for your own Alfred workflows with Hookmark.

Author: Stephen Millard
Tags: | hook | hookmark | alfred |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read