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