Import Papers notes into Evernote (Mac)

Papers is a great application for reading, organizing, and citing academic papers. The iOS version syncs with the desktop so I can read papers on my iPad. But Papers is not where I keep my notes; that’s Evernote’s job! So I’ve written a script to export my Papers notes into Evernote.

It takes a paper like this, with highlights, comments, and tags:

Papers 3.4.6ScreenSnapz006

And creates an Evernote note like this:


The paper’s general comment is at the beginning of the note in italics, followed by a link back to the paper in Papers and the abstract. The notes and comments section has highlighted text in normal font and in-text comments bolded. Page numbers are according to the “pages” field in the paper’s metadata.

Many papers, such as those from JStor, have a title page before the paper begins. To get correct page numbering the script needs to adjust the page numbers exported by Papers. When you run the script it asks for a “page increment.” For most papers this should be 0: no adjustment needed. For papers with a title page, it should be -1.

To install the script you can download it here: Papers notes to Evernote (and unzip), or cut-and-paste the source below into Apple’s Script Editor. Either way, place the script in an appropriate folder (I use /Users/michaelthicke/Library/Scripts/Applications/Papers). Then you can run it from the script menu with Papers open and a paper selected. It will import the selected paper into Evernote.

Papers 3.4.6ScreenSnapz005


--Script to export notes of selected Papers paper to Evernote with nice formatting.
--Exports General Note, Papers link, highlights, and notes.
--Mike Thicke, Aug 2016
use framework "Foundation"
use scripting additions
--export the selected file to a temporary file
tell application id "com.mekentosj.papers3"
	set pubList to (selected publications of front library window) as list
	log pubList
	set thePublication to first item of pubList
	set itemLink to ("<p><a href=\"" & (item url of thePublication) as text) & "\">Papers Link</a></p>"
	set itemAbstract to abstract of thePublication
	--deleted at end of script
	set outFile to ((path to desktop from user domain) as string) & "papersexporttemp001.txt"
	export {thePublication} as Notes to outFile
	set keywordsString to keyword names of thePublication
	if not keywordsString = "" then
		set keywordList to my theSplit(keywordsString, ",")
		set keywordList to {}
	end if
end tell
--add a page increment to adjust for papers with title pages (eg. from JStor)
display dialog "Number to offset each page number by (can be positive or negative)" default answer "0"
set textIncrement to text returned of result
set numberIncrement to textIncrement as integer
--open notes file
tell application "System Events"
	set notesFile to (outFile as text)
	set notesText to read file (notesFile) as «class utf8»
end tell
--title of Evernote note is citation of paper
set noteTitle to paragraph 1 of notesText
set noteTitle to my replacePattern("< .*>", noteTitle, "")
--capture General Note section
set generalNoteText to "<em>"
set pNum to 2
	set noteCheckText to my replacePattern("^p.[0-9]*: .*", (paragraph pNum of notesText), "found")
	if noteCheckText = "found" then
		set generalNoteText to generalNoteText & "</em>"
		exit repeat
		set generalNoteText to generalNoteText & "<p>" & (paragraph pNum of notesText) & "</p>"
		set pNum to pNum + 1
	end if
end repeat
--remove paragraphs of noteText containing citation and General Note
set newText to ""
repeat with i from pNum to (count paragraphs of notesText)
	set newText to newText & paragraph i of notesText & " "
end repeat
set notesText to newText
--remove excess spaces
set notesText to my replacePattern("( )+", notesText, " ")
--break notes and comments into chunks
--uses ❄︎ as delimiter because comments can be multiple paragraphs and so can't just split on paragraph breaks
set notesText to my replacePattern("(?ms)(p.[0-9].+?)($?)(.*?(Highlighted|Written).*?)[0-9]{4}", notesText, "$1$3❄︎")
set notesArray to theSplit(notesText, "❄︎")
set abstractHTML to "<h3>Abstract</h3><p>" & itemAbstract & "</p>"
--will be used to create new note
set final_html to generalNoteText & itemLink & abstractHTML & "<h3>Notes and Comments</h3>"
--construct html list from notesArray
repeat with noteItem in notesArray
	set pageNumber to my replacePattern("(?ms)^\\s*p.([0-9]+).*", noteItem, "$1")
	if (not pageNumber = "") and (not pageNumber = noteItem) then
			set pageNumber to (pageNumber as integer) + textIncrement
			--highlights are normal list items
			set noteItem to my replacePattern("(?ms)^(\\s*p.[0-9]*:) (.*).--.Highlighted.*?$", noteItem, "<li>$2 (" & pageNumber & ")</li>")
			--comments in bold
			set noteItem to my replacePattern("(?ms)^(\\s*p.[0-9]*:) (.*).--.Written.*?$", noteItem, "<li><strong>$2 (" & pageNumber & ")</strong></li>")
		on error errormessage
			set pageNumber to 0
		end try
		set final_html to final_html & noteItem
	end if
end repeat
--create a new note with noteTitle as title and final_html as content
tell application "Evernote"
	if not final_html = "" then
		--new notes put in General Research folder
		if (not (notebook named "General Research" exists)) then
			create notebook "General Research"
		end if
		set my_notebook to notebook named "General Research"
		set new_note to create note title noteTitle with html final_html
		set end of keywordList to "papers notes"
		repeat with theKeyword in keywordList
			set theKeyword to my replacePattern("^\\s*(.+?)\\s*$", theKeyword, "$1")
				set my_tag to make tag with properties {name:theKeyword}
			on error errormessage
				set my_tag to tag named theKeyword
			end try
			assign my_tag to new_note
		end repeat
		move new_note to my_notebook
		open note window with new_note
		tell application "System Events" to set frontmost of process "Evernote" to true
	end if
end tell
tell application "System Events"
	delete alias outFile
end tell
on replacePattern(thePattern, inString, usingThis)
	set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:thePattern options:0 |error|:(missing value)
	set theResult to theRegEx's stringByReplacingMatchesInString:inString options:0 range:{location:0, |length|:length of inString} withTemplate:usingThis
	return theResult as text
end replacePattern
on theSplit(theString, theDelimiter)
	log theString
	-- save delimiters to restore old settings
	set oldDelimiters to AppleScript's text item delimiters
	-- set delimiters to delimiter to be used
	set AppleScript's text item delimiters to theDelimiter
	-- create the array
	set theArray to every text item of theString
	-- restore the old setting
	set AppleScript's text item delimiters to oldDelimiters
	-- return the result
	return theArray
end theSplit

Comment ( 1 )

  1. ReplyKevkev
    Hi Mike I really like your Script, it works perfectly fine. But I'm wondering if it was possible to export the note and marks separatly no evernote? Do you have any tip therefore? best wishes, Kevkev

Leave a reply

Your email address will not be published.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>