# Seeking working JXA code to view record in current window

**URL:** <https://discourse.devontechnologies.com/t/seeking-working-jxa-code-to-view-record-in-current-window/87836>\
**Category:** Automation\
**Created:** [October 13, 2026, 6:11pm UTC](https://discourse.devontechnologies.com/t/seeking-working-jxa-code-to-view-record-in-current-window/87836 "2026-10-13T18:11:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pfarrelle](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@pfarrelle](https://discourse.devontechnologies.com/u/pfarrelle)\
**Post date:** [October 13, 2026, 6:11pm UTC](https://discourse.devontechnologies.com/t/seeking-working-jxa-code-to-view-record-in-current-window/87836/1 "2026-10-13T18:11:44Z")

</div>

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.

```auto
(() => {
	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.

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [October 13, 2026, 7:16pm UTC](https://discourse.devontechnologies.com/t/seeking-working-jxa-code-to-view-record-in-current-window/87836/2 "2026-10-13T19:16:36Z")

</div>

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

```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**

```javascript
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.

---

<div class="post-metadata">

**Author:** ![pfarrelle](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@pfarrelle](https://discourse.devontechnologies.com/u/pfarrelle)\
**Post date:** [October 13, 2026, 7:37pm UTC](https://discourse.devontechnologies.com/t/seeking-working-jxa-code-to-view-record-in-current-window/87836/3 "2026-10-13T19:37:15Z")

</div>

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 😉.

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.

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [October 13, 2026, 7:48pm UTC](https://discourse.devontechnologies.com/t/seeking-working-jxa-code-to-view-record-in-current-window/87836/4 "2026-10-13T19:48:46Z")

</div>

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.

 ![Screenshot 2026-10-13 at 3.46.34 PM](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/1/d/1d378b466cd74bc4197c05de46b6578bb1901b96.png)

---

<div class="post-metadata">

**Author:** ![troejgaard](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@troejgaard](https://discourse.devontechnologies.com/u/troejgaard)\
**Post date:** [October 13, 2026, 9:19pm UTC](https://discourse.devontechnologies.com/t/seeking-working-jxa-code-to-view-record-in-current-window/87836/5 "2026-10-13T21:19:26Z")

</div>

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`.

> [@pfarrelle](#):
>
> I am trying to set the selection to a record, so that I can see its contents.

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?

> [@pfarrelle](#):
>
> `//dt.openTabFor({record:newSheet}); // works but opens a new window`

…because you didn’t specify the `in` parameter 😉

 ![SCR-20261013-openTabFor](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/3/3/334ce71de46583955986cae7c0ed05976eb2698e.png)

```javascript
(() => {
	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:

```javascript
(() => {
	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;
	}
})()

```

---

<div class="post-metadata">

**Author:** ![pfarrelle](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@pfarrelle](https://discourse.devontechnologies.com/u/pfarrelle)\
**Post date:** [October 14, 2026, 2:39am UTC](https://discourse.devontechnologies.com/t/seeking-working-jxa-code-to-view-record-in-current-window/87836/6 "2026-10-14T02:39:33Z")

</div>

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:

```auto
 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 ?

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [October 14, 2026, 3:05am UTC](https://discourse.devontechnologies.com/t/seeking-working-jxa-code-to-view-record-in-current-window/87836/7 "2026-10-14T03:05:37Z")

</div>

> [@pfarrelle](#):
>
> I wonder how other people know about this - “no `viewWindows` in JXA”?

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

---

<div class="post-metadata">

**Author:** ![cgrunenberg](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/cgrunenberg/32/7172_2.png) [@cgrunenberg](https://discourse.devontechnologies.com/u/cgrunenberg)\
**Post date:** [October 14, 2026, 5:34am UTC](https://discourse.devontechnologies.com/t/seeking-working-jxa-code-to-view-record-in-current-window/87836/8 "2026-10-14T05:34:29Z")

</div>

> [@pfarrelle](#):
>
> 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 ?

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.
