Bi-directional links for a markdown Zettelkasten

Having taken first humble steps into creating my own zettelkasten (and being a strong proponent of only using one app for my data management, i.e. Devonthink) I quickly stumbled across @benoit.pointet absolutely fabulous script to create a graph view.

obviously the key to a zettelkasten is connecting the atomic notes and at least in my case most of the time these should be bi-directional links, which is a little cumbersome to maintain by hand. and while I am sure there are better ways of doing this and definitely more elegant ways to code it, I have written my very 1st applescript to append the names and links of selected markdown files to each other. in other words each of the selected files will link to each other file after the script has run:

-- script to add file names and x-devonthink links of each selected markdown file
-- to each of the other selected markdown files (bi-directional links).

	tell application id "DNtp"
		-- get the selection
		set thisSelection to the selection
		-- error handling: has to be two or more files
		if (length of thisSelection) < 2 then error localized string "Please select two or more (markdown) documents, then try again."
		-- loop over the selected files
		repeat with theItem in thisSelection
			set theURL to (reference URL of theItem)
			set theName to name of theItem
			-- inner loop to match all files
			repeat with theInnerItem in thisSelection
				if theInnerItem is not theItem then
					-- append name and x-devonthink-link of thisItem to theInnerItem
					set oldText to plain text of theInnerItem as string
					set newText to oldText & return & "- [" & theName & "](" & theURL & ")"
					set plain text of theInnerItem to newText
				end if
			end repeat
		end repeat
	end tell
on error errMsg
	display alert (localized string "error when adding names & links to items") message errMsg
end try

thanks in advance for any pointers to improve this or simpler ways of achieving the same goal. and happy if this is useful for somebody besides myself…:slight_smile:

1 Like

Thank you for posting this. Also a Zettler who uses DT.

Bidirectional links seems to be one of those topics that evokes strong opinions for and against the idea. Personally I don’t use bidirectional links; but that’s just an arbitrary preference.

In terms of the script, I suppose that you could conceptualize it differently. For example, you could set up a script to run as part of a smart rule, such that whenever your Zettel is saved, a script scans for links to other Zettels and makes the back link for you. This would eliminate the need to explicitly forge the link by selecting two notes and running the script manually.

1 Like

excellent point, thank you. I am still in early stages of my zettelkasten with quite a few thoughts and observations still to be moved in so the zettel I introduce typically come in batches that are interrelated hence the approach. but as my/a zettelkasten matures your suggested approach makes a lot of sense. might take a while for my basic applescript skills to get me there, but it’s a worthwhile goal…:wink:

Have you looked into the Tools > Inspectors > Document > Links inspector’s Links and Incoming Links ?

yes, I am aware of these. am probably missing your point as for the links to show up in the inspector I 1st have to set them, right? and the point of the script is to set them bi-directionally among two or more zettel once I bring them into the zettelkasten. is there an easier way to do this via the inspector?

No, you can’t create links in the Links inspector. It merely lists any outgoing or incoming item links or WikiLinks in the selected document.