Bookends to Obsidian with front matter filled

Hi all,

I am trying to set up a workflow with Bookends and Obsidian and I am looking for a script that takes the selected reference in Bookends and creates a new note in Obsidian with “Author Year Title” as name, and with “citation ID” (without the # first) as an alias in the front matter. Would any of your scripts, @ryanjamurphy, be possible to adjust to create this?

I tried combining Devonthink and Bookends last year (indexed the pdf folder), with the result that Bookend lost the link to half of the pdf’s in my library, so I am looking for an adapted version of the existing DT-BE-Obsidian script. I am not interested in having the notes exported as notes in Bookends and Obsidian serve different purposes.

Adding keywords (from BE) as tags in the front matter, and adding the link back to the BE reference would be nice, but not essential for me.

The citation ID in the alias is essential because it allows tracking all citations of the reference in Obsidian.

Thanks in advance for any help!

Ken

1 Like

Off the topic here but I also had DT index my BE attachment folder on iCloud and that happened to me before. I was able to fix that by disabling the iCloud optimize storage setting.

1 Like

It should be doable. I don’t have the bandwidth to script it for you, but you’d be looking for something like the following (which isn’t strictly AppleScript, you’ll have to check with the appropriate AppleScript dictionaries on exact terms):

tell application "Bookends"
set selectedReferences to the selected Bookends publications
set theReference to the first item in selectedReferences -- assumes you've only selected one reference
set theAuthor to author of theReference
set theYear to year of theReference
set theTitle to title of theReference
set theID to bookends ID of theReference
set bookendsLink to "bookends://sonnysoftware.com/" & theID
set noteFilename to theAuthor & " " & theYear & " " & theTitle
set noteText to "---" & return &  "citation ID: " & theID & return & "bookendsLink: " & bookendsLink & return & "---"

tell application (some application that can create files—Finder, DEVONthink, etc.,) 
create new file with filename noteFilename and text noteText
end tell

Handling the keywords would involve getting the Bookends keywords, parsing them to make them into a comma-separated list, then using the same techniques above to put 'em into note YAML.

You might want to check in at Sonny Software’s forum for more detailed support. Technically this isn’t an Obsidian issue as the note you’re creating is just a markdown file in a folder somewhere—just one that Obsidian happens to pick up.

1 Like

Thanks @ryanjamurphy ! I am not skilled at coding but I’ll give it a go :+1:

1 Like

I ended up writing this script with @ryanjamurphy’s comment as a sketch. It’s not exactly what you want since it doesn’t even use the front matter but I thought I’d post it here nonetheless since it was inspired by this thread. I also wrote it as an Alfred workflow instead of a plain AppleScript.

You can easily take the AppleScript from the workflow and modify it to your needs.

3 Likes

Amazing, thanks @oash I tried to set it up now and get this:
Screenshot 2021-06-23 at 13.11.01
Can you help me solve that?
Cheers,
Ken

In my limited experience, this usually happens when the vault path is set incorrectly, or the “Papers” folder (or whatever you named it) does not exist. Can you double check those two things?

Using the debugging mode in Alfred I discovered that it concerned the path to the ‘papers’ folder. It needs to be in the first level of the vault apparently. Moving it there it works like a charm now. Thank you so much @oash !!

Hmm I hadn’t thought of that limitation. I’ll add that to the ReadMe until I fix it, thank you for being an alpha tester :smiley:

Please let me know if you have anymore issues or feature requests!

Bookend and Obsidian Users,

I use Bookends, Obsidian, and Devonthink as the core of my research and annotation process. I have a script in the Bookends folder that allows me to export all of my Bookends notes/annotations to a single Markdown file that is imported to Obsidian. I have a second script that exports the same notes to Devonthink as an opml file that separates each note into a different text file that I then convert, in Devonthink, to Markdown format. Both scripts are adaptations of scripts that other, better programmers, built to work with Bookends. Below I’ll share the script that exports a single Markdown file for import to Obsidian. Before I do, however, I need to explain how I format my notes in Bookends, because the formatting matters a lot for the script

Each Bookends note or annotation is formated as follows:

Line 1: #@<pg> <ID> <Note Title>
Line 2: <Note Content>

or for notes that include a quotation:

Line 1: #@<pg> <ID> <Note Title>
Line 2: > <quotation>
Line 3: <comments on quotation>

So, for example, here are two notes from Susan Wells’ book, Sweet Reason

#@3 210614-1104 Modernity as a system of texts - quotation
> Modernity is a system of texts that we are only now learning to read.
Susan Wells argues here that modernity is more than just a period of time. It is a mode of writing, a system that articulates certain relationships and those relationships are, for the most part, distinctions that separate reason from desire or academic writing from governmental writing. Modernity as writing is all about seemingly “natural” classifications that are designed according to the character of what is being written or the context in which it takes place.

#@4_6 210614-1108 Modern Rhetoric as a set of compositional themes
Wells seems to indicate on pages 4 through 6 that the text of modernity is a rhetoric that attends to the materiality of writing, to its entrainment in relations of desire and displacement, as it will attend to various narratives of power, knowledge, disclosure, and conversion.

The script I’m about to paste below presumes that each Bookends note is structured fairly closely to what you see above. OK. here is the Bookends Script that I run to produce a single Markdown Annotation File of a Bookend’s source.

-- Script to create Annotations summary from Bookends reference
-- Original Script by Kyle Eggleton, November 2019
-- Adaptations to Script by Kirt Wilson, February 2021

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 citekey 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 theShortTitle to short title of theRef
		set theJournal to journal of theRef
		set thePubdate to publication date string of theRef
		set theDOI to doi of theRef
		set theKeywords to keyword names of theRef
		set theProjects to user4 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 "---
aliases: [" & theShortTitle & "]
ID: <% tp.file.creation_date(\"YYMMDD-HHmm\") %>
type: annotation
author: [\"" & theAuthors & "\"]
pubdate: " & thePubdate & "
doi: " & theDOI & "
bkurl: bookends://sonnysoftware.com/" & theID & "
created: <% tp.file.creation_date(\"MMMM DD, YYYY\") %>
tags:
---
# Annotations: " & theAuthorDate & "

**Title**:: " & theTitle & "

**Authors**:: " & theAuthors & "

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

**Reference**:: " & theFormattedReference & "

**BookendURL**:: bookends://sonnysoftware.com/" & theID & ") 

**Project**:: [[" & theProjects & "]]

**Abstract**:: " & theAbstract & "

---  
## Annotations

" & theNotes & "

ID: <% tp.file.creation_date(\"YYMMDD-HHmm\") %>"


-- Save notes as a 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

So, if the script runs successfully, you end up with a single Markdown file that has a YAML header, a Title, a section that provides citation information, and a series of notes all separated by ‘###’.

In my own workflow, I go one step further by running a regexp search/replace on the file using BBEdit. This search/replace pattern places the page number and ID at the end of each note rather than allowing it to appear in each note’s title. I like this formating better. That regular expresses is as follows:

Find: ^(### )(Page\s([0-9,_]{1,7}))\s([0-9](.\d\d\d\d-\d\d\d\d))(.*)\n(.*)

Replace: \1\6\n\7 (\2) ^\4

That’s it. I hope that this is useful to someone else who is using Bookends and needs to generate a Markdown file of their Bookend Annotation Notes. Obviously, this file is useful for Obsidian, but it could work in any note app that accepts Markdown formatting.

Best,

Kirt Wilson

2 Likes