How to open a group in new window in a new space?

One of my Keyboard Maestro shortcuts is for opening a new DEVONthink window on a specific group. It uses the following AppleScript code in that macro:

try
	tell application id "DNtp"
		set r to get record with uuid "461D0CBC-612E-4A7C-98F3-9D17499B593D"
		open window for record r
	end tell
end try

I want this to open a new window always, even if the group is already in an existing window, because I use virtual desktops (macOS “Spaces”) and often have another window open on the same group in multiple different Spaces. Here is the problem: the code above sometimes doesn’t open a new window, and instead, causes macOS to switch to a Space where the group is open in an existing window.

How can I make this always open a new window in the current Space, even if there is an existing DEVONthink window in another space open on the same group?

1 Like

We have no control over Spaces (and there’s not even an available API for us to use, AFAIK).

Have you tried disabling either of these options in System Preferences > Mission Control?

Here is something that should do what you’re looking for…

tell application id "DNtp"
	tell current database
		set newWindow to open window for record (get root)
		set root of newWindow to (get record at "/Automation") 
	end tell
end tell

Thanks for the quick reply. Regarding the preference settings, yeah, I already have those disabled:

image

That AppleScript code is not quite doing it either, but I’m experimenting with it. Stay tuned …

OK, I think I understand something about the behavior of this AppleScript code in 3.6.1:

tell current database
    set newWindow to open window for record (get root)
end tell

This seems to open a new window if there is no other window open on a group in that same database in the current Space. If there is a window open on a group, then DEVONthink does not open a new window.

But you gave me an idea. What if instead of root I use something that I rarely open in a separate window, say, the Inbox? That would cause a new window to be opened more reliably, and then we can switch the contents of the window. So that led to the following,

try
	tell application id "DNtp"
		set newWindow to open window for record (get record with uuid "inbox")
		set r to get record with uuid "461D0CBC-612E-4A7C-98F3-9D17499B593D"
		set root of newWindow to r
	end tell
end try

where the UUID is for the specific group I want to open via the keyboard shortcut. Bingo! This now consistently opens a new window, regardless of whether there are other windows anywhere open on the same group.

Thanks!

In case anyone’s interested, what I do in Keyboard Maestro is define multiple actions on the same key binding for DEVONthink, like this:

image

And the action for each one is the same, but with a different DEVONthink group UUID. Here’s an example:

Hitting that key binding in DEVONthink causes Keyboard Maestro to bring up a small menu asking you to disambiguate which particular action you have in mind,

image
from where you can type one of the highlighted letters to make a selection.

(This is a common approach to using Keyboard Maestro for selecting between alternatives. I’m just explaining it in detail because it seems this general question of how to open windows on specific databases has come up before. Maybe this will help future users.)

1 Like

Glad to assist.

Yes, it’s generally quite annoying and it’s very annoying if due to this a script acts on the wrong window:

That’s what I do too. Finally made a handler after reading this thread.

-- Open a new window

tell application id "DNtp"
	try
		set newWindow to my openNewWindow("DB278225-DEF3-40D7-9F38-A5A28961CBEA") -- set UUID of the group you want to open a new window for
		activate
		
	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 openNewWindow(theGroupUUID)
	tell application id "DNtp"
		try
			set theWindowUUIDs to uuid of root of every think window
			set theGroup to (get record with uuid theGroupUUID)
			
			if theGroupUUID is not in theWindowUUIDs then
				set newWindow to open window for record theGroup
				return newWindow
			else
				set theParentUUIDs to uuid of parents of database of theGroup
				repeat with thisParentUUID in theParentUUIDs
					if thisParentUUID is not in theWindowUUIDs then
						set newWindow to open window for record (get record with uuid thisParentUUID)
						set root of newWindow to theGroup
						return newWindow
					end if
				end repeat
				set newWindow to open window for record (get record with uuid thisParentUUID) -- Fallback
				return newWindow
			end if
			
		on error error_message number error_number
			activate
			display alert "Error: Handler \"openNewWindow\"" message error_message as warning
			error number -128
		end try
	end tell
end openNewWindow
1 Like

The behavior is intentional. May people complain of having too many windows open, especially redundant ones, e.g., “Why do I have sixteen windows of the same document open ?!?”

This makes sense, however without an option to open a new window instance for a given group it makes it hard in scripting.

I’m not sure whether you made a feature request from my old post I linked above, if not then this is a feature request.

It’s an annoyance to finish writing a script and then have to work around this behaviour, so please add an option to the open window for command that opens a new window if there’s already one opened. :slight_smile:

Development will have to assess this.

Sure. Thanks!

I just want to write in support of the request for a scripting way to reliably open a new window on a group regardless of whether there already exists another open window on the same group.

1 Like

It’s already planned to add another parameter to the command to achieve this.

3 Likes

Sorry to jump in, but this looks similar to a question I have:

I would like to simply have DevonThink open a new window starting from a group that I currently have highlighted.

I was hoping a “mouse right-click > open in new window” built-in command, but haven’t been able to find it.

Alternatively, I’m not (yet) an AppleScript coder, but I do use Keboard Maestro. Is there an existing KM Macro file or AppleScript that I can just use without having to learn all the details of coding them at this time?

The sidebar offers such a contextual menu item.

Thanks!

Somehow, I never looked there.

I hope they consider adding it to the right-click context menu too, at some point?

It’s actually the first request of its kind and the contextual menu is already very crowded.