Seeking working JXA code to view record in current window

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.

Why does it need to be JXA?
This does what you’re talking about in pure AppleScript…

tell application id "DNtp"
	set newSheet to create record with {name:"new sheet", record type:sheet, columns:{"Site#string", "URL#url"}} in incoming group
	set selection of main window 1 to newSheet
end tell

JXA

let app = Application("DEVONthink");
const mySheet = app.createRecordWith({
  name: "new sheet",
  type: "sheet",
  columns: ["Site#string", "URL#url"]
}, { in: app.currentGroup() });

app.mainWindows[0].selection = mySheet;

PS: type is defined as recordType in the dictionary but this doesn’t produce a file. @cgrunenberg would have to comment on that.

Hey Jim - thanks for the quick response, as always. With regard to the “language wars”, of course it doesn’t need to be JXA, except that many of my scripts use extensive JavaScript features, so it’s a preference, but you knew that :wink:.

I’m away from my desk right now so will check your code when I get back.

The main difference seems to be the window type that you are targeting with selection.

Is that a known difference between AS and JXA?, since as I show, the working AS uses view window.

May have my terminology a bit mixed up - I’m typing in my phone with no view of the post.

Indeed, we all have our favorite languages. Path of least resistance

main window and viewer window are synonymous in AppleScript and the dictionary for JXA reports similar. However, the Application class lists mainWindows explicitly so I went with it. Also, thinkWindows also works but doesn’t distinguish between a window types.

Did you check the scripting dictionary? That’s the go-to if you’re unsure about terminology or the hierarchy of application elements.

There is no viewerWindow in JXA. In AppleScript viewer window is a synonym for main window, likely included for backwards compatability with older scripts. But there are no synonyms in JXA. You have to use the proper term, in this case mainWindow.

Setting the window selection is not necessary to display a record and would not be my default approach. Do you specifically want to avoid opening multiple tabs or is there some other reason?

…because you didn’t specify the in parameter :wink:

(() => {
	const app = Application("DEVONthink");
	app.includeStandardAdditions = true;
	
	const r = ... // Some record
	const w = app.thinkWindows()[0]; // Front window (whether 'documentWindow' or 'mainWindow')
	const newTab = app.openTabFor({ record: r, in: w });
	/* New tabs in existing windows open in the background.
	   Focus the new tab: */
	newTab.thinkWindow.currentTab = newTab;
})()

If you only want main windows you can do something like this:

(() => {
	const app = Application("DEVONthink");
	app.includeStandardAdditions = true;
	
	const r = ... // Some record
	let w = app.mainWindows()[0];
	if (w) {
		const newTab = app.openTabFor({ record: r, in: w });
		w.currentTab = newTab;
	} else {
		w = app.openWindowFor({ record: r.locationGroup });
		w.selection = r;
	}
})()

Thank you both for taking the time. and helping me learn some more.

@BLUEFROG I can confirm that your code works here for mainWindows and thinkWindows, but blows up for viewWindows, which I now know don’t exist in JXA, despite the indication in the JavaScript dictionary that they are synonymous. This is what led me astray along with recommendations from other (AppleScript) scripters years ago in this forum with untested suggestions. In this case the dictionary doesn’t offer any insight. I wonder how other people know about this - “no viewWindows in JXA”?

Secondly, I was trying to provide a list of one record because the dictionary indicates that for the selection property the parameter is:

“selection (list of Record or missing value) : The current selection. ‘selected records’ element is recommended instead.” Your code passes a record, not a list of records.

When I try to pass a list of one record, it blows up again with the error: “Error: Can’t convert types. (-1700)”.

So, now I know how to make this work:

 specify a `mainWindow` (or `thinkWindow`) and a `record`, 

 no `viewWindows` and no *lists*.

I did check that I could read the selection back ,

and dt.mainWindows[0].selectedRecords[0].uuid() returns the expected value.

@troejgaard thanks for pointing out my errors with dt.openTabFor({record:newSheet}). I haven’t used tabs before so didn’t pursue it further when I got a new window. In that case, I hadn’t checked the dictionary - my fault! I need to experiment some more and see if tabs is a good fit for this workflow, if so I have your code which works for me here. Thanks again.

Finally maybe the JavaScript dictionary should be updated to remove the synonym indication. It also shows that DocumentWindow is synonymous with ContentWindow which may, or may not, be true

@cgrunenberg ?

Most people aren’t using JXA and a viewWindow is mainWindow, as I pointed out earlier.

There’s no JavaScript dictionary actually, only one definition of the script suite (including synonyms). And the Script Editor.app shows the suite’s commands and properties in the syntax of the selected language (AppleScript, JavaScript, Objective-C). Unfortunately the app includes even unsupported features like synonyms for JavaScript.

1 Like