Use AppleScript to get oldest record in a given group

Hi,

I want to get the oldest record by creation date in a given group.
Through the user interface, it is elementary: I just sort all rows throught the ‘created’ field and pick the top row.

To do it through AppleScript, I use the following basic sorting algorithm:

tell application "DEVONthink 3"
tell database 1
	set myParent to every parent whose name is "Daily dose"
	set myParent to item 1 of myParent
	tell myParent
		set r to children
		set rc to count r
		set oldest_record to child 1
		repeat with x from 2 to rc
			set d1 to creation date of oldest_record
			set d2 to creation date of child x
			if d2 < d1 then
				set oldest_record to child x
			end if
		end repeat
	end tell
end tell

end tell

This works but very slowly, which makes it unpractical for the workflow that I have in mind.
For example, it takes about 20 seconds to run through a group containing about 820 records (the records are all emails).

Is there a better, more direct way to achieve what I want ?
Thanks in advance! W.

Tested without

set myParent to every parent whose name is "Daily dose"
set myParent to item 1 of myParent

I used a selected group with 820 children. Took 4 seconds. As far as I know there’s no better way.

What is the intent of scripting this?

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

@pete31:
Thank you for your much more efficient algorithm. I just tested it over my data and it takes 1’’ vs 20’’ for my initial script. It is not exactly what I wanted because I need to select the group in the GUI, but it is good enough as a workable solution.

In addition, it makes me aware that there are four different windows types in the DT3 AS object model. Now, I will try to figure out their differences. So thanks again.

@Bluefrog:
I have a group with emails that I need to process on a first-in first-out basis.

How do you want to use it?

Just sort the group on Date Created if you want to select the items manually. There’s no need for a script in this case. The most recent item will be at the top or the bottom.

@pete31
Ideally, the parent group would be selected in the ‘Navigate’ sidebar while the central viewer would show the records in a ‘List’ view, so I could see what’s happening in the subsequent stages of the workflow. But this is really no big deal: I now just select the parent group’s parent in the sidebar, the parent group in the central viewer and I still can see its children expanded under it in ‘List’ view.
Again, thank you very much - both for the solution and for showing me that it is much faster for AS to go through a list of dates than through a list of records of which the dates are a property.

@BLUEFROG
The problem that I shared (ie: selecting the oldest record through AS) is just the first step of a larger script which subsequently programatically processes the record in multiple ways. I run the script and passively check what’s going on, like in a movie, without any more manual intervention.
Thank you for your interest.

This should do it

tell application id "DNtp"
	try
		set theGroup to root of viewer window 1
		
	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