Use AppleScript to get oldest record in a given group

Yes.

This one needs 6 seconds for a 8700(!) records group.

-- Get oldest (or newest) record

property getOldest : true

tell application id "DNtp"
	try
		set theSelection to selection of think window 1
		if theSelection = {} then error "Nothing selected."
		set theGroup to item 1 of theSelection
		
		set theCreationDates to creation date of children of theGroup
		set theDate to item 1 of theCreationDates
		
		if getOldest = true then
			repeat with thisDate in theCreationDates
				if thisDate < theDate then
					set theDate to thisDate
				end if
			end repeat
		else
			repeat with thisDate in theCreationDates
				if thisDate > theDate then
					set theDate to thisDate
				end if
			end repeat
		end if
		
		set theDate_1 to (theDate - (1 / 60 * minutes))
		set theDate_string_1 to (year of theDate_1 & "-" & ((month of theDate_1) as number) as string) & "-" & day of theDate_1 & space & hours of theDate_1 & ":" & minutes of theDate_1 & ":" & seconds of theDate_1
		set theDate_2 to (theDate + (1 / 60 * minutes))
		set theDate_string_2 to (year of theDate_2 & "-" & ((month of theDate_2) as number) as string) & "-" & day of theDate_2 & space & hours of theDate_2 & ":" & minutes of theDate_2 & ":" & seconds of theDate_2
		
		set theResults to search "creationDate>=" & theDate_string_1 & space & "creationDate<=" & theDate_string_2 in theGroup
		
		if (count theResults) = 1 then
			set theRecord to item 1 of theResults
			#open window for record theRecord
			#activate
			#else
			#error "More than one result."
		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
	end try
end tell
1 Like