Search string to find dupes of selected item

I’d like to set a search string of viewer window using an AppleScript to list all duplicates of selected item. Is there an easy way to do it? Not just listing all UUIDs from the duplicates property of the record.

Not sure I understood. If you want to show the duplicates of a selected record in a window then you could get them and set the search results of the viewer window to those records.

-- Show duplicates in viewer window

tell application id "DNtp"
	try
		set theRecords to selected records
		if (count theRecords) ≠ 1 then error "Please select one record"
		set thisRecord to item 1 of theRecords
		set theDuplicates to duplicates of thisRecord
		set search results of viewer window 1 to theDuplicates
		activate
		
	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

Thanks @pete31, this is really cool that you can directly set the search results of viewer window. But this method has one downside: you cannot to further refine the search results in this window manually adding/removing the search criteria of advanced search. The only way to do it - is to compile the search string first and then set the search query of viewer window.

search results may be better in the situation where you need, say, to display 95 duplicates of selected item. Because if you choose to enlist all 95 dupes by their UUIDs in the search query, you’ll get a loooong one ) Just thought if there is a shorter way to compile such a search query… May be some operators or wildcards…

Yes, unfortunately. Would be really cool if setting the search results would create an virtual ad hoc group which could be additionally filtered via the toolbar. @cgrunenberg would something like this be possible at all?

I doubt it’s possible to reliably get duplicates this way as we can’t search for uuids. Interested to see what you come up with.

No such plans yet but noted.

1 Like

This script allows to search for all attachments in selected emails:

tell application id "DNtp"
	set theRecords to (get selection)
	if (count of theRecords) > 0 then
		set theString to {}
		repeat with theRecord in theRecords
			set theURL to get custom meta data for "airmailid" from theRecord
			set the end of theString to "mdairmailid==" & theURL
		end repeat
		set AppleScript's text item delimiters to " "
		set theString to theString as string
		set AppleScript's text item delimiters to ""
		set theString to "kind:!email {any: " & theString & "}"
		set search query of viewer window 1 to theString
		set search query of viewer window 1 to theString & " scope:mail"
	else
		display dialog "Select one or more emails in Devonthink" with title "No Emails Selected" buttons {"OK"} default button "OK"
	end if
end tell

This script allows the further refining of the search.

This script searches for all emails with selected attachment (using “dupe” term of DT3), and it of course, doesn’t allow refining:

tell application id "DNtp"
	try
		set theRecords to selected records
		if (count theRecords) ≠ 1 then error "Please select one record"
		set theRecord to item 1 of theRecords
		set theFound to {}
		set MailDB to get database with uuid "FD3DD7CE-58BE-4345-9421-0176ABFFBA5B"
		set theDuplicates to duplicates of theRecord
		repeat with theDuplicate in theDuplicates
			set theUUID to (characters 21 thru -10 of (get custom meta data for "messagelink" from theDuplicate)) as string
			set theFound to theFound & {get record with uuid theUUID in MailDB}
		end repeat
		set search results of viewer window 1 to theFound
		
	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

One possibility might be already to replicate the items to a temporary group in the script and to set the root of the window to this group. Afterwards the advanced search or filters could be applied.

Yes, this would be nice

Nice workaround, thanks!

I don’t understand this script. What does your custom meta data airmailid contain?

This field contains the link to the original message in Mail.app, which in its turn is the UUID of the corresponding .eml file in DEVONthink. The trick is that when you use “Import into DEVONthink” function for the emails, DEVONthink makes their message-id from the header == UUID of .eml item.

1 Like

Didn’t know that, very nice. Thanks!

That’s why, I guess, you can’t import already imported email :wink:

To see all dupes you’ll need to add thisRecord itself, so:

set theDuplicates to duplicates of thisRecord & {thisRecord}

Depends on what is considered a duplicate. If coming from a selected record I probably only want to see other records that DEVONthink considers a duplicate but not the selected record itself. That’s why I didn’t include it :wink:

Fair enough
I wanted to see all messages, containing this (or very close to this = dupe) attachment.

Just finished the script which does the same but asks with which rate of similarity to do this (means the ‘score’ limit for ‘see also’ collection) - Real DT AI power to the email database, no email client can boast of! )

How did you integrate this into the other script?

That’s correct.

Here it is:

A little bit of a description for the rest of interested:
This script searches for the emails containing similar attachment to which is currently selected in DT. The ‘rate of similarity’ you set at the beginning.

This is very useful when you want to find messages with different versions over time and threads or different formats of a given attachment (e.g. you choose pptx file, and script will return messages with the same or slightly changed pdf or docx variants of this attachment).

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

set theResponse to display dialog "Set the similarity:" & return & "from 1.0 (full duplicate)" & return & "to 0.0 (any similarity)" with title "How close to the selected item?" default answer "0.8" with icon note buttons {"Cancel", "Continue"} default button "Continue"

if button returned of theResponse is "Continue" then
	set theScore to (text returned of theResponse) as real
	set theList to {}
	set theFound to {}
	
	tell application id "DNtp"
		set MailDB to get database with uuid "FD3DD7CE-58BE-4345-9421-0176ABFFBA5B"
		try
			set theRecords to selected records
			if (count theRecords) ≠ 1 then error "Please select one record"
			set theRecord to item 1 of theRecords
			set theSeeAlso to compare record theRecord
			repeat with theItem in theSeeAlso
				if score of theItem ≥ theScore then set the end of theList to theItem
			end repeat
			if (count of theList) > 0 then
				repeat with theAttachment in theList
					try
						set theUUID to (characters 21 thru -10 of (get custom meta data for "messagelink" from theAttachment)) as string
						set theFound to theFound & {get record with uuid theUUID in MailDB}
					end try
				end repeat
				set search results of viewer window 1 to theFound
			else
				display dialog "No emails with similar attachments (similarity ≥ " & theScore & ")" with title "No Emails" buttons {"OK"} default button "OK"
			end if
			
		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
end if
1 Like