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?
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
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.
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,
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.)
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
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.
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.