Possible to open and/or switch to a database in existing window via applescript?

Hi,

I am trying to create a workflow to save some time and stop myself from getting distracted, but I keep getting stumped on one issue:

How do I open a database in an existing window via applescript? Or is that not possible?

I’d appreciate any help.

Thanks a lot.
Tim

How are you intending to define what database is opened?
What are you specifically trying to accomplish?

Thanks a lot for your quick response, @BLUEFROG .
I am trying to pick the database from a predefined list. Depending on the database I pick, some other programs should open as well.

I know that saved Workspaces can be selected via AppleScript. But this would be a workaround, so I am trying to figure out whether a database can be picked directly.

Cheers
Tim

This should do it

-- Open database (and other apps)

property databaseFolderPath : "/Users/Username/Documents/DEVONthink/DEVONthink Datenbanken/"
property databaseNames : {"database 1", "database 2"}

tell application id "DNtp"
	try
		set theChoice to choose from list databaseNames with prompt "Open database:" default items (item 1 of databaseNames) with title ""
		if theChoice is false then return
		set databaseName to item 1 of theChoice
		
		open database (databaseFolderPath & databaseName & ".dtBase2") as string
		
		open window for record (root of database databaseName)
		activate
		
		if databaseName = item 1 of databaseNames then
			-- open new safari window
			tell application "Safari"
				make new document
				activate
			end tell
			
		else if databaseName = item 2 of databaseNames then
			-- do stuff
			
		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

Thanks a lot, @pete31 . This is helps already.

However, it opens the database in a new window. Is there any way to open the database in the existing DEVONthink window, if one exists?

Thanks again for your help!

It’s not possible to set the current database. But adding this should work

if (count viewer windows) > 1 then
    set bounds of viewer window 1 to (bounds of viewer window 2) as list
    close viewer window 2
end if

This is fantastic. Thank you so much, @pete31.

One last question: is it also possible to open the Inbox for a particular database in an existing (or new) window?

Thanks again!!

open window for record (incoming group of database "database 1")
activate

Check out Script Debugger which makes exploring AppleScript dictionaries easy. There’s a free version too.

Thanks a lot. I just got my script to work – thanks to your help, @pete31 and thanks to Script Debugger.

I am really grateful for the fantastic help in this forum.

1 Like