Export Note Cards

Users asking other users for AppleScripts that work with Bookends.
Post Reply
kseggleton
Posts: 43
Joined: Fri Jan 01, 2016 6:47 am

Export Note Cards

Post by kseggleton »

Here is an Applescript that will export the note cards for a selected record in Bookends. It will format the note cards in Markdown as in the screenshot below and will open a dialogue box to name and save the notes to a destination:

Image

Code: Select all

-------------------------------------------------------------------------
-- SCRIPT FOR EXTRACTING MARKDOWN FORMATTED NOTES
-- by Kyle Eggleton 07/2016
-------------------------------------------------------------------------

tell application "Bookends"
	
	-- Get the selection
	
	try
		set theID to «event ToySRUID» "Selection"
		if theID is {} then error "Nothing selected in Bookends!"
	end try
	
	-- Set URL
	
	set refURL to "(bookends://sonnysoftware.com/" & theID & ")"
	
	-- Get the first Author
	
	set refAuthorList to («event ToySRFLD» theID given string:"authors")
	set origDelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {","}
	set refAuthors to text items of refAuthorList
	set AppleScript's text item delimiters to {"'"}
	set refAuthor to first item of refAuthors
	set AppleScript's text item delimiters to {""}
	
	-- Extract date
	
	set refDateList to («event ToySRFLD» theID given string:"thedate")
	set regexDef to "([12][0-9][0-9][0-9])"
	set refDate to (do shell script "if [[ \"" & refDateList & "\" =~ " & regexDef & " ]]
then echo \"${BASH_REMATCH[1]}\"
fi")
	
	-- Set citation
	
	set refCitation to refAuthor & ", " & refDate & ", #" & theID
	
	-- Get title 
	
	set refTitle to («event ToySRFLD» theID given string:"title")
	
	-- Extract Notes
	
	set refNote to («event ToySRFLD» theID given string:"notes")
	set AppleScript's text item delimiters to ASCII character 10
	set noteItems to text items of refNote
	set AppleScript's text item delimiters to return & return
	set refNote to noteItems as string
	set AppleScript's text item delimiters to "#"
	set noteItems to text items of refNote
	set AppleScript's text item delimiters to "####"
	set refNote to noteItems as string
	set AppleScript's text item delimiters to origDelim
	
	-- Markdownify notes
	
	set refMD to "# " & refCitation & return & return & "## " & refTitle & return & return & "[Bookends link]" & refURL & return & return & refNote
	
	-- Save notes as markdown file
	
	set refPath to POSIX path of (choose file name with prompt "Save As File" default name "Notes.md" default location path to desktop) as text
	if refPath does not end with ".md" then set refPath to refPath & ".md"
	do shell script "echo " & quoted form of refMD & "> '" & refPath & "'"
	
	
end tell
KirtWilson
Posts: 3
Joined: Thu Jul 21, 2016 5:08 pm

Re: Export Note Cards

Post by KirtWilson »

This is a terrific script. Thank you!

I would be interested in making a slight modification if you could put me on the right track. How might I alter the script so that refCitation appears (is appended) at the end of each individual note?

By altering the script so that each note begins with "## " rather than "#### " I was able to process the md file into OPML using multimarkdown bash command

Code: Select all

multimarkdown -t opml ./Notes.md > Bookends.opml
. When imported into DevonthinkPro, OmniOutliner or Scrivener it provides a true "notecard" result, because each note is separated from the others. If you could help me understand how to get the refCitation to append at the end of every note, perhaps after a return, I could shuffle the cards around but always track back to the source.

Well done on the script and thank you for any assistance you are able to provide.
kseggleton
Posts: 43
Joined: Fri Jan 01, 2016 6:47 am

Re: Export Note Cards

Post by kseggleton »

Hi @KirtWilson, below is an updated script that will place the Citation in the heading of each note and give you the option of exporting the note as either Markdown or OPML. It is a bit of a hack but seems to work. I found it difficult to get the citation at the end of each note so gave up and ended up putting it in the heading.

Code: Select all

-------------------------------------------------------------------------
-- SCRIPT FOR EXTRACTING MARKDOWN OR OPML FORMATTED NOTES
-- by Kyle Eggleton 07/2016
-------------------------------------------------------------------------

tell application "Bookends"
	
	-- Get the selection
	
	try
		set theID to «event ToySRUID» "Selection"
		if theID is {} then error "Nothing selected in Bookends!"
	end try
	
	-- Set URL
	
	set refURL to "(bookends://sonnysoftware.com/" & theID & ")"
	
	-- Get the first Author
	
	set refAuthorList to («event ToySRFLD» theID given string:"authors")
	set origDelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {","}
	set refAuthors to text items of refAuthorList
	set AppleScript's text item delimiters to {"'"}
	set refAuthor to first item of refAuthors
	set AppleScript's text item delimiters to {""}
	
	-- Extract date
	
	set refDateList to («event ToySRFLD» theID given string:"thedate")
	set regexDef to "([12][0-9][0-9][0-9])"
	set refDate to (do shell script "if [[ \"" & refDateList & "\" =~ " & regexDef & " ]]
then echo \"${BASH_REMATCH[1]}\"
fi")
	
	-- Set citation
	
	set refCitation to refAuthor & ", " & refDate & ", #" & theID
	set refCitationx to "- {" & refAuthor & ", " & refDate & ", qhashq" & theID & "}"
	
	-- Get title 
	
	set refTitle to («event ToySRFLD» theID given string:"title")
	
	-- Extract Notes
	
	set refNote to («event ToySRFLD» theID given string:"notes")
	set AppleScript's text item delimiters to ASCII character 10
	set noteItems to text items of refNote
	set itemsToDelete to {""}
	set cleanList to {}
	repeat with i from 1 to count of noteItems
		if (noteItems's item i) is not in itemsToDelete then set cleanList's end to the noteItems's item i
	end repeat
	set noteItems to text items of cleanList as string
	set AppleScript's text item delimiters to "#"
	set refNote to text items of noteItems
	set AppleScript's text item delimiters to "##"
	set noteItems to text items of refNote as string
	set refNoteCitation to do shell script "echo \"" & noteItems & "\" | sed -E 's/(##.*)/\\1 " & refCitationx & "/g'"
	set refNote to do shell script "echo \"" & refNoteCitation & "\" | sed -E 's/qhashq/#/g'"
	set AppleScript's text item delimiters to return
	set noteItems to text items of refNote
	set AppleScript's text item delimiters to return & return
	set refNote to text items of noteItems as string
	set AppleScript's text item delimiters to origDelim
	
end tell

choose from list {"Markdown", "OPML"} with title "File format" with prompt "Choose file format"

-- Markdownify notes

if result = {"Markdown"} then
	set refMD to "# " & refCitation & return & return & "**" & refTitle & "**" & return & return & "[Bookends link]" & refURL & return & return & refNote
	set refPath to POSIX path of (choose file name with prompt "Save As File" default name "Notes.md" default location path to desktop) as text
	if refPath does not end with ".md" then set refPath to refPath & ".md"
	do shell script "echo " & quoted form of refMD & "> '" & refPath & "'"
	
	--OPML notes
	
else if result = {"OPML"} then
	set refMD to "# " & refCitation & return & return & "**" & refTitle & "**" & return & return & "[Bookends link]" & refURL & return & return & refNote
	set refPath to POSIX path of (choose file name with prompt "Save As File (without extension)" default name "Notes" default location path to desktop) as text
	if refPath does not end with ".md" then set refPath to refPath & ".md"
	set AppleScript's text item delimiters to "."
	set thePath to text items of refPath
	set opmlPath to item 1 of thePath & ".opml"
	set AppleScript's text item delimiters to origDelim
	do shell script "echo " & quoted form of refMD & "> '" & refPath & "'"
	do shell script "/usr/local/bin/multimarkdown -t opml '" & refPath & "' > '" & opmlPath & "'"
	tell application "Finder"
		delete refPath as POSIX file
	end tell
	
end if
KirtWilson
Posts: 3
Joined: Thu Jul 21, 2016 5:08 pm

Re: Export Note Cards

Post by KirtWilson »

That is terrific!

Thank you very much.

Sincerely,

Kirt
fmeres
Posts: 12
Joined: Sun Apr 14, 2019 1:23 pm

Re: Export Note Cards

Post by fmeres »

I just found this script and I think something changed in Bookends' code and a part of the syntax isn't working. I'm getting the following error and was wondering how to fix it as I'm very interested in using the script:
error "Bookends got an error: sh: -c: line 0: syntax error in conditional expression
sh: -c: line 0: syntax error near `]]
then'
sh: -c: line 0: `if [[ \"1976\" =~ ([12][0-9][0-9][0-9]) ]]
then echo \"${BASH_REMATCH[1]}\"
fi'" number 2
kseggleton
Posts: 43
Joined: Fri Jan 01, 2016 6:47 am

Re: Export Note Cards

Post by kseggleton »

fmeres wrote: Fri Nov 22, 2019 10:27 am I just found this script and I think something changed in Bookends' code and a part of the syntax isn't working. I'm getting the following error and was wondering how to fix it as I'm very interested in using the script:
I modified the script sometime ago to directly export an annotation summary into DEVONthink 3. Here is a version that will create a markdown file without needing DEVONthink. There are some personalisations that you will have to make - these are commented in the script.

Code: Select all

-- Script to creat Annotations summary from Bookends reference
-- Kyle Eggleton November 2019

tell application "Bookends"
	tell front library window
		
		-- Get selected publication 
		set theRefs to selected publication items
		set theRefsNo to count of theRefs
		set theRef to first item of theRefs
		
		-- Error messages	
		if theRefsNo is greater than 1 then error "Select only one item"
		if theRefs is {} then error "Nothing selected in Bookends"
		
		
		-- Get properties of selected reference
		set theID to id of theRef
		set theCitation to user1 of theRef -- user1 is the Bookends field where BibTex citation is stored
		set theAbstract to abstract of theRef
		set theAuthors to author names of theRef
		set theAuthorDate to format theRef using "Author Date.fmt"
		set theTitle to title of theRef
		set theJournal to journal of theRef
		set theFormattedReference to format theRef using "APA 6th Edition Markdown.fmt" --change to reflect the formatted reference required
		set theNotes to the notes of theRef
		
		
		
	end tell
end tell

--Format the annotations summary to Markdown
set theNotes to replaceText("#", "####", theNotes)
set theNotes to replaceText("@", "Page ", theNotes)
set theReference to replaceText("
", "", theFormattedReference)

set theAnnotations to "# Annotations: " & theAuthorDate & "

**Title:** " & theTitle & "

**Authors:** " & theAuthors & "

**Citation:** [" & theCitation & "](bookends://sonnysoftware.com/" & theID & ") 

**Reference:** " & theFormattedReference & "

**Abstract:** " & theAbstract & "

## Annotations


" & theNotes


-- Save notes as markdown file

set refPath to POSIX path of (choose file name with prompt "Save As File" default name "Notes.md" default location path to desktop) as text
if refPath does not end with ".md" then set refPath to refPath & ".md"
do shell script "echo " & quoted form of theAnnotations & "> '" & refPath & "'"


--Replace text subroutine
on replaceText(searchString, replaceString, theText)
	set AppleScript's text item delimiters to searchString
	set theItems to every text item of theText
	set AppleScript's text item delimiters to the replaceString
	set theText to the theItems as string
	return theText
end replaceText
dr.cox
Posts: 7
Joined: Tue May 22, 2018 4:23 pm

Re: Export Note Cards

Post by dr.cox »

Hi guys,

the script works well as far as I can see. I am totally clueless when it comes to scripting. Is there any chance the exported highlights can be sorted by color-code / annotation type, i.e. if if I highlight in different colors / underline / cross out or the like that these are sorted accordingly in the exported markdown file?

cheers in advance for the help, stay safe,


J.
kseggleton
Posts: 43
Joined: Fri Jan 01, 2016 6:47 am

Re: Export Note Cards

Post by kseggleton »

dr.cox wrote: Thu Jan 14, 2021 6:26 am Is there any chance the exported highlights can be sorted by color-code / annotation type, i.e. if if I highlight in different colors / underline / cross out or the like that these are sorted accordingly in the exported markdown file?
No, the script can not be easily modified to do this
dr.cox
Posts: 7
Joined: Tue May 22, 2018 4:23 pm

Re: Export Note Cards

Post by dr.cox »

hi guys,

never got round to replying. topic still pertinent for me, unfortunately.

can the script be modified to at least include the annotation color in the header? if so, can someone maybe please help out with this as I have no coding skilss whatsoever.

cheers,

j.
vinschger
Posts: 57
Joined: Fri Nov 19, 2021 1:28 pm

Re: Export Note Cards

Post by vinschger »

would it be possible to modify the script in a way, that I can select multiple (!) references, and all annotations in these references are copied into notecards (and if the script runs a second time, only the new annotations are copied into notecards)? probably not so easy, I assume?!
kseggleton
Posts: 43
Joined: Fri Jan 01, 2016 6:47 am

Re: Export Note Cards

Post by kseggleton »

vinschger wrote: Sun Nov 28, 2021 4:06 pm would it be possible to modify the script in a way, that I can select multiple (!) references, and all annotations in these references are copied into notecards (and if the script runs a second time, only the new annotations are copied into notecards)? probably not so easy, I assume?!
No that wouldn't be possible
Post Reply