Setting selection to n records?

Hi, is it possible to set the selection of a viewer window to n records?

Tried but got errors with “vector”. I then tried to work around by first getting the uuid (and getting the record afterwards), but no matter what I tried, again errors with “vector”.

What are you referring to by vector ??

Why are you trying to get some number of records in a viewer window?

2019-12-08_04-29-52

I stumbled over this every now and the, but still have no idea what this error means.

Would be very handy to automatically set the selection, e.g. to group records or script testing

The script throws the error on this line

set selectRecords to sel & thisRecord

property targetSelectionCount : 5

tell application id "DNtp"
	try
		set win to viewer window 1
		set theGroup to root of win
		set theRecords to children of theGroup
		
		repeat with thisRecord in theRecords
			set sel to selection of win
			set selCount to count of sel
			
			if selCount < targetSelectionCount then
				set selectRecords to sel & thisRecord -- error "vector"
				set selection of win to selectRecords
			else
				exit repeat
			end if
			
		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
	end try
end tell

@cgrunenberg would have to respond about the vector error, but in general, you usually shouldn’t look to automate things like this in the interface. It’s usually fragile and inconsistent, or requires a lot of exception handling.

It’s just a type error – it works without error if you make sure that both operands of & are lists:

set selectRecords to sel & {thisRecord}

(Not sure, of course, whether the effect is what you are aiming for)

That’s it. Thanks a lot!

1 Like

This one is not perfect, but very handy to group records.

I’m not sure how DEVONthink sorts records in AppleScript, however running it on an old database works fine if the window is sorted by addition date.

-- Set selection to n records

property targetSelectionCount : 100

tell application id "DNtp"
	try
		set theWindow to viewer window 1
		set theGroup to root of theWindow
		if theGroup = (root of current database) then error "Select a group in the navigation sidebar"
		set theRecords to children of theGroup
		set theRecordsCount to (count theRecords)
		
		-- Uncomment this block (by prefixing it with -- or #) if you don't want to be asked 
		set theDialog to display dialog "Set selection to n records" default answer targetSelectionCount buttons {"Cancel", "Ok"} default button 2 with title "DEVONthink"
		if button returned of theDialog = "Cancel" then return
		set targetSelectionCount to (text returned of theDialog) as integer
		
		if targetSelectionCount = 0 then return
		if targetSelectionCount > theRecordsCount then set targetSelectionCount to theRecordsCount
		if selection of theWindow = {} then set selection of theWindow to {item 1 of theRecords}
		set selectedRecords to selection of theWindow
		set selectedRecords_1 to item 1 of selectedRecords
		set selectedRecords_1_Position to my getPositionOfItemInList(selectedRecords_1, theRecords)
		set rangeEnd to ((targetSelectionCount + selectedRecords_1_Position) - 1)
		if rangeEnd ≤ theRecordsCount then
			set selectTheseRecords to items selectedRecords_1_Position thru rangeEnd in theRecords
		else
			set selectTheseRecords to items ((theRecordsCount - targetSelectionCount) + 1) thru theRecordsCount in theRecords
		end if
		set selection of theWindow to selectTheseRecords
		
	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

on getPositionOfItemInList(theItem, theList)
	repeat with a from 1 to count of theList
		if item a of theList is theItem then return a
	end repeat
	return 0
end getPositionOfItemInList