Annotation note vs. summarize highlights + customizability

I am looking for a way to use DT3’s summarize highlights feature in generating a PDF’s annotation note—ideally via scripting, and with the ability to customize e.g., the file name.

Has this been done before? Or might there be a straightforward/simple way of doing it?

I have a few ideas, but they are not straightforward…

Thanks!

1 Like

Summarize Highlights is not an AppleScript command in the dictionary so you can’t script that process.

There were some attempts at doing custom summarized docs, like this one…

However, there is nothing custom addressing summarizing highlights in PDFs.

1 Like

Thanks—I imagine I can script activating the feature via AppleScript “select menu item” commands (or whatever they’re called). (Or, worst case, via a Keyboard Maestro macro.) That’s part of my “non-straightforward” solution. The trick then is:

  • finding the newly created summarize highlights record; and
  • moving its contents to the annotation note.

Any thoughts or advice on either of those fronts would be appreciated!

Take a look at Use AppleScript to get oldest record in a given group. The name is misleading, the script can also get the newest record.

I would actually do it the other way round, there’s nothing more reliable to open a menu than Keyboard Maestro’s Select menu action.

1 Like

Not sure whether I understood what you want. If you want to append to an annotation note’s text, then you could do something like this:

-- Append text 

tell application id "DNtp"
	try
		set theAnnotationRecord to (get record with uuid "75BFE263-B17C-44E5-B339-88E7F10F64AE") -- testing, this could be the result 1 of "search"
		set theAnnotation_Text to plain text of theAnnotationRecord
		
		set theRecords to selected records
		if theRecords = {} or (count theRecords) > 1 then error "Please select one Summarize Highlights Markdown record."
		set theSummarizeRecord to item 1 of theRecords
		
		set theSummarize_Text to plain text of theSummarizeRecord
		set theSummarize_Text_paragraphs to paragraphs 3 thru -1 of theSummarize_Text
		set theSummarize_Text_trimmed to my tid(theSummarize_Text_paragraphs, linefeed)
		
		set newText to theAnnotation_Text & linefeed & linefeed & theSummarize_Text_trimmed
		set plain text of theAnnotationRecord to newText
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

on tid(theInput, theDelimiter)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	if class of theInput = text then
		set theOutput to text items of theInput
	else if class of theInput = list then
		set theOutput to theInput as text
	end if
	set AppleScript's text item delimiters to d
	return theOutput
end tid
2 Likes

Thanks, @Pete31, those are very handy!

It turns out finding the newest item wasn’t necessary, since the summarize filename is programmatically easy to generate.

In fact, this whole workflow was simpler than I thought it would be. The following script works. It needs a lot of additional “If” statements (e.g., it probably should behave certain ways if the annotation or highlights summary files already exist), but it works!

Note: it’s got some cruft in there (like my specific database UUID, the “bookendsID” custom meta data, and some references to Keyboard Maestro) that are specific to my use-case. If anyone uses it, be sure to clean it up first.

tell application id "DNtp"
	set theDatabase to get database with uuid "956EB0B2-F8DE-4955-873A-A065F0D096B7"
	set theSelection to get the selection
	repeat with eachItem in theSelection
		set theAnnotation to eachItem's annotation
		tell application "System Events"
			tell process "DEVONthink 3"
				click menu item "as Markdown" of menu of menu item "Summarize Highlights" of menu "Tools" of menu bar 1
			end tell
		end tell
		delay 0.25
		set parentUUID to eachItem's parent's uuid
		set highlightsSummaries to lookup records with file (eachItem's name & ".pdf" & space & "Summary.md")
		set highlightsSummary to item 1 of highlightsSummaries
		set highlightsSummaryText to plain text of highlightsSummary
		delete record highlightsSummary
		try
			
			set eachItem's annotation to create record with {type:markdown, name:annotationNoteName, content:highlightsSummaryText} in theDatabase's annotations group
			set annotationUUID to theAnnotation's uuid
			tell application "Keyboard Maestro Engine"
				setvariable "Annotation File Found" to "true"
			end tell
		on error
			set itemBookendsID to get custom meta data for "bookendsID" from eachItem
			set annotationNoteName to "∎ " & itemBookendsID & eachItem's name
			set eachItem's annotation to create record with {type:markdown, name:annotationNoteName, content:highlightsSummaryText} in theDatabase's annotations group
			tell application "Keyboard Maestro Engine"
				setvariable "Annotation File Found" to "false"
			end tell
		end try
	end repeat
end tell

Refined this a bit. It now handles whether a given DEVONthink record is already linked to Bookends via a custom meta data bookendsID, and if not, it asks for a little user help in making that happen. It should otherwise be cleaner than the above.

edit: further cleanups and edge case catches (Jan 27, 2021 4:50pm MST)

tell application id "DNtp"
	set theDatabase to get database with uuid "956EB0B2-F8DE-4955-873A-A065F0D096B7"
	set theSelection to get the selection
	repeat with eachItem in theSelection
		if eachItem's annotation exists then
			display dialog "This file already has an annotation note. Continuing will replace its contents."
		end if
		set recordUUID to eachItem's uuid
		set itemBookendsID to get custom meta data for "bookendsID" from eachItem
		if itemBookendsID is missing value then
			set bookendsPrompt to display dialog "This record is not yet linked to a Bookends reference. Select the appropriate reference in Bookends and click OK to continue." buttons {"OK", "Don't link with Bookends", "Cancel"}
			if the button returned of bookendsPrompt is "OK" then
				set linkToBookends to "true"
			end if
			
			if the button returned of bookendsPrompt is "Don't link with Bookends" then
				set linkToBookends to "false"
			end if
			
		end if
		try
			if linkToBookends is "true" then
				tell application "Bookends"
					tell front library window
						set selectedReferences to selected publication items
						set theReference to item 1 in selectedReferences
						set itemBookendsID to theReference's id
						set theReference's user20 to recordUUID
					end tell
				end tell
				add custom meta data itemBookendsID for "bookendsID" to eachItem
				set annotationNoteName to "∎ " & itemBookendsID & space & eachItem's name
			else if linkToBookends is "false" then
				set annotationNoteName to "∎ " & eachItem's name
			end if
		on error -- no value for linkToBookends, which means the record already had a bookendsID meta data
			set annotationNoteName to "∎ " & itemBookendsID & space & eachItem's name
			set linkToBookends to "true"
		end try
		
		set theAnnotation to eachItem's annotation
		
		
		-- create highlights summary file
		tell application "System Events"
			tell process "DEVONthink 3"
				click menu item "as Markdown" of menu of menu item "Summarize Highlights" of menu "Tools" of menu bar 1
			end tell
		end tell
		
		-- wait to make sure file exists
		delay 0.25
		
		-- get the summary file and its markdown text contents, then delete it
		set highlightsSummaries to lookup records with file (eachItem's name & ".pdf" & space & "Summary.md")
		try
			set highlightsSummary to item 1 of highlightsSummaries
			set highlightsSummaryText to plain text of highlightsSummary
			delete record highlightsSummary
		on error
			set highlightsSummaryText to "# [" & eachItem's name & ".pdf](" & eachItem's reference URL & ")"
		end try
		
		-- create the annotation text
		if linkToBookends is "true" then
			set highlightsSummaryText to highlightsSummaryText & return & return & "[Bookends Reference Link](bookends://sonnysoftware.com/" & itemBookendsID & ")"
		end if
		
		-- create the annotation
		set eachItem's annotation to create record with {type:markdown, name:annotationNoteName, content:highlightsSummaryText} in theDatabase's annotations group
		set theAnnotation to eachItem's annotation
		set annotationUUID to theAnnotation's uuid
		tell application "Keyboard Maestro Engine"
			setvariable "Annotation File Found" to "true"
		end tell
		
		set summaryNotesGroup to get record with uuid "8251BF25-C345-45E7-8198-42DE5276AC92"
		move record theAnnotation to summaryNotesGroup
		
	end repeat
end tell

I wrote this up and posted it here: Connect DEVONthink PDFs, Bookends references, and Obsidian summary notes with this script - Axle

1 Like

Hey Ryan,
I was looking at the page you posted a link to, and it looks like it may contain the feature I need but I’m not really sure.
My workflow is to capture webpages to DT as PDF files. I then add the PDF to TheBrain, and open it from there using HIGHLIGHTS. My highlights are saved by HIGHLIGHTS (the app) as an .md sidecar file which is now an attachment to my PDF in TheBrain. Then, and this is the part that I’d like to get rid of, I copy all of the text from the MD file and paste into the main note page in TB with the header HIGHLIGHTS. I would love if this could be automated from DT3, ideally. Does that functionality exist within your script?

I am not familiar enough with TheBrain to comment, sorry! If it provides decent AppleScript support, I’m sure something is possible. The script I provided above simply operates within DEVONthink though.