External linking for nvALT notes: Part 1

[Tweet]

I devised a couple of strategies for attaching my notes to other places on my system where url links were ugly or inconvenient. It turned out to be a system that’s pretty effective in a lot of applications; even ones that provide a linking system to begin with.

The basic idea is that you can use [[links]] like you would inside of nvALT, or WikiLinks (camelcased words with no spaces) anywhere you can put text, then you run a script or Service (I have both for you, in part 2) with that text selected and it will open the related note. If you put the action on a hotkey, you can just select the text and press your shortcut to jump straight to the note. That’s tomorrow, though.

This first one I’m sharing is specifically for Mindjet MindManager, though, and it uses real urls. This is where I started, before I got the idea to reference and create notes with wiki links. This script goes through all selected nodes in a MindManager mind map, taking their title text and creating a note named for it, then adding the link to that note as a hyperlink on the node. Now you can just click the little rocket next to the node it to open the note in nvALT1. Notes are tagged based on the map’s main topic title for fast searching for an entire project’s notes. This screenshot might help it make more sense.

Yes, MindManager has built-in support for notes, and on occasions when you’re sharing them with others it’s preferable to use the internal ones. When I’m brainstorming, though, having my notes available outside of a single application and universally accessible is more useful. Look at me feeling obligated to explain myself.

Run this script from anywhere, as long as MindManager is open and has at least one topic selected. There are a few settings you can adjust at the top of the script. I don’t know how many nerdy MindManager users there are for this, but if you have any questions, let me know. At the rate of interest this is likely to have, it’ll be easier to assist one on one than try to explain everything at this point.

Stay tuned for part 2, wherein I provide you with something more universally useful.

Mindjet2nvALT.applescriptraw
(* 
mindjet2nvalt

Goes through selected nodes in MindJet Manager and creates 
nvALT notes for them, adding a rocket icon to each node which 
will locate the note when clicked. If the node has a note 
attached, it is used as the content of the nvALT note.

Copyright 2012 Brett Terpstra
License is granted to use and modify for personal use
Do not distribute modified copies without permission
*)
----====User Config====----
-- by default, this property will avoid changing a 
-- link on a node in case the title changes but you 
-- want the link to remain connected
property overwriteLinks : true

-- centralTopicAsTags will convert the title of
-- map's main node into tags, allowing you to group 
-- them together with nvALT or Spotlight searches
property centralTopicAsTags : true

-- centralTopicAsNotePrefix will prefix "[Central Topic title]: "
-- to the title of the note, if that's how you prefer to search
property centralTopicAsNotePrefix : false
---===End User Config===---


property _ucChars_ : "AÄÁÀÂÃÅĂĄÆBCÇĆČDĎĐEÉÈÊËĚĘFGHIÍÌÎÏJKLĹĽŁMNÑŃŇ" & ¬
	"OÖÓÒÔÕŐØPQRŔŘSŞŠŚTŤŢUÜÚÙÛŮŰVWXYÝZŽŹŻÞ"
property _lcChars_ : "aäáàâãåăąæbcçćčdďđeéèêëěęfghiíìîïjklĺľłmnñńň" & ¬
	"oöóòôõőøpqrŕřsşšśtťţuüúùûůűvwxyýzžźżþ"

tell application "Mindjet MindManager"
	set theDocument to document 1
	set theSelection to selection of theDocument
	set _proj to central topic of theDocument
	if theSelection is not equal to {} then
		repeat with _t in theSelection
			if hyperlink URL of _t is missing value or overwriteLinks is true then
				set _map to POSIX path of (file of theDocument as alias)
				if centralTopicAsNotePrefix is true then
					set _name to (title of _proj) & ": " & title of _t
				else
					set _name to title of _t
				end if
				set _note to notes of _t as string
				if centralTopicAsTags is true then
					set _tags to "#" & my replace_string(" ", "-", my lowercase_string(title of _proj))
				else
					set _tags to ""
				end if
				set _link to my create_nv_note(_name, _note, _map, _tags)
				set hyperlink URL of _t to _link
			else if hyperlink URL of _t is not missing value then
				set _url to hyperlink URL of _t
				tell application "Finder" to open location _url
				return
			end if
		end repeat
		tell application "Mindjet MindManager" to activate
	else
		display dialog "You must have at least one node selected"
	end if
end tell

-- creates a new note and returns a url for it
on create_nv_note(_title, _note, _map, _tags)
	set newlink to ""
	if _note is missing value or _note is equal to "" then
		set newlink to "nvalt://make/?title=" & _title & "&tags=" & _tags & "&txt=" & "New note from Mind Map <file://" & my replace_string(" ", "%20", _map) & ">"
	else
		set newlink to "nvalt://make/?title=" & _title & "&tags=" & _tags & "&txt=" & _note
	end if
	tell application "Finder" to open location newlink
	set _link to "nvalt://find/" & _title
	return _link
end create_nv_note


on replace_string(thisStr, thatStr, origString) -- credit: Jon Pugh <http://www.seanet.com/~jonpugh/>
	set {oldDelim, AppleScript's text item delimiters} to {AppleScript's text item delimiters, thisStr}
	set theList to text items of origString
	set AppleScript's text item delimiters to thatStr
	set theString to theList as string
	set AppleScript's text item delimiters to oldDelim
	return theString
end replace_string


on lowercase_string(theText) -- credit: <http://applescript.bratis-lover.net/library/string/>
	local upper, lower, theText
	try
		return my translateChars(theText, my _ucChars_, my _lcChars_)
	on error eMsg number eNum
		error "Can't lowerString: " & eMsg number eNum
	end try
end lowercase_string

on translateChars(theText, fromChars, toChars)
	local Newtext, fromChars, toChars, char, newChar, theText
	try
		set Newtext to ""
		if (count fromChars)  (count toChars) then
			error "translateChars: From/To strings have different lenght"
		end if
		repeat with char in theText
			set newChar to char
			set x to offset of char in fromChars
			if x is not 0 then set newChar to character x of toChars
			set Newtext to Newtext & newChar
		end repeat
		return Newtext
	on error eMsg number eNum
		error "Can't translateChars: " & eMsg number eNum
	end try
end translateChars
  1. I went down a deep, deep rabbit hole trying to make it possible for the notes to link back to the topic on the map. MindManager can do this internally, but exposes no url scheme for it. I built a background applet with a ‘mmgo:’ url scheme, even got it working, but there are too many problems with it to share. Some insanity is best kept to one’s self.