I am trying to set the selection to a record, so that I can see its contents. The original intent was to create a new sheet and then have that sheet become the currently selected record and its contents be visible in DEVONthink. I simplified the problem by trying to do the same for an existing record specified by its UUID.
I found various references in the forum to setting the selection of the front viewer window in response to similar queries. The working code always seemed to be in AppleScript with proposals for JXA equivalents. I tried to make those suggestions work but ran into trouble.
I have included the code with comments showing what I could get to work and what I couldn’t.
(() => {
const dt = Application("DEVONthink");
dt.activate();
const recordUUID = "4F127AC3-E19D-4F0E-9C8B-495136FE1491"; // a sheet in the Inbox
const newSheet = dt.getRecordWithUuid(recordUUID);
//dt.openWindowFor({record:newSheet}); // works but opens a new window
//dt.openTabFor({record:newSheet}); // works but opens a new window
const vw1 = dt.viewerWindows[0];
/*
Seems to work
app = Application("DEVONthink")
app.selectedRecords()
--> [app.databases.byId(1).contents.byId(894225)]
app.getRecordWithUuid("4F127AC3-E19D-4F0E-9C8B-495136FE1491")
--> app.databases.byId(1).contents.byId(942975)
*/
//vw1.selection = [newSheet]; // fails
/*
Error: TypeError: undefined is not an object (evaluating 'vw1.selection = [newSheet]')
Result:
Error -2700: Script error.
*/
const recordList = [newSheet]; // works
//vw1.selection = recordList; // fails
/*
Error: TypeError: undefined is not an object (evaluating 'vw1.selection = recordList')
Result:
Error -2700: Script error.
*/
// .... but this works!
const app = Application.currentApplication();
app.includeStandardAdditions = true;
app.runScript(`
tell application "DEVONthink"
set newRecord to get record with uuid "${recordUUID}"
set selection of viewer window 1 to {newRecord}
end tell
`, {in: "AppleScript"});
})();
I did get my script to work by calling the working AppleScript from within JXA as shown at the bottom of my code.
The reason for the post is to see if anyone has any working pure JXA code for this.

