Script: Open last added record

This script opens a window for the last added record across all open databases.

One of my use cases is opening webarchives which were created by dragging from Safari to DEVONthink in the Dock. In this case DEVONthink doesn’t show a notification which I normally can click to open the new record (e.g. to highlight text or write a comment). Maybe this should be added in future versions?

Don’t know if this can be done in a simpler way and it is not really well tested yet, I just wrote it, but it seems to work. Good bye “last added” smart group, hello shortcut :smiley:

tell application id "DNtp"
	try
		set theAge to (3600 * 24)
		set theComparisonDate to (current date) - theAge
		
		set theDatabases to databases
		repeat with thisDatabase in theDatabases
			set thisDatabasesResults to search age theAge in (root of thisDatabase)
			if thisDatabasesResults ≠ {} then
				set thisDatabasesLastAdded to item -1 of thisDatabasesResults
				if (addition date of thisDatabasesLastAdded) ≥ theComparisonDate then
					set theComparisonDate to (addition date of thisDatabasesLastAdded)
					set globalLastAdded to thisDatabasesLastAdded
				end if
			end if
		end repeat
		
		open window for record globalLastAdded
		activate
		
	on error error_message number error_number
		if error_number is -2753 then display notification "Im angegebenen Zeitraum wurde nichts hinzugefügt" with title "DEVONthink"
	end try
end tell

Handy - thanks for sharing!

Updated for DEVONthink 3

-- Open last added record (DEVONthink 3)

property alwaysOpenInDocumentWindow : true

tell application id "DNtp"
	try
		set currDate to current date
		set theComparisonDate to currDate - 86400 -- 24 hours
		
		set theDatabases to databases
		repeat with thisDatabase in theDatabases
			set thisDatabasesResults to search "additionDate >" & theComparisonDate in (root of thisDatabase)
			if thisDatabasesResults ≠ {} then
				set thisDatabasesLastAdded to item -1 of thisDatabasesResults
				if (addition date of thisDatabasesLastAdded) ≥ theComparisonDate then
					set theComparisonDate to (addition date of thisDatabasesLastAdded)
					set globalLastAdded to thisDatabasesLastAdded
				end if
			end if
		end repeat
		
		if alwaysOpenInDocumentWindow = true then
			
			set theDocWins to every document window
			
			repeat with thisDocWin in theDocWins
				if content record of thisDocWin = globalLastAdded then
					set thisDocsIndex to index of thisDocWin
					tell application "System Events" to tell process "DEVONthink 3"
						perform action "AXRaise" of window thisDocsIndex
					end tell
					activate
					return
				end if
			end repeat
			
			open tab for record globalLastAdded
			activate
			
		else
			
			open window for record globalLastAdded
			activate
			
		end if
		
	on error error_message number error_number
		if the error_number is not -128 then
			display alert "DEVONthink 3" & space & error_number message error_message as warning
		end if
	end try
end tell
1 Like