How to *set* current database?

The nice thing about DTP (and up) is multiple discrete databases, yet you can search them as one… except when you use scripting. For example, this:


set theDatabasePath to "~/Documents/Secondary.dtBase2"
set theDropboxPath to "/Dropbox/Exports/"

tell application id "com.devon-technologies.thinkpro2"
	try
		set theDatabase to (open database theDatabasePath)

		-- This fails with a useless error message
		-- get database with id theDatabase
		
		-- 30 days in seconds 
		-- Wildcard search that returns results
		set theListing to search "article[0-9][0-9] pdf" age 2592000 comparison fuzzy within titles
		
		repeat with oneFile in theListing
			set theFilename to (name of oneFile as string)
			
			tell application "Finder"
				if not exists POSIX file (theDropboxPath & theFilename) then
					tell application id "com.devon-technologies.thinkpro2"
						export record oneFile to theDropboxPath
					end tell
				end if
			end tell
		end repeat
	
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

I cannot get it to select the “Secondary” database, it always eventually defaults back to “Inbox”. If I deliberately select the database then run the script (in AppleScript Editor) it seems to work. I’d like this to be more hands-off, maybe even automated.

It’s not possible to set the current database via AppleScript but you could specify the “in” parameter of the “search” command, e.g. the root aka top level group of the desired database.

BTW: Fuzzy isn’t applied to wildcards.

I guess that will have to do, thankfully I am just searching. It would be handy to be able to access smart groups through AppleScript to simplify things like this. Maybe you can, I just haven’t figured out how.

This works:


-- Export most recent documents matching a wildcard to a Dropbox folder
-- Created by Jontathan Markevich

set theDatabasePath to "~/Documents/Secondary.dtBase2"
set theDropboxPath to "/Dropbox/Exports/"

tell application id "com.devon-technologies.thinkpro2"
	try
		set theDatabase to open database theDatabasePath
		if not (exists current database) then error "No database is open."
		
		-- 30 days in seconds 
		set theListing to search "article[0-9][0-9] pdf" age 2592000 in (root of theDatabase) within titles
		
		repeat with oneFile in theListing
			set theFilename to (name of oneFile as string)
			
			set cmd to "if test -e '" & theDropboxPath & theFilename & "' ; then echo 0 ; else echo 1 ; fi"
			set notExists to ((do shell script cmd) as integer) as boolean
			
			if notExists is true then
				export record oneFile to theDropboxPath
			end if
		end repeat
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

I battled for the longest time with AppleScript which refused to detect if the file existed or not, so I found the little shell script workaround that works consistently.