Script: Open or close databases

This script opens or closes databases via a dialog.

Usage

  • Open a database: ⌘ + click
  • Close a database: ⌘ + click
  • Open all databases: ⌘ + A
  • Close all databases: Select last item (the empty line)

Setup

  • Set property theDatabaseFolderPath to your databases folder’s path (with trailing slash)
  • Set property theDefaultDatabases to the names of databases that should always be selected.
-- Open or close databases

property theDatabaseFolderPath : "/Users/USER/Documents/DEVONthink/Databases/" -- note the trailing slash
property theDefaultDatabases : {"_temp", "Test"}

tell application id "DNtp"
	try
		set theDatabaseNames to my getNamesFromDatabaseFolder()
		
		set theInboxName to name of inbox
		set theRunningDatabases to name of (databases whose name ≠ theInboxName)
		
		set theDatabaseNames_withRunningDatabases to theDatabaseNames
		
		repeat with thisDatabaseName in theRunningDatabases
			if thisDatabaseName is not in theDatabaseNames then
				set end of theDatabaseNames_withRunningDatabases to thisDatabaseName
			end if
		end repeat
		
		
		activate
		set theSelectedDatabases to choose from list (theDatabaseNames_withRunningDatabases & {""}) with prompt "" with title "Databases" default items (theDefaultDatabases & theRunningDatabases) with multiple selections allowed and empty selection allowed
		if theSelectedDatabases is false then return
		
		repeat with thisDatabaseName in theDatabaseNames_withRunningDatabases
			if (thisDatabaseName is not in theSelectedDatabases) and (thisDatabaseName is in theRunningDatabases) then close database thisDatabaseName
		end repeat
		
		repeat with thisDatabaseName in theDatabaseNames_withRunningDatabases
			if (thisDatabaseName is in theSelectedDatabases) and (thisDatabaseName is not in theRunningDatabases) then
				set thisDatabase to open database theDatabaseFolderPath & thisDatabaseName & ".dtBase2"
				if thisDatabase = missing value then set thisDatabase to open database theDatabaseFolderPath & thisDatabaseName & ".dtSparse"
				if thisDatabase = missing value then set thisDatabase to open database theDatabaseFolderPath & thisDatabaseName & ".sparseimage"
			end if
		end repeat
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

on getNamesFromDatabaseFolder()
	try
		tell application "System Events"
			set theFileNames to name of files of folder theDatabaseFolderPath whose name ends with ".dtBase2" or name ends with ".dtSparse" or name ends with ".sparseimage"
			set theList to {}
			repeat with thisFileName in theFileNames
				set thisFileName to thisFileName as string
				if thisFileName ends with ".dtBase2" then
					set end of theList to (characters 1 thru -9 in thisFileName) as string
				else if thisFileName ends with ".dtSparse" then
					set end of theList to (characters 1 thru -10 in thisFileName) as string
				else -- .sparseimage
					set end of theList to (characters 1 thru -13 in thisFileName) as string
				end if
			end repeat
			set theList_sorted to my sort_list(theList)
		end tell
	on error error_message number error_number
		activate
		if the error_number is not -128 then display alert "Error: Handler \"getNamesFromDatabaseFolder\"" message error_message as warning
		error number -128
	end try
end getNamesFromDatabaseFolder

on sort_list(theList)
	considering numeric strings
		set theIndexList to {}
		set theSortedList to {}
		repeat (length of theList) times
			set theLowItem to ""
			repeat with a from 1 to (length of theList)
				if a is not in theIndexList then
					set theCurrentItem to item a of theList as text
					if theLowItem is "" then
						set theLowItem to theCurrentItem
						set theLowItemIndex to a
					else if theCurrentItem comes before theLowItem then
						set theLowItem to theCurrentItem
						set theLowItemIndex to a
					end if
				end if
			end repeat
			set end of theSortedList to theLowItem
			set end of theIndexList to theLowItemIndex
		end repeat
	end considering
	return theSortedList
end sort_list

1 Like

Thanks for sharing this script! Is it intended that canceling the dialog closes all databases?

Encrypted databases aren’t supported currently but this revision does:

		tell application "System Events"
			set theFileNames to name of files of folder theDatabaseFolderPath whose name ends with ".dtBase2" or name ends with ".dtSparse" or name ends with ".sparseimage"
			set theList to {}
			repeat with thisFileName in theFileNames
				set thisFileName to thisFileName as string
				if thisFileName ends with ".dtBase2" then
					set end of theList to (characters 1 thru -9 in thisFileName) as string
				else if thisFileName ends with ".dtSparse" then
					set end of theList to (characters 1 thru -10 in thisFileName) as string
				else -- .sparseimage
					set end of theList to (characters 1 thru -13 in thisFileName) as string
				end if
			end repeat
			set theList_sorted to my sort_list(theList)
		end tell
2 Likes

Haha, no! I’ve copy pasted from a script that I use for some years and missed a line.

Never used encrypted databases… I’ve added it to the script.

Great feedback, thanks!

2 Likes

Pete, do you keep this script in your toolbar, or run it using a smart rule on opening DT, or…?

Smart rules requiring user interaction are possible but not really recommended as they should run seemlessly in the background.

Via a global Keyboard Maestro shortcut. Quite often I want to capture from Safari (via Clip to DEVONthink) and then realize that the database isn’t open. Press shortcut, open database, capture.

1 Like

I know - but sometimes (please don’t tell anybody) I actually do things which aren’t recommended :smiley:

1 Like

I have integrated your script into my routine for opening and closing DT; I’ve placed it in my toolbar and designed a little icon for it:

DT DB icon

(Instructions: save @pete31’s script to ~/Library/Application Scripts/com.devon-technologies.think3/Toolbar; in the file’s context menu in Finder select Get Info; drag the icon posted above to the generic symbol to the left of the file name in the info window; in DT select Customize Toolbar from the context menu of the toolbar; drag the icon/script to the toolbar)

Thank you, dear Pete, for sharing your work!

1 Like