Linking Bookends reference to DEVONthink

There are a number of scripts in the forums on linking or importing a Bookends reference into DEVONthink. For my purposes I use a script that links a Bookends reference to a DEVONthink record. I use a folder in the Finder that I index with DEVONthink and is the same folder where my Bookends references are stored. The script uses an external dependency - exiftool, that will need to be installed separately. Exiftool changes the title and author fields of a PDF. For anyone else that might the script useful - here is mine (although I have realised after I posted this that I have already posted this script on the forum :frowning_face:):

-- Script to link DEVONthink file and Bookends reference along with custom metadata
-- Kyle Eggleton April 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
		set theAbstract to abstract of theRef
		set theAbstract to abstract of theRef
		set theAuthor to authors of theRef
		set theTitle to title of theRef
		set theAttachments to attachments of theRef
		set theJournal to journal of theRef
		set theFormattedReference to format theRef using "APA 6th Edition Markdown.fmt" --Change to theformat in Bookends that you want
		
		-- Only set metadata fields for PDF if there is an attachment
		
		if theAttachments is equal to "" then error "There is no file attached to the Bookends reference"
		if theAttachments is not equal to "" then
			set theFile to the first attachment item of theRef
			set thePath to the path of theFile
			if theFile is "" then error "The file is missing from the Bookends reference"
			
			-- Set the metadata fields for PDF. Need to install exiftool (https://www.sno.phy.queensu.ca/~phil/exiftool/install.html)
			do shell script "/usr/local/bin/exiftool -title=" & quoted form of theTitle & " -author=" & quoted form of theAuthor & " -subject=" & quoted form of theJournal & " -overwrite_original " & quoted form of thePath
		end if
	end tell
end tell


-- Set URL to Bookends and custom metadata in DevonThink. Need to create custom meta data fields for Abstract, Citation and Reference

tell application id "DNtp"
	set theURL to ""
	set theRecord to the content record of think window 1
	if theRecord is {} then error "Nothing selected in DEVONThink Pro"
	set the URL of theRecord to ("bookends://sonnysoftware.com/" & theID) as text
	add custom meta data theAbstract for "abstract" to theRecord
	add custom meta data theCitation for "citation" to theRecord
	add custom meta data theFormattedReference for "reference" to theRecord
	set theURL to reference URL of theRecord
end tell

-- Set URL to DEVONThink in Bookends (using user4 field)

tell application "Bookends"
	tell front library window
		set user4 of publication item id theID to theURL
	end tell
	
end tell
10 Likes

The second script that I use for creating better linkages between Bookends and DEVONthink is a script that extracts the annotations from Bookends. These annotations are the ‘Note Cards’ created in Bookends. The script creates a Markdown annotations summary in the ‘Annotations’ group in the DEVONthink database where the indexed PDF exists. It also creates a link between the indexed PDF and the Markdown annotation summary that is stored in a custom meta data field ‘annotationlink’ as well as a link back to the Bookends reference

-- Script to create Annotations summary from Bookends reference linked to indexed  DEVONthink file 
-- Kyle Eggleton June 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" -- Change to Bookends format of your choice
		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
		set theLink to user4 of theRef
		
		
		--Error message if no DEVONthink link exists
		if theLink is {} then error "No DEVONthink link made in Bookends"
		
		
	end tell
end tell

--Format the annotations summary to Markdown
set theUUID to replaceText("x-devonthink-item://", "", theLink)
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

-- Create Annotations file in DevonThink
tell application "DEVONthink 3"
	set theRecord to get record with uuid theUUID
	set theGroup to create location "/Annotations" in the current database
	set theMDRecord to create record with {name:theAuthorDate & " (Annotations)", type:txt, URL:theLink, content:theAnnotations} in theGroup
	set name of theMDRecord to theAuthorDate & " (Annotations).md"
	set theMDLink to the reference URL of theMDRecord
	add custom meta data theMDLink for "annotationlink" to theRecord -- change the field name to suit own purposes/custom meta data field
	
end tell

--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

An example of what the annotations summary looks like is below

3 Likes

The final script that I use for linking between Bookends and DEVONthink is the creation of a Markdown reading brief file. This is stored in a group in DEVONthink called ‘Reading briefs’. The template creates four headings - Summary, Main Points, Critique, Question. The title, authors, citations, reference and abstracts are pulled from the Bookends reference that is linked in the PDF (created by the first script). The script creates a link in the PDF in a custom meta data field called ‘readingbrieflink’

-- AppleScript to create a Markdown reading brief from the current selection in DEVONthink Pro using Bookends data
-- Created by Kyle Eggleton April 2019

try
	
	
	tell application "DEVONthink 3"
		set theRecord to the content record of think window 1
		-- Requires a linked reference in Bookends  
		set theURL to the URL of theRecord
		if theURL does not contain "bookends" then error
		open tab for URL theURL
		close think window named theURL
		-- Get the URL of the selected record in DEVONthink
		set theLink to reference URL of theRecord
	end tell
	
	tell application "Bookends"
		tell front library window
			set theRefs to selected publication items
			set theRef to first item of theRefs
			
			-- Get properties of selected reference
			set theID to id of theRef
			-- Uses BibKey citation stored in user1
			set theCitation to user1 of theRef
			set theAbstract to abstract of theRef
			-- Uses a custom Bookends format ("Author Date.fmt") to pull out the Author and Date 
			set theAuthorDate to format theRef using "Author Date.fmt"
			set theAuthors to author names of theRef
			set theTitle to title of theRef
			-- Uses a custom Bookends format ("APA 6th Edition Markdown.fmt") to make a Markdown formatted reference
			set theFormattedReference to format theRef using "APA 6th Edition Markdown.fmt"
		end tell
	end tell
	
	-- Creates a Markdown record using the above properties
	-- Places link back to original record
	tell application "DEVONthink 3"
		set theGroup to create location "/Reading briefs" in the current database
		set theMDRecord to create record with {name:theAuthorDate & " (Reading Brief)", type:txt, URL:theLink, content:"# Reading Brief: " & theAuthorDate & "
	
**Title:** " & theTitle & "  

**Authors:** " & theAuthors & "

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

**Reference:** " & theFormattedReference & "  

**Abstract:** " & theAbstract & "  

## Summary  


## Main Points  


## Critique  


## Question  

"} in theGroup
		set name of theMDRecord to theAuthorDate & " (Reading Brief).md"
		-- Creates a link to reading brief from original record. Comment out two following lines if do not want link
		set theMDLink to the reference URL of theMDRecord
		add custom meta data theMDLink for "readingbrieflink" to theRecord -- change the field name to suit own purposes
		
		-- Opens reading brief to edit
		open tab for record theMDRecord
		activate
	end tell
	
	-- Error messaging	
on error errorMessage number errorNumber
	if errorNumber = -2700 then
		set theAlertText to "Error message"
		set theAlertMessage to "The DEVONthink record has no attached Bookends reference"
		display alert theAlertText message theAlertMessage as critical buttons {"OK"}
	else
		display dialog "Error: " & the errorNumber & ": " & the errorMessage buttons {"OK"} default button 1
	end if
end try

An example of what the reading brief looks like is in the screenshot below:

1 Like

This looks wonderful. I’m wondering, though: I store Bookends attachments in iCloud Drive to achieve syncing between Bookends and Bookends for iOS. Is creating custom metadata fields likely to “bother” iCloud?

I wouldn’t think so. The custom metadata fields are stored by DEVONthink and are independent of the PDF.

That’s good news. Thanks for the feedback (and for making the scripts available).

I’ve finally gotten round to experimenting with the first two scripts. I’m generally impressed with how they work, but have found something puzzling with this second script.

The annotations summary file’s contents are as follows:

  1. Reference
  2. All notecards (using the Bookends @-style page numbers)
  3. Title
  4. Authors
  5. Citation
  6. Reference
  7. All notecards (using the Bookends @-style page numbers)
  8. Abstract
  9. A level-2 heading “Annotations”
  10. All notecards (using the word “Page” before the page numbers)

This isn’t a big deal, since I can easily delete 2, 6, and 7 manually. But I am fascinated as to why this is happening!

Can you send me a screenshot to show me what you get

I’ve made the text super-small so that you can get an overall picture.
Since I posted previously, I’ve found that the problem appears to occur only when I have a lot of notecards associated with a reference in Bookends. Maybe the amount of information is overloading the script in some way? Things have worked as expected with references that have only 2 or 3 notecards associated.

I think that the problem is the reference format that you are calling from Bookends in this part of the script set theAuthorDate to format theRef using "Author Date.fmt". I am using a custom format “Author Date.fmt” which in the Bookends format manager uses the following fields “a d”. If you have a look at the reference format that you are calling you will probably find that you have an “n” field name in the format somewhere - which outputs the notes.

Thanks for the response, and sorry I didn’t notice it earlier. Yes, I had neglected to substitute in a format that actually exists on my computer in the spot you point out.

One other quick query:

Do you have a format actually called APA 6th Edition Markdown.fmt, or is the format called APA 6th Edition.fmt and it’s converted to Markdown on the fly?

Yes I have an APA 6th Edition Markdown format that I created

1 Like

Over at Sonny Software, I happened to run into the following topic with a similar goal: Autolinking Bookends and DEVONthink records. Between that script at sonnysoftware and the script posted here by @kseggleton, I have been finding cross-linking DT items and Bookends records super easy. Very cool, and much appreciate your sharing your script!

I’ve been looking for this solution for a long time, so thank you kseggleton for a really great job with those scripts. My only problem is that script for Annotations doesn’t extract any highlights/annotation from my pdf and returns only comments (notes) I wrote. Am I missing something in settings? I would really appreciate your help with this.

Welcome @solk

I can’t speak for the script, but have you looked at DEVONthink 3’s Tools > Summarize Highlights ?

The script extracts the ‘Note Cards’ in Bookends. If you create Note Cards with your highlights then it should extract them. It won’t extract highlights that are not Note Cards.

So, it seems that my Note Cards was blocked (the padlock icon). When I select the note and choose “Add Selected PDF Annotation nad Page # to Bookends” command (right mouse button) then the annotation is added to Notes and script works as expected. I highlighting pdf in my iPad, do I need to adding annotation for each pdf separately or it could be done automatically?

1 Like

The locked symbol just means that it is a PDF highlight rather than a Note Card. If you are highlighting on an iPad then there is no ability to create a Note Card on the iPad. Check out the Bookends forum on this topic - evidently they are working on an update for the iOS version of Bookends to create Note Cards. You might be better off using the ‘make summary from highlights’ feature of DevonThink in this case.

Thanks a lot

You actually now can now convert highlights to note cards on IOS. Simply long press them in the quote summary section (*i.e. not while in the PDF itself), and they convert to a note card. It is a bit of a pain in so far as you need to do each individually, and neither brings the page numbers over with them. I’m hoping this gets tightened up in future releases.