A "See Also" report

This script, when executed while a single record is selected in DEVONthink, will produce an RTF file containing the names and clickable links to all comparable items in the database. In other words, it is a “See Also” report. It produces results (over here) that are the same as “See Also” produces.

Why use this? It is a demo. My intended use is to copy the contents of the “See Also Report” and paste them into Tinderbox, Scrivener, or TheBrain or a mind mapping program for evaluation, linking thoughts, notes, etc. Whatever. I encourage anyone who uses this script and expands the concept to post their revisions of the script back to this forum for others to use.

The core of the script is one line of code

set theSuggestions to content text compare record content record

The rest of the code is about producing a report.

	AI Test of Compare Command v3 20150201
	This script will produce a report, in RTF format, of the items in the current databases that are similar to the selected item with regard to 'Text' content.  It produces results congruent with the See Also feature in DEVONthink Pro Office.
	Use: select a record.  Run the script.
	If the property ask_destination is "true" then the script will prompt for a destination, otherwise not
	The script can be used by all -- with the provision that if you make changes or enhancements that you post the revised script to the DEVONthink forum so that all may use the improvements
	v3: added a switch (property include_text, default false) to add to the report the first paragraph of the text of each comparable record.

property ask_destination : true
property include_text : false

tell application id "DNtp"
		set theSuggestions to compare content text record content record
		set defaultReportName to "See Also for: " & (the name of the content record)
		set o_HTML to "" & quoted form of ("<font face=\"helvetica\">" & "See Also for: " & "<a href=\"" & reference URL of the content record & "\">" & name of the content record & "</a></font> </br></br>")
		repeat with thisSuggestion in theSuggestions
			set thisName to the name of thisSuggestion
			set thisURL to the reference URL of thisSuggestion
			if include_text then
				set endText to quoted form of ("<br>" & the first paragraph of the rich text of thisSuggestion & "</font><br><br>")
				set endText to quoted form of "</font></br>"
			end if
			set o_HTML to o_HTML & quoted form of ("<font face=\"helvetica\"><a href=\"" & thisURL & "\">" & thisName & "</a>" & endText)
		end repeat
		-- set o_RTFLink to (do shell script "echo " & o_HTML & " | textutil -format html -convert rtf -stdin -stdout" as rich text)
                set o_RTFLink to (do shell script "echo " & o_HTML & " | textutil -format html -convert rtf -inputencoding UTF-8 -stdin -stdout")
		set documentName to text returned of (display dialog "Please enter the new file name	" default answer defaultReportName with title "Name of File?")
		if ask_destination then
			create record with {name:documentName, rich text:o_RTFLink, type:rtf} in display group selector
			create record with {name:documentName, rich text:o_RTFLink, type:rtf} in the current group
		end if
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

The script bombs if a group is selected. I’ll fix the error handling on that someday.

Works well, I can see this being most useful. Thanks form :slight_smile:

Thanks, korm! I am excited to think of uses for this in my teaching.

Thanks, korm! Very useful.

Thanks a lot korm.
It appears that Unicode characters in the names of the similar files are not handled correctly:
“retrouvée” instead of “retrouvée”
It might have to do with a bad automatic conversion from UTF8 to UFT16, but I’m not too sure how to fix this in the script.
Does anyone have an idea?
Best regards

Not fully tested but I believe if you change

set o_RTFLink to (do shell script "echo " & o_HTML & " | textutil -format html -convert rtf -stdin -stdout" as rich text)


set o_RTFLink to (do shell script "echo " & o_HTML & " | textutil -format html -encoding UTF-16 -convert rtf -stdin -stdout" as rich text)

that might work. Please let me know if not.

This looks super-useful!

Unfortunately, doesn’t appear to be working here?

I presume I can select a PDF+Text, or must it be a RTF as a the selected record?
Where is the new RTF report stored/placed by default – cannot find anything being produced. :blush:

As per usual, I might be having a face-palm moment again, error might be sitting in the chair over here! 8)

@Cassady, you might want to look at the instructions in the header of the script.

The script works on a single record – of any kind. It works like See Also – it suggests related records for that single selected record.

The report is stored in the same group as the selected record, unless the switch, below, is “true” – in that case you are prompted for a destination

property ask_destination : true

and yes, I verified yet again, that the script actually works :open_mouth:

Thanks a lot korm for your help. I have just tested it and unfortunately the two resulting reports (before/after) are exactly the same: recadré instead of recadré, concentré instead of concentré, Façade HD instead of Façade, etc.

@Edouard try

set o_RTFLink to (do shell script "echo " & o_HTML & " | textutil -format html -convert rtf -inputencoding UTF-8 -stdin -stdout")

It works perfectly now, thanks korm!

Great. I updated the script in the original post.

[EDIT] >> It’s working! :laughing:

I was pretty sure a facepalm was due, and it was. Never thought to quit/restart DTPO – that did the trick. Occurred to me a few seconds after popping up the below, just did it, and working a charm.

Super useful script this – many thanks, as always!