Activate/Bring Open DEVONthink Window to Front - A Very Basic AppleScript Question

I have a very basic AppleScript question that I’m ashamed to ask, given how simple it seems, but that I have somehow made overly complicated.

In short, I’d like to know how to activate a DEVONthink window that’s already open using AppleScript. The catch is that it has to work across different desktops/spaces (i.e., so that if the window is open on a different desktop/space from where you’re currently working that your screen will automatically shift to that desktop/space), and it also needs to work for both document and viewer windows.

I’ve been toying around with this for quite some time, but I haven’t quite come up with something that works under both conditions described above. If you’re curious, it’s part of an Alfred workflow that I’d like to start using more (And, yes, I’ve used app switchers, like Witch, and am just looking to accomplish this through something homegrown).

Because I think I’ve made this issue overly complicated, let me ask a more basic question: How can I use a think window id number to activate/bring-to-front a given DEVONthink window?

For example, at the moment, I have two open think windows: a document window and a viewer window. And here’s the output from Script Editor:

Script Editor

Now, let’s say that I want to activate/bring-to-front the viewer window with the id of 34099. In other words, assume that you know you want window id 34099 from a prior step or input (like from Alfred, etc.). How can I use the window ID number 34099 to activate/bring-to-front that specific window via AppleScript? Is there a generalizable approach that will work for document and viewer windows (e.g., so that if I gave you id 29150, from the example above, that it would also work for that document window)?

Thanks a ton for any help you can lend! Thanks for helping this scripting neophyte out!!

It’s not simple. I’ve tried the same thing more than once and searched over and over just to find again that it’s not possible.

To get a DEVONthink window via its id use

set theWindow to first window whose id = theID

In general raising a window can be done via System Events. System Events doesn’t know if it’s a DEVONthink viewer or document window and it doesn’t know what a DEVONthink id is, so you’ll have to get the window’s index

set theIndex to index of theWindow 

Then tell System Events to raise the window

tell application "System Events"
	tell process "DEVONthink 3"
		perform action "AXRaise" of window theIndex
	end tell
end tell
Here's a working demo
-- Demo: Bring window to front (via System Events)

tell application id "DNtp"
	try
		set theWindow to viewer window 2
		set theID to id of theWindow
		
		set theWindow to first window whose id = theID
		set theIndex to index of theWindow
		
		tell application "System Events"
			tell process "DEVONthink 3"
				perform action "AXRaise" of window theIndex
			end tell
		end tell
		
	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

In case of DEVONthink there are also two hacky and not very reliable ways to bring a window to the front

open window for record theRecord
activate

and

do shell script "open " & theRefURL
activate

Of course this only works if a viewer window’s root has a reference URL, e.g. trash can’t be raised this way.

If two viewer windows show the same group (= their root’s uuid is the same) or the same record is viewed in more than one window it’s also not possible to raise a specific window. In those cases they always raise the oldest window for a given record (= the one with the smallest id).

Unfortunately System Events and those hacky ways can only act on windows in the current space.

But thanks to your post I’ve tried again - this time with success!

Another hacky way maybe will do what you want. If

open window for record theRecord
activate

is not used once, but twice

open window for record theRecord
activate
open window for record theRecord
activate

then a viewer window whose root or a document window whose content record is theRecord will be raised if it’s on the current space - if it’s not the space switches to the one the window is on.

As having multiple windows with the same root/content record open is quite seldom over here this is exactly what I was looking for - for years!

I’ve already written a window switcher script with this hacky solution (I’ll post a link to a new thread).

:slight_smile:

There’s one important thing:

If you use TotalSpaces.app you’ll have to make sure to disable the option “Use transitions”. If this is enabled it’s likely that your spaces switch, switch back, switch and so on …

1 Like

@pete31 I can’t thank you enough. Honestly, this is too kind. Thanks a ton for digging into this for me, and for your explanations. This is super helpful. And, sorry for the slow response. We have a little one at home that is more than a handful right now!

I spent forever poking around, but kept running into all of the dead ends that you point out above. In fact, I landed on the shell script approach that you described, but I couldn’t get it to work reliably with viewer windows.

I thought I was overlooking something simple, like using the window ID (I just assumed it was used for this purpose). In any case, thanks for letting me know that I’m not crazy!

I can’t wait to dig into this tomorrow. Thank you so much!!

2 Likes