JXA — Open command — how to use?

I can create new Markdown file, and can open it in new window using JXA.

How to open new file in current window? I tried, and got errors.

Please help.

(function run() {
  const app = Application("DEVONthink 3");
  app.includeStandardAdditions = true;

  var dateISO = new Date().toISOString().slice(0, 10);

  var docTitle = app.displayDialog("New Note", {
    defaultAnswer: dateISO + " ",
  }).textReturned;

  const newRec = app.createRecordWith(
    {
      name: docTitle,
      type: "markdown",
      "plain text": `# ${docTitle}\n\n`,
    },
    { in: app.currentGroup }
  );

  // Opens in new window — OK
  app.openWindowFor({ record: newRec });

  //   Cannot open in current window — error
  app.open(newRec);

  //   Cannot open in current window — error
  app.open({ record: newRec });
})();

I think open does not work in this context. But @cgrunenberg would know better, of course.

You might be able to set a property of the top viewer window to the current record or something like that. I can’t check right now since I’m away from my Mac.

The open command is a standard AppleScript command but not supported by windows, tabs or records. Use openTabFor to open a new tab in the same window, in case of viewer windows having a preview pane you could also just set the selection if you want to reuse the current tab.

1 Like

Just app.openTabFor({ record: newRec }); makes the same result — opens in new window.

@cgrunenberg, can you please advise correct code?

In case of viewer windows you can set the window’s selection to the record.

@pete31, can you share working code please?

It’s really too few information about JXA for DEVONthink, meantime there are plenty of materials about JavaScript — MDN and so on — no problems with learning JavaScript.

Thank’s to chrillek’s Scripting with JXA | JavaScript for Automation (JXA) I could start.

I would, but I don’t know JavaScript or JXA.

@cgrunenberg, can you write a code example please?

You could try
window.selection = [record]

Check the scripting dictionary for the type of selection. That should give you an idea how it might work.

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.

It’s r/o. Anyway thanks.

Bummer. Then @pete31 might be able to provide a working example in AppleScript that can be translated.

That’s the selection of the app, use the one of the front viewer window.