Use scripting to select the draft which has oldest ‘modified’ date in inbox?

Does anyone know if it is possible to use an action script in Drafts to automatically select the draft which has the oldest ‘modified’ date in the inbox?

My scenario is that after archiving a draft, I would like to automatically select the next ‘oldest’ draft (by modification date), and work through the drafts from oldest to newest.

Take a look at the Draft.query() function. It can sort by modified date, and you can specify the order. Populate the query parameters to search the inbox too, and you’ll get an array of Drafts with the first element as the next draft you want. You then can load that in using the editor.load() function.

Based on that, try this:

editor.load(Draft.query("", "inbox", [], [], "modified", false, false)[0]);

Thanks Stephen, that worked perfectly.

Best wishes,

Andy

Is there a way to also have the query only select a draft that does not have a tag? I’ve tried a couple of guesses on how to not select a tag. But the only query that works ends up finding the oldest draft (which is correct) but that draft is also tagged (incorrect)


Side note @agiletortoise : this action using the Draft.query will crash Drafts on macOS -

editor.load(Draft.query(“”, “inbox”, [null], [], “modified”, false, false)[0]);

Prefix a hyphen for tag omission.

Thanks for the link, it gave me a few more ideas, but none of them panned out.

I’m trying to create an action that would find the oldest Draft that has no tags so I can review for updates or trash it. The I have so far action works like I want, except for the finding the draft note I want.

Side note: trying to use regular expressions in Drafts for macOS also crashed the app:

editor.load(Draft.query(“”, “inbox”, [/^(?![\s\S])/], [], “modified”, false, false)[0]);

So not have any tags rather than not have a tag. For that you want to use “untagged” like in workspaces.

editor.load(Draft.query("", "inbox", ["untagged"], [], "modified", false, false)[0]);

Would load the oldest modified draft in your inbox that has no tags.

Perfect! Thanks for the help on this. :beers: