Inconsistency between current database and selected records

If one has several DT windows open

  • currentDatabase returns the name of the database in the currently selected window. So, this value can change when another window is selected.
  • selectedRecords, however, does not change when another window is selected.

I’m not sure if that is intentional. To me, it seems counter-intuitive: I think that the selectedRecords should change together with the currentDatabase. If it doesn’t, one can end up with selectedRecords that are in the currentDatabase, which seems weird.

The documentation in the scripting dictionary says

selection (list, r/o) : The current selection of the frontmost viewer window or the record of the frontmost document window. ‘selected records’ relationship is recommended instead especially for bulk retrieval of properties like UUID.

This seems to imply that selectedRecords behaves the same as selection in that it changes with the front most viewer window. Which, in my experience, it does not.

Simple script to test (I tried with two DT windows open on different databases):

let app = Application("DEVONthink 3");

And I can’t even reproduce this. How do you actually “select” another window? Is it on its own space?

Both windows are in the same space. I select by clicking somewhere in the window, that brings it to the front.

And how do you execute the script? In the Script

selected records actually just returns selection internally, the only important difference is the way it’s defined in the script suite.

I had it run in CodeRunner because that one does not save state between runs (while Editor does). In order for it to run more than once in editor, you have to wrap the code like so:

(() => {
const app = Application("DEVONthink 3");
console.log(`current DB: ${} [${}]`);
const selRec = app.selectedRecords()[0];
console.log(`selected records: ${} in database ${}`);

I added some more output to that to make clear what I perceive (!) as the problem. Here’s what I see in script editor’s “message” pane:

current DB: Bru6 [3]
selected records: Deutsche Rentenversicherung 2022-07-01 Nachfrage Lücken in database 2 

So the currentDatabase is # 3, while the database the currently selected record belongs to is # 2 (I consider this to be inconsistent).
You’ll of course see more interesting things when looking at the AppleScript events.
When I change the top window, the currentDatabase becomes #2, while the selected record remains the same.

Actually these properties are independent and don’t have to match. The selection might contain records from various databases whereas the database of the frontmost window (the current database) might not exist as a global smart group/rule is viewed.

1 Like

Thanks for the explanation. So, it all boils down to “how do I get the selection of the front-most window?”.
Something like

const records = app.viewerWindows()[0].selectedRecords();


In both cases the last activated window should be used. Are you able to reproduce the issue by using AppleScript and executing it via the Script Just to rule out issues of JXA or CodeRunner.

In AppleScript

tell application id "DNtp"
	set r to selected records
	log (name of first item of r) as string
	log (name of current database) as string
	log (name of root of viewer window 1) as string
end tell

gives me

(*Deutsche Rentenversicherung 2022-07-01 Nachfrage Lücken*)
(*ck Privat*)
(*ck Privat*)

and after switching the other window to the front

(*Deutsche Rentenversicherung 2022-07-01 Nachfrage Lücken*)
(*ck Privat*)

So, the current database is switched, but the root of the first viewer window and the selected records remain the same. I also wanted to show if the viewer window 1 is the front most, but AppleScript wouldn’t let me (log (frontmost of viewer window 1) as string throws an error). Or I don’t know the right lingo.

What’s actually selected in the second window, especially in the sidebar?

In the sidebar, the database “Bru 6” is selected. Nothing in the list view. The output does not change when I select something in the list view, though.

I guess a screenshot of both windows would be useful.

Try front viewer window instead.

Left window active (database is Bru6, selected record is group “Angebote”)

Script output (selected record, current DB, root of viewer window 1):

(*Deutsche Rentenversicherung 2022-07-01 Nachfrage Lücken*)
(*ck Privat*)

Right window active (database is “ck Privat”, selected record " Deutsche Rentenversicherung…")

Script output (selected record, current database, root of viewer window 1):

(*Deutsche Rentenversicherung 2022-07-01 Nachfrage Lücken*)
(*ck Privat*)
(*ck Privat*)

Does using front viewer window as suggested make a difference?

Nope. I used it as a command (front viewer window – not sure if that makes any sense, though) before all the other commands – no change.
Also, log front viewer window always gives me “viewer window 1”.

Just tried a similar setup but the results are the expected ones. Which macOS version do you use? Any third-party software that might be of interest or does a reboot in safe mode fix this?

It does, indeed. Now the fun starts – finding the startup item responsible. Thanks!
Edit Even better, after restarting in normal mode, everything behaves as it should.


/reboots FTW!! :smiley:

Yeah, well … I somehow don’t feel at ease with operating systems that require voodoo to work (again). That’s why I was using Linux for a long time – never had to reboot to get anything back on track. If something does not work as expected and starts to do so after a reboot, that’s an indication of a bug somewhere. Not necessarily in DT, of course – could as well be one of Apple’s frameworks or the OS itself. But still …