Keyboard Maestro Macro for Wikilinks on Evernote (kinda)

Following suit on my AppleScript learning journey, I’ve (kinda) successfully automated one of my desired Evernote features.

The keyboard macro

This macro is activated by selecting a word on Evernote and then pressing Cmd + [

It performs a search in a given notebook (one named Zettelkasten, for instance) for notes titled the same as the selected text.

Should there be one, it will fetch Evernote’s internal link and link it to the original.

Should there be none, it will create a new note and link it to the original.

In both instances, the new/existing note will have a link to the original appended. Same thing will happen to the other note, so there will be bidirectional links on both notes.

As a bonus, tags will be created and assigned both ways.

The heart of the matter: Applescript!

Although this is a Keyboard Maestro Macro, its heart is this AppleScript:

tell application "Keyboard Maestro Engine"
	set selectedText to getvariable "selectedText"
	set zettelkastenNotebook to getvariable "zettelkastenNotebook"
end tell

set newLink to ""

tell application "Evernote"
	-- Gets the current note title and internal link in order to append its link and title to the linked note
	set selectionList to selection
	set originalNote to first item of selectionList
	set originalTitle to title of originalNote
	set originalLink to note link of originalNote
	
	-- Creates tags for the titles of both the New and Original notes if there aren't. Those tags will be nested into a tag with the same name of the selected notebook
	
	-- Original Tag
	if (not (tag named originalTitle exists)) then
		set originalTag to make tag with properties {name:originalTitle, parent:tag named zettelkastenNotebook}
	else
		set originalTag to tag originalTitle
	end if
	
	-- Destination Note Tag
	if (not (tag named selectedText exists)) then
		set newTag to make tag with properties {name:selectedText, parent:tag named zettelkastenNotebook}
	else
		set newTag to tag selectedText
	end if
	
	--Lookup for existing notes and note creation if needed
	
	set searchList to every note of notebook zettelkastenNotebook whose title is selectedText
	if (count of searchList) is greater than 0 then
		-- Get's Evernote internal link of the first note should there be at least one note titled the same of the selected text
		set existingNote to the first item of searchList
		assign newTag to existingNote
		assign originalTag to existingNote
		tell existingNote to append html "<br><br>" & "Ver " & "<a href=" & originalLink & ">" & originalTitle & "</a>"
		set newLink to note link of existingNote
	else
		-- Creates a newNote if there's none
		set newNote to create note title selectedText with text " " notebook "Zettelkasten"
		assign originalTag to newNote
		assign newTag to newNote
		tell newNote to append html "<br><br>" & "See also " & "<a href=" & originalLink & ">" & originalTitle & "</a>"
		set newLink to note link of newNote
	end if
	return newLink
end tell

Known limitations:

  • Only works within the same notebook as I could not figure out how to look for the first note in a list with every notebook parameter on line 33

Download

Link to download the macro