MarginNote adds AppleScript support

The latest version (3.7.8) of MarginNote now supports AppleScript. I haven’t done anything serious with this yet, but it appears from the dictionary that access to all notebooks and their notes is exposed. Could be some interesting possible integrations.


Not a user of MarginNote, but it makes me smile when developers of applications add support for AppleScript.


Interesting! Thanks for the tip. Looks pretty robust, too:


I do not like MarginNote’s export features. Weird looking RTF. No markdown export. For now, I add markdown to my notes in MarginNote then copy the notes and paste them elsewhere.

When I get a chance to experiment with the new features, rolling my own export will be the first project.

Here’s a first pass at an export to markdown. It assumes one document (e.g., one PDF) per notebook.


on clipNotes()
	tell application "MarginNote"
		set theNotebook to the first notebook
		set theNotes to note ids of theNotebook
		set theDictionaries to fetch dictionaries theNotes
		set clippedNotes to ""
		set theDocument to the document of (fetch note (the first item in theNotes))
		set fullText to "# " & title of theDocument & return & return
		repeat with eachNote in theDictionaries
			set clippedNote to ""
			set theNote to fetch note eachNote's id
			set theExcerpt to excerpt text of eachNote as string
			if theExcerpt is not "" then
				set clippedNote to clippedNote & "> " & theExcerpt
			end if
			set theComments to eachNote's comment dictionaries
			if theComments is not missing value then
				if (count of theComments) is not 0 then
					set commentText to ""
					repeat with eachComment in theComments
						set commentText to commentText & text of eachComment
					end repeat
					set clippedNote to clippedNote & return & commentText
				end if
			end if
			set noteLinkText to (tab & "- [" & title of theDocument) & ", page " & start page of eachNote & "](marginnote3app://note/" & id of eachNote & ")"
			set clippedNote to clippedNote & return & noteLinkText
			set clippedNotes to clippedNotes & clippedNote & return & return
		end repeat
		set fullText to fullText & clippedNotes
	end tell
end clipNotes

Sample output looks like this:

# Research Data Reusability- Conceptual Foundations, Barriers and Enabling Technologies - Thanos - 2018

> Research data reuse is the quintessence of the open science and open data principles on which modern science is based
Not only reuse, but all use, since serendipity plays such a strong role in the discovery process.
	- [Research Data Reusability- Conceptual Foundations, Barriers and Enabling Technologies - Thanos - 2018, page 16](marginnote3app://note/915A2ADB-C83F-4A5D-875C-67F7644EC1E1)

> It has four main dimensions: policy, legal, economic and technological
What about social? I wonder how these dimensions were defined and defended...
	- [Research Data Reusability- Conceptual Foundations, Barriers and Enabling Technologies - Thanos - 2018, page 1](marginnote3app://note/986C7A26-87C0-4F38-A1DA-29D85848AE0F)

- How does the author define and model reusability?
- How does this relate to data itself? 
- How does it relate to the conceptual model of the data?
- How does it relate to serendipity?
	- [Research Data Reusability- Conceptual Foundations, Barriers and Enabling Technologies - Thanos - 2018, page 1](marginnote3app://note/5C3E246B-B0A5-43A7-98C4-B44ED31EC662)

A neat thing is that you can use the colours of MarginNote notes to do different things with the exported notes. E.g., put all red notes next to each other under a “Critiques” heading, or always append a #idea to green notes.


I don’t actually currently use MarginNote, but this might have tipped me towards adopting it!

edit: damnit. I always forget the lock-in. MarginNote has its own wrapper around PDFs (e.g., annotations can only be viewed in MarginNote). This new AppleScript support helps resolve that issue for me. But MN doesn’t provide a file-based structure for its format—instead, everything is imported into the MN app database. So there’s no way to e.g., store a bunch of MN3 “files” in DEVONthink.

Alas. I need to tattoo this somewhere so I don’t forget it the next time MN comes out with something exciting…


There’s a failure in the middle

(if (count of theComments...
missing value

I’ll have to play around with this when I have time.

Ah, whoops. I guess if (count of theComments is not 0) is not as robust as if theComments is not missing value.

Edited the original to deal with both cases!

It’s actually not that.

I have several dozen notebooks in MN3 and both ScriptDebugger and Script Editor are getting a lot of “missing values” for properties that in reality have values.

The database is not corrupt, but for some reason the scripting interface is not scanning it correctly. I’ll take this offline and see if Sun Min can help diagnose the issue.

@quorm Ah, interesting. FWIW, the first approach I used for getting commented text…

set theNotes to the first notebook's notes
repeat with eachNote in theNotes
    set theComments to eachNote's comments
    repeat with eachComment
        set theComment to eachComment's text
    end repeat
end repeat

… just would not work. ScriptDebugger showed that theComment wasn’t properly returning a dictionary of values. So, I think there’s a few bugs in the AppleScript implementation still. I had to switch to explicitly grabbing the dictionaries of notes with MN3’s fetch dictionaries {list of note ids} command.

Sadly it doesn’t look like the AppleScript addition adds any new ways of exporting an annotated PDF. I’d love to back up the PDFs in “proper” PDF format, just for posterity.

The notes do have start and end positions, so it may be possible to use another scripting-friendly PDF app to take those values and regenerate annotations…