I’m trying to write an action that creates a new draft with template text and a tag, and opens it in a new window if possible. If not, it should open the draft in the regular editor (on iOS).
The goal is essentially “Quick Capture with a template and tag”, so that I don’t lose my place in the main Drafts window.
Here’s my script:
const template = `...`;
const d = new Draft();
d.content = d.processTemplate(template);
d.addTag(...);
d.update();
// Open in new window on Mac, editor on iOS
if (!app.openInNewWindow(d)) {
console.log("openInNewWindow failed");
editor.load(d);
}
When I run the action on macOS, app.openInNewWindow()
returns false
, the code in my conditional runs, and the new draft opens in both a new window and the main window.
I also tried the sample action New in New Window. This appears to fail in the same way, invoking context.fail()
, even though it does what it says on the tin.
-
Is this a bug? Or am I using this method incorrectly?
-
As a workaround, should I just use device.model
to check the platform and ignore whatever app.openInNewWindow()
returns?
For example:
if (device.model == 'Mac' || device.model == 'iPad') {
app.openInNewWindow(d);
}
else {
editor.load(d);
}
-
Related, can I invoke the Quick Capture window from an action? Or invoke actions from the Quick Capture window?
Oh, I should note as of 2025-04-30 I’m running the latest Drafts version 47.2 (573) on macOS 15.4.1 (24E263).
You might also want to look at these example actions for direction:
Thanks!
Is it true that the “context” of an action remains in the window it was invoked from? In other words, app
continues to refer to and act on the original window?
// Open in new window on Mac or iPad, editor on iOS
if (device.model == "Mac" || device.model == "iPad") {
app.openInNewWindow(d);
// It looks like the Action context remains in the window it was invoked
// from, so anything else we do with the interface will continue to happen
// in the original window.
app.currentWindow.showDraftList();
app.applyWorkspace(workspace);
} else {
editor.load(d);
}
You are reminding me of some issues I’ve had in the tracker for a while and not bothered to finish (
).
Those workspace methods need to be deprecated and moved to the Window
object, and some more window related methods added. Requires a little re-architecting that I’ve been avoiding.
I don’t think there really a great way to accomplish what you are trying to do right now. It’s a bit tricky to work with windows on iOS, so I haven’t devised a great solution.
Ha! No worries. I was thinking how difficult it must be to provide scripting access to windows in a unified way across all 3 platforms.
For my purposes, I already assigned a keyboard shortcut ⌘+0 to toggle the draft list. That’s plenty close enough.
Looking forward to seeing how you eventually tackle this. (Wisely after WWDC I’m sure, when we find out what they might do with the iPad again.)