Putting AI better to work: Moving the item to a Document suggestion

I try to use the See Also & Classify inspector more. Often one of the suggestions in the Groups pane is the right one and dragging the document onto the groups does the trick: The item is moved into that group.

Sometimes however none of teh Groups suggestion is right but a document in the Documents* pane is in the correct group. Usually it is the second document in the pane like here:

Screen Shot 2021-01-24 at 10.28.44

How can I move an item into the group of a document in the Documents pane?

Why don’t you use Data > Move to ?

Thanks for taking this on! To your question

Why don’t you use Data > Move to ?

Well, I don’t really easily see where the group which contains the similar document is located. It’s somewhere deepish in the group/folder structure…

Of course I can search for it, then reveal and so go to the enclosing group and then drag/move the item in question there - but that’s exactly what I would like me the AI to help with!

So, is there a way to move an item into the group of a document in the Documents pane?

Yes, this script does it.

However we can’t get the exact same result as displayed in the See Also Inspector because

As compare record only compares to the record’s database we have to use compare content which means the script can’t compare images, it needs records with text.

This script

  • shows a choose from list with See Also suggestions’ parent groups
  • moves the selected record to the chosen group

Properties

  • maxLength: truncate group names at
  • truncateEnd: truncate end instead of middle
  • deduplicate: deduplicate group names
-- Move record to See Also suggestion's parent group

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

property maxLength : 125 -- truncate group names
property truncateEnd : false -- truncate end instead of middle
property deduplicate : true -- deduplicate group names

tell application id "DNtp"
	try
		set theRecords to selected records
		if theRecords = {} or (count theRecords) > 1 then error "Please select one record"
		set theRecord to item 1 of theRecords
		set theText to plain text of theRecord
		if theText = "" then error "No text to compare to"
		set theParent_UUID to uuid of parent 1 of theRecord
		
		set theSeeAlsoResults_record to {}
		
		repeat with thisDatabase in databases
			set theSeeAlsoResults to compare content theText to thisDatabase
			repeat with thisResult in theSeeAlsoResults
				set thisParent to parent 1 of thisResult
				set thisParent_UUID to uuid of thisParent
				if thisParent_UUID ≠ theParent_UUID then
					set thisName_truncated to my truncateText(name of thisParent)
					set end of theSeeAlsoResults_record to {name_:thisName_truncated, score_:(score of thisResult), uuid_:thisParent_UUID}
				end if
			end repeat
		end repeat
		
		if theSeeAlsoResults_record = {} then error "No See Also suggestions found"
		
		set theSeeAlsoResults_record_sorted to my sort(theSeeAlsoResults_record)
		
		set theUUIDs to {}
		set chooseFromListItems to {}
		
		repeat with this_record in theSeeAlsoResults_record
			set thisUUID to uuid_ of this_record
			set thisName_truncated to name_ of this_record
			if deduplicate = true then
				if thisUUID is not in theUUIDs then
					set end of chooseFromListItems to thisName_truncated & linefeed & thisUUID
					set end of theUUIDs to thisUUID
				end if
			else
				set end of chooseFromListItems to thisName_truncated & linefeed & thisUUID
			end if
		end repeat
		
		set theWidthPlaceholder_list to {}
		repeat maxLength times
			set end of theWidthPlaceholder_list to space
		end repeat
		set theWidthPlaceholder to my tid(theWidthPlaceholder_list, "")
		
		set theChoice to choose from list chooseFromListItems & {theWidthPlaceholder} with prompt "Move record to:" default items (item 1 of chooseFromListItems) with title ""
		if theChoice is false or item 1 of theChoice = theWidthPlaceholder then return
		set theUUID to item 2 of paragraphs of (item 1 of theChoice)
		set theGroup to get record with uuid theUUID
		move record theRecord to theGroup
		
	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 truncateText(theText)
	try
		if (length of theText) > maxLength then
			if truncateEnd = true then
				set theText_trucated to ((characters 1 thru (maxLength - 1) in theText) & "…") as string
			else
				set x to round ((maxLength / 2) - 1)
				set theStart to characters 1 thru x in theText as string
				set theEnd to characters -x thru -1 in theText as string
				set theText_trucated to (theStart & "…" & theEnd) as string
			end if
			return theText_trucated
		else
			return theText
		end if
	on error error_message number error_number
		activate
		display alert "Error: Handler \"truncateText\"" message error_message as warning
		error number -128
	end try
end truncateText

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

1 Like

In the next release, there will be an alternate contextual menu command (shown when holding the Option key) in See Also: Reveal in New Window so you can open a new window for a clicked item in the See Also. You could then drag and drop to that window.

3 Likes

Nice! Looking forward to it. I’ve also had this issue.

1 Like

Thanks. Will try it out.

1 Like