How do I batch Summarize selected notes or a group?

I would like to automate the use of DEVONthink’s Summarize service on multiple notes. I currently select a note, click inside, hit Cmd + A, then use the system-level hotkey I’ve assigned to the Summarize service. This is very cumbersome for groups of many similar results, such as the results automatically imported from the first few DEVONagent search set results. How could I automate the service so that I could select either a group or a selection of files, then run the Summarize service for each file? Many thanks!

Although the results are usually not identical, the summarize AppleScript command found in the standard additions might be useful.

Thanks! Is that the macOS service? I could only find summarize highlights of and summarize mentions of in the DEVONthink dictionary.

Yes, it’s the same as the service.

It’s not a DEVONthink AppleScript command.

You’ll find in the StandardAdditions:

  • Open Script Editor.app
  • Menu Window > Library
  • Click StandardAdditions
  • Search summarize

Here’s a capture taken from Script Debugger’s dictionary window:

Thanks, @pete31. I have a lot of trouble navigating Applescript so I appreciate the in-depth assistance. (And thank you for mentioning Script Debugger!)

I tried to adapt Blanc’s highlight summarization script to summarize rather than summarize highlights by changing:

summarize highlights of records {thisItem} to sheet in theDest # or use "markdown" or "rich" instead of "sheet"

to

summarize {thisItem} in 10

I keep getting a type error: DEVONthink 3 got an error: Can’t make {content id 104975 of database id 2} into type string. Is this because I am trying to operate on a record, and I need to do something like this?

set someVar to get plain text of {thisItem}
summarize someVar in 10

This won’t work as these are completely different things.

Take a look at the capture, the “type” for “direct parameter” is text or alias, which means there are two different things we can throw at this command either some text …

-- Using StandardAdditions's "summarize" by providing text

tell application id "DNtp"
	try
		set theRecords to selected records
		if theRecords = {} then error "Please select a record"
		set theRecord to item 1 of theRecords
		set theRecord_Text to plain text of theRecord
		
	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

set theRecord_Text_summarized to summarize theRecord_Text

… or an alias.

-- Using StandardAdditions's "summarize" by providing an alias

tell application id "DNtp"
	try
		set theRecords to selected records
		if theRecords = {} then error "Please select a record"
		set theRecord to item 1 of theRecords
		set theRecord_Path to path of theRecord
		
	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

set theRecord_Path_Alias to POSIX file theRecord_Path as alias
set theRecord_Text_summarized to summarize theRecord_Path_Alias

Thanks again, @pete31. I’ve had a little trouble working with the one for aliases. I did manage to hack together a script that will Summarize an arbitrary number of selected files and save the results as a Markdown file.

-- Batch Summarize selected files

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application id "DNtp"
	set theRecords to selected records
	if theRecords = {} then error "Please select a record"
	set theFinalSummary to ""
	try
		repeat with theRecord in theRecords
			set theRecordName to name of theRecord
			set theRecord_Text to plain text of theRecord
			set theRecord_Text_summarized to summarize theRecord_Text in 5
			set theFinalSummary to theFinalSummary & return & "## " & theRecordName & return & theRecord_Text_summarized & return
		end repeat
		
		create record with {name:"Selected Records Summary", type:markdown, plain text:"# Selected Records summary" & return & return & theFinalSummary} in current group
		
	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

A nice quality of life improvement would be the ability to recognize and reformat Summarize results that are Markdown header-level text. Is there a more elegant way to write an output record from a for repeat loop?

Not sure what that means. Could you explain what you’re looking for, maybe with an example?

Instead of appending to a string variable (theFinalSummary) I would collect everything in a list (set end of theFinalSummary_list to XY), then add the level 1 heading to the list and use this handler

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

It probalby doesn’t make a difference, but it feels easier to figure out the desired output this way.

Most of my Markdown files restate the file name as an H1 header. (I suppose that’s a habit rather than a requirement.) Many times the script captures the file “title” as something worth summarizing, so I frequently end up with results like this:

This certainly involves some conditional logic that strips the header from the line (and perhaps removes the link using regex), but it has been a long week and I’ll just suffer through it for the time being. :upside_down_face:

It is funny that you mention this. I found the same function in your Create Markdown See Also List script (here) and it had me scratching my head. I suppose it’s worth going back to that script and studying that function.

By the way, I wanted to modify the Create Markdown See Also script in two ways that may be interesting or helpful, but I’m having trouble with both:

  1. The script can’t handle notes that start with >. Many of my book notes start with blockquotes from the source material, so the script breaks frequently for me.
  2. I wanted the text excerpt to use Summarize rather than the first n paragraphs.

Use a repeat to check each line. If it starts with # (note the space) throw it away. If it doesn’t add it to a list.

True. Please post it in the other thread.