Automation Documentation: Hooking SnippetsLab

My efforts to link together my documentation and automation took a little jump forward recently with a new beta of the code library utility called SnippetsLab. A small update to the user interface has enabled me to construct a basic integration with Hookmark (the app formerly known as Hook). In this post I am going to explain a bit more about this.

What is SnippetsLab?

SnippetsLab is a Mac code management utility that allows you to keep common snippets of code easily accessible. Sometimes I write (or find) code that I may want to use in several standalone places. Code that isn’t practical to include in a single library. In these cases, SnippetsLab is my go to utility.

The Library Advantage

As far as possible, I like to use a cenralised library approach for my code, but as noted above, not all code snippets belong in a library. The challenge then you end up with multiple locations where the code is used, and multiple places to maintain the code if there are any changes.

I wanted to find a way that enabled me to easily generate a way to track the relationship between a snippet and where it was used. More so, what I really wanted was to “hook” this into my existing automation documentation workflow utilising Hookmark and Obsidian.

Hookmark Compatibility

I looked at a few options with the SnippetsLab application, and I could get pretty far in getting it to work with Hookmark. Unfortunately, I couldn’t quite get one easy access to one thing. I decided to reach out to SnippetLab’s developer, Renfei Song, and we got into a discussion about my desire to use SnippetsLab with Hookmark.

After a bit of back and forth and me explaining the principles in a little detail, a new beta release of the app was forthcoming with a new menu item that I could then use to get the specific deep link I was looking for.

I do so appreciate developers who are willing to make these sorts of app tweaks to help their users.

Hookmark Script

The beta version the key menu item was added to is 3.3 build 3127, so the script below should work with that and any subsequent version of SnippetsLab. If you want to help with the beta testing of the app, Renefei has a page all about it.

The integration script is for the Get Address integration, and you can find out all about adding and using integration scripts on the Hookmark creating integration scripts page.

-- `menu_click`, by Jacob Rus, September 2006
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item.  In this case, assuming the Finder 
-- is the active application, arranging the frontmost folder by date.

on menu_click(mList)
	local appName, topMenu, r
	
	-- Validate our input
	if mList's length < 3 then error "Menu list is not long enough"
	
	-- Set these variables for clarity and brevity later on
	set {appName, topMenu} to (items 1 through 2 of mList)
	set r to (items 3 through (mList's length) of mList)
	
	-- This overly-long line calls the menu_recurse function with
	-- two arguments: r, and a reference to the top-level menu
	tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
		(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click

on menu_click_recurse(mList, parentObject)
	local f, r
	
	-- `f` = first item, `r` = rest of items
	set f to item 1 of mList
	if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
	
	-- either actually click the menu item, or recurse again
	tell application "System Events"
		if mList's length is 1 then
			click parentObject's menu item f
		else
			my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
		end if
	end tell
end menu_click_recurse

-- Now the scripting that uses those functions to return a Markdown link to Hook
tell application "SnippetsLab" to activate
-- Jump to a point we can navigate to the snippet name from
menu_click({"SnippetsLab", "Editor", "Edit Tags"})
delay 0.2
-- Copy the name of the snippet
tell application "System Events"
	keystroke (ASCII character 9) using shift down
	delay 0.2
	keystroke "c" using command down
	delay 0.2
end tell
-- Store the name of the snippet that is currently on the clipboard
set strSnippetName to get the clipboard
-- Copy the URL to the clipboard
tell application "SnippetsLab" to activate
menu_click({"SnippetsLab", "Edit", "Copy", "Copy Snippet Link"})
delay 0.2
-- Build and return the Markdown link
set strMarkdownLink to "[" & strSnippetName & "](" & (get the clipboard) & ")"
return strMarkdownLink

Once in place, this will allow you to quickly grab a deep link for any SnippetsLab snippet via Hookmark.

Summary

Being able to bidirectionally link to a note in Obsidian to create documentation for my code snippets, and to other automations like Keyboard Maestro macros gives me a greater control over my documentation, and to an extent a sense of peace that things are neatly connected and as I progress, I won’t have to rely on in-file scans to determine if some code is used or not - something that can be particularly painful where the code snippet may have been indented.

Hopefully, you’ll find this useful for your own workflows, and once again, my sincerest appreciation for Renfei for making this change to the app and further enabling it’s functionality and linkability.

Author: Stephen Millard
Tags: | hook | applescript |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read