# How to open database in current DEVONthink window via JXA?

**URL:** <https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497>\
**Category:** DEVONthink\
**Tags:** devonthink3, automation\
**Created:** [February 27, 2021, 6:27am UTC](https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497 "2021-02-27T06:27:13Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![astrofin](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/astrofin/32/19320_2.png) [@astrofin](https://discourse.devontechnologies.com/u/astrofin)\
**Post date:** [February 27, 2021, 6:27am UTC](https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497/1 "2021-02-27T06:27:13Z")

</div>

I have built a JXA script that opens a database root.

The script currently opens the database in a new DEVONthink window.

How can I force the database to open in an existing DEVONthink window, if the app is already open?

I’m sure I’m overlooking something obvious, and am grateful for any advice. After searching around, I think I probably need to do something with `ViewerWindow` and `selection`, but I’m having trouble working out the JXA syntax.

My script looks like this:

```
// init methods for DEVONthink
const dT = Application('DEVONthink 3')
dT.includeStandardAdditions = true

// focus DEVONthink
dT.activate()

// get the database
const dTdb = dT.getDatabaseWithUuid("uuid-of-the-db")

// open the db root in a new window
dT.openWindowFor({record: dTdb.root() })
```

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [February 27, 2021, 10:01am UTC](https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497/2 "2021-02-27T10:01:57Z")

</div>

What seems to work here is

```auto
(() => {
const app = Application("DEVONthink 3");
app.openDatabase(absolutePathToDatabaseAsString);
})()

```

I’m not sure if that is what you want though: After executing the command, the DB is simply open when it was closed before.

But while fooling around with DT and JXA, I stumbled about this phenomenon:  
When I try to list all databases (`app.databases()...`), DT only returns those that are _open_. Same for AppleScript. If that is intended behaviour, it should be mentioned in the documentation I think (@cgrunenberg ?).  
How would one programmatically open all closed databases, for example?

---

<div class="post-metadata">

**Author:** ![pete31](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/d26b3c/32.png) [@pete31](https://discourse.devontechnologies.com/u/pete31)\
**Post date:** [February 27, 2021, 1:06pm UTC](https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497/3 "2021-02-27T13:06:30Z")

</div>

> [@chrillek](#):
>
> How would one programmatically open all closed databases, for example?

You would need to provide each path, in AppleScript:

```auto
open database thisPath

```

---

<div class="post-metadata">

**Author:** ![pete31](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/d26b3c/32.png) [@pete31](https://discourse.devontechnologies.com/u/pete31)\
**Post date:** [February 27, 2021, 1:37pm UTC](https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497/4 "2021-02-27T13:37:23Z")

</div>

> [@astrofin](#):
>
> How can I force the database to open in an existing DEVONthink window, if the app is already open?

You can set a window‘s `root`, in AppleScript:

```auto
set root of viewer window 1 to theGroup

```

To change it to the root of the desired database something like this should work, in AppleScript:

```auto
set theDatabase to open database "/abc/test.dtBase2"
set root of viewer window 1 to (root of theDatabase)

```

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [February 27, 2021, 1:57pm UTC](https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497/5 "2021-02-27T13:57:00Z")

</div>

TX @pete31  
That’s a bit of a catch 22: to open the database I have to know its path which I can’t get from the database unless it’s open. I can smell the logic behind it, but the documentation should state this behavior explicitly, I think.

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [February 27, 2021, 3:19pm UTC](https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497/6 "2021-02-27T15:19:56Z")

</div>

> [@pete31](#):
>
> ```auto
> set theDatabase to open database "/abc/test.dtBase2"
> set root of viewer window 1 to (root of theDatabase)
> 
> ```

this would translate to

```auto
const db = app.openDatabase("/abc/test.dtBase2");
let vw = app.vw();
vw.root = db.root();

```

**not** tested, though

---

<div class="post-metadata">

**Author:** ![astrofin](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/astrofin/32/19320_2.png) [@astrofin](https://discourse.devontechnologies.com/u/astrofin)\
**Post date:** [February 27, 2021, 6:35pm UTC](https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497/7 "2021-02-27T18:35:55Z")

</div>

Thanks @pete31 @chrillek , this is looking promising!

I’m still having trouble with the JXA however.

@chrillek, am I correct in thinking that `app.vw()` should be revised to `app.ViewWindow()`?

However, while this runs without error, the DEVONthink window is not updated to focus on db.root.

This seems very close, but I’m struggling to see why this isn’t working.

My code now:

```
// init methods for DEVONthink
const dT = Application('DEVONthink 3')
dT.includeStandardAdditions = true

// focus DEVONthink window
dT.activate()

// get the DEVONthink database for the selected project
const dTdb = dT.getDatabaseWithUuid("uuid-of-the-db")

// get the viewer window 
let vw = dT.ViewerWindow();

// set viewer window root
vw.root = dTdb.root();

```

I’ve also tried adding openDatabase after `getDatabaseWithUuid`

```
const dTpath = dTdb.path()

dT.openDatabase(dTpath)

```

Grateful for any more thoughts you may have.

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [February 27, 2021, 7:19pm UTC](https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497/8 "2021-02-27T19:19:07Z")

</div>

> [@astrofin](#):
>
> should be revised to `app.ViewWindow()`?

Kind of: `viewerWindow()` - note the lowercase v!  
Do you run the code in script editor? Then you can pepper it with `console.log()` statements. I suppose that `vw` is undefined.

---

<div class="post-metadata">

**Author:** ![astrofin](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/astrofin/32/19320_2.png) [@astrofin](https://discourse.devontechnologies.com/u/astrofin)\
**Post date:** [February 28, 2021, 11:41am UTC](https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497/9 "2021-02-28T11:41:12Z")

</div>

thanks @chrillek, this is precisely one of the issues that is confusing me!

I too had thought that `viewerWindow` (lowercase v) would be the correct syntax.

However, it raises an error in the script.

I changed this to `ViewerWindow` (uppercase v) to match the class definition in the Script Editor Dictionary (but I may very well be wrong to do this).

But you are correct that while this returns an object `[object ObjectSpecifier]` it appears to be empty.

Is this syntax working for you?

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [February 28, 2021, 1:00pm UTC](https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497/10 "2021-02-28T13:00:33Z")

</div>

> [@astrofin](#):
>
> Is this syntax working for you?

I didn’t really try it before. What you describes matches my experiences now, though.  
`viewerWindow()` throws an error, whereas `ViewerWindow()` returns an object. But setting this object’s `root` property to the root of a database does not do anything here. According to the code posted by @pete31, `ViewerWindow` should be a list (aka array). However, there’s no `length` property available on the object returned by `app.ViewerWindow()`.

I think @cgrunenberg would have to comment on this. Although I did some JXA scripting with DT, I never tried to do what you do (which is to get where, exactly?). DT and JXA are not always happy with one another, and some parts of DT are not really working with JXA at all (or not as one would expect).  
But maybe you don’t need all this viewer window stuff to achieve what you want to?

---

<div class="post-metadata">

**Author:** ![astrofin](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/astrofin/32/19320_2.png) [@astrofin](https://discourse.devontechnologies.com/u/astrofin)\
**Post date:** [February 28, 2021, 2:15pm UTC](https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497/11 "2021-02-28T14:15:35Z")

</div>

Thanks for your help with this, much appreciated!

I had wondered whether this was a failing on my part, or something more fundamental in the interaction between Applescript and JXA. From what I can tell, this seems to be relatively straightforward with Applescript, but as I’m adding this into a workflow automation that is entirely based in JXA, I was hoping there would be an easy way to translate. Hopefully @cgrunenberg can shed some light on whether or not this would be expected to work.

> But maybe you don’t need all this viewer window stuff to achieve what you want to?

Do you have an alternative approach in mind? As mentioned in my original post, I have this working with the `openWindowFor` method but it opens a new window each time it is run. I’d be very interested if you can suggest alternatives to viewer window that might enable me to change the selection in the active DT window.

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [February 28, 2021, 2:29pm UTC](https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497/12 "2021-02-28T14:29:58Z")

</div>

> [@astrofin](#):
>
> Do you have an alternative approach in mind?

Given that I don’t know any context of this - no, I don’t. The automation I did with DT always centred on (selected) records, there was never the need to open a window. The only other thing I’m doing with JXA in this context is adding files to different databases/groups with Hazel. Also straigh tforward.

---

<div class="post-metadata">

**Author:** ![pete31](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/d26b3c/32.png) [@pete31](https://discourse.devontechnologies.com/u/pete31)\
**Post date:** [February 28, 2021, 3:24pm UTC](https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497/13 "2021-02-28T15:24:19Z")

</div>

> [@astrofin](#):
>
> I’d be very interested if you can suggest alternatives to viewer window that might enable me to change the selection in the active DT window.

It’s `viewerWindows`, not `viewerWindow`. 🙂

In my AppleScript example I used `viewer window 1` as there’s no need to first get all `viewer windows` and afterwards `item 1 of`, at least not in AppleScript - don’t know about JavaScript.

This works.

```auto
// init methods for DEVONthink
const dT = Application('DEVONthink 3')
dT.includeStandardAdditions = true

// focus DEVONthink window
dT.activate()

// get the DEVONthink database for the selected project
let dTdb = dT.getDatabaseWithUuid("C06D2AA9-7453-4A6E-9A86-EDCF3B242A6F")

// get the first viewer window - no error checking included …
let vw1 = dT.viewerWindows()[0];

// set viewer window root
vw1.root = dTdb.root(); 

```

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [February 28, 2021, 5:04pm UTC](https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497/14 "2021-02-28T17:04:25Z")

</div>

This actually works, thanks.

---

<div class="post-metadata">

**Author:** ![astrofin](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/astrofin/32/19320_2.png) [@astrofin](https://discourse.devontechnologies.com/u/astrofin)\
**Post date:** [February 28, 2021, 6:56pm UTC](https://discourse.devontechnologies.com/t/how-to-open-database-in-current-devonthink-window-via-jxa/62497/15 "2021-02-28T18:56:36Z")

</div>

Brilliant! Thanks so much @pete31 , @chrillek. This does indeed work. Appreciate your help (and patience) with this.
