Can the current database be changed by script?

The property seems to be read-only, so the simple approach doesn’t work:

set current database to database "Work"

Is there another way to accomplish this?

What is the purpose of trying to change the current database?

To use a keyboard shortcut to switch between a small number of frequently used database (which would also open the requested database if it wasn’t currently open).

Then this wouldn’t be “setting the current database”. It would be a filesytem operation.

All you need is an open command operating and the POSIX path of the database.
If the database isn’t open, it will open.
If the database is open, it will switch to an active window or open a new windw for the database.

PS: If you’re using Keyboard Maestro, you could set up a palette of databases. This would make a hotkey available outside DEVONthink, as you can’t assign a hotkey to scripts with an out-of-the-box Mac (i.e. it requires FastScripts or other utility). You could create a Service in Automator as well, if you are inclined in that direction.

I had tried this already, but running this code (when the database is already open, but not the current database) does not change the view in the active window for me:

tell application "DEVONthink Pro"
	set thePath to path of database "Work"
	open database thePath
end tell

It doesn’t open a new window either, but that wouldn’t be what I want anyways; I want the same effect as if I had clicked on the database in the left panel of the active window.

Yes, I was planning to trigger these scripts via Keyboard Maestro, but first I have to get them to work. :smiley:

I guess I can fall back on Keyboard Maestro’s “Move and Click … on the Found Image” action, but I prefer to use application scripting of UI scripting, which can be less reliable.

I just tried this, and it does indeed work:

I’m still interested if the same effect is possible using DEVONthink scripting, which would not be affected by changing screen resolutions (running on iMac vs. Macbook).

That’s not going to happen via scripting. I would suggest what you’re attempting, with changing the active window, is not a good idea. Trying to get UI interaction is not a reliable thing.

What’s the problem with opening or switching to a window?

Nothing wrong with it. :slight_smile:

I just prefer to use the keyboard over the mouse/trackpad for navigation and was looking for a way to switch databases in the current window.

It is fine if this isn’t possible with DEVONthink scripting; I can just take screenshots of the other database names (including the little icon in front, to make them unique), and use the KM technique I showed above.

It is working fine so far (but I haven’t tested yet if I need separate screenshots on the laptop). It’s just that moving the mouse pointer around and clicking on things like that using automation makes my skin crawl a little bit. :smiley:

Not tried it so may not be possible but could you assign a keystroke to every database in File>Open Recent?

It turns out that I indeed need to search for the image using all the different screen resolutions I use (iMac, external monitor, and laptop), but then it seems to work everywhere:

So yeah,

set current database to database "Work"

would be so much simpler if it could be made to work, but at least I have a workaround.

I don’t think so. Even if you could, the order of the databases in that menu is not constant, so the keystroke would change every time you loaded/reloaded a database.

Okay I have just accomplished what you want to do in KM. Very simple:

Great, but how did you do it? Your screenshot just show the name of your macro, but not the implementation… :smiley:

In KM set a new hot key to open the database on your hard drive. It is like opening any application except point it at the database file instead of an application.


Thank you!

It once opened the database in a new window, but that must have been an artifact of my previous experiments. After restarting DEVONthink this worked exactly how I wanted it to, and it is so much simpler and more robust than the image matching!

Also it hs the advantage that you do not even have to have DevonThink open, it will open it for you! Pleased it helped. :slight_smile:

Check the bottom section of the Window menu in DEVONthink.

Thank you! I somehow missed this because now all the windows are in exactly the same position.

Can be fixed by running a quick script though:

tell application "DEVONthink Pro"
	set theWindowList to the viewer windows
	repeat with theWindow in theWindowList
		if theWindow's name is not "Work" then
			close theWindow
		end if
	end repeat
end tell

Of course this relies on my personal preference for running with a single viewer window at (almost) all times.