Trying to compare record to a given database

A future release will make this much easier so these tasks can be accomplished without iOS, Siri or third-party software.

Ok, i think, in the meantime I’ll have to chose the database for the comparison, move the record and do the comparison.

Will try to play a little. If I get the time and a result, or run into trouble, I’ll check back in to the forums.

Thanks for the quick response time and support.

All the best,
Björn

I tried to compare text to a database but only get results from the current database. Is comparing to a given database possible?

Edit: This workaround does what I’m looking for

tell application id "DNtp"
	try
		-- choose from list ...
		--> Database_Name
		
		open window for record root of database Database_Name
		activate
		
		set theResults to compare content theText
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell
1 Like

This, I have to try. Thanks for sharing :grinning:

By default the current database is used, the to parameter can be used to specify the desired one.

This doesn’t work over here. Without my workaround (first setting the current database by opening a new root window) the current database is used. Perhaps I’m missing something? It seems @bjorn and @BLUEFROG got this result too, see this old post

How did the script trying to use the to parameter look like?

Here is the script I initially posted…

tell application id "DNtp"
	log (id of current database as integer) -- Get the ID of the current database
	
	set theRecord to item 1 of (get the selection)
	
	choose from list (name of databases as list) with title "Select the database for comparison"
	set chosenDatabaseName to the result as string
	
	log (id of database chosenDatabaseName as integer) -- Get the ID of the chosen database.
	
	compare content (plain text of theRecord) to database chosenDatabaseName
	
	-- Check the returns to see what DB the results came from.
end tell

And the results…

1 Like
tell application id "DNtp"
	try
		set theDatabase to database "_temp"
		set theResults to compare content "test" to theDatabase
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

A typo in the AppleScript support, the next maintenance release will fix this. Thanks!

2 Likes

I’m trying to compare with record instead of content but it only returns results of the current database.

tell application id "DNtp"
	try
		set theRecords to selection of viewer window 1
		if theRecords = {} then error "Nichts ausgewählt."
		
		set thisRecord to item 1 of theRecords
		set theDatabases to databases
		set allSeeAlsoResults to {}
		
		repeat with thisDatabase in theDatabases
			
			#set theContent to plain text of thisRecord
			#set theResults to compare content theContent to thisDatabase --> all databases
			#set allSeeAlsoResults to allSeeAlsoResults & theResults
			
			set theResults to compare record thisRecord to thisDatabase --> only current database
			set allSeeAlsoResults to allSeeAlsoResults & theResults
			
		end repeat
		
		
	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


What kind of record was selected?

PDF+Text

I’ve found some additional compare behaviour I don’t understand.

As it seems currently not possible to use compare record I used compare content. Maybe due to this some behaviour can be explained?

I’ve seen this depending on different PDF and Markdown records, behaviour does not change for the same record.

  • different results

    • why do AppleScript compare and UI See Also show different results?

    • are See Also’s results not sorted by score? (see below)

I collected compare results for two databases, then sorted them by score. The capture below shows that the See Also results are not sorted by score as the compare results definitely are, double checked as I was very surprised. This happened with different records. One thing I noticed is that it seems to happen only with pre-DEVONthink 3 records, but I’m not sure.

  • results count

    • why does See Also’s count change? Most often it’s 10, but sometimes less, sometimes more.

    • why is compare's count always 10?

    • would it be possible to get a results count parameter for compare?

Could you please explain a little how this works and which is the “realer” one? :slight_smile:

The inspector supports all opened databases and merges the results since version 3, the script commands work the same way they always did. In addition, the score is always relative to the last compare/classify/search command (see description of score property), therefore a simple merge of results of multiple databases will cause unexpected scores.

All good results but at least 10 (if possible) are returned. What’s the advantage of a parameter if it would only cause even more poor results?

Ah ok, didn’t know that it’s at least 10.

Not sure I understand. I’m doing everything in one go, does this not work?

-- Get See Also names via AppleScript

use AppleScript version "2.7"
use framework "Foundation"
use scripting additions

tell application id "DNtp"
	try
		set theRecords to selection of viewer window 1
		if theRecords = {} then error "Nothing selected."
		set theRecord to item 1 of theRecords
		set theContent to plain text of theRecord
		set theDatabases to databases
		
		set theSeeAlsoResults_record to {}
		
		repeat with thisDatabase in theDatabases
			set theSeeAlsoResults to compare content theContent to thisDatabase
			repeat with thisResult in theSeeAlsoResults
				set end of theSeeAlsoResults_record to {name_:(name of thisResult), score_:(score of thisResult)}
			end repeat
		end repeat
		
		set theSeeAlsoResults_record_sorted to my sort(theSeeAlsoResults_record)
		
		set theNames to {}
		repeat with thisResult in theSeeAlsoResults_record_sorted
			set end of theNames to name_ of thisResult
		end repeat
		
		set theNames_string to my tid(theNames, linefeed)
		
	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 sort(theList)
	set anArray to current application's NSArray's arrayWithArray:theList
	set theDesc to current application's NSSortDescriptor's sortDescriptorWithKey:"score_" ascending:false selector:"compare:"
	set newList to (anArray's sortedArrayUsingDescriptors:{theDesc}) as list
end sort

on tid(theList, theDelimiter)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	set theString to theList as text
	set AppleScript's text item delimiters to d
	return theString
end tid

I added another note to the AppleScript suite description as the to parameter is only relevant for comparing to contents. Comparing to a record simply uses the database of the record.

1 Like

This is of course a quite different script but should work.

That’s the one that I made the diff capture from. Shouldn’t the results then be the same? :thinking:

The inspector uses also certain (custom) metadata & properties whereas the script uses only the contents.

I assumed that but am surprised that it makes such a great difference. Thank you very much! :slight_smile: