I am trying to write a script that locates a draft with a specific title and tag.
Draft.queryByTitle returns an array of all drafts with the correct title, but doesn’t give me an option to limit by tag. Draft.query allows for tag searches, but doesn’t have a search by title option (I don’t think?).
I think using the javascript filter() method on the Draft.queryByTitle array could work, but I can’t figure out a way to filter on tags (I have only the most basic understanding of javascript). And that seems like perhaps it is overkill for what seems like a pretty simple query?
The use case for all this is to append something to a Daily Note which has the day’s date as its title and the tag “daily”. I have other drafts with the date in the title, so I need to filter by the “daily” tag to select the correct draft.
1 Like
The Draft.query
search parameter supports all the same advanced queries that searching in the app does, like title:
to restrict queries to only title matches. You probably want something like:
// find drafts with tag "blue" and exact phrase "My Title" in title line...
let drafts = Draft.query("title:\"My Title\"", "inbox", ["blue"])
Ah, great, thank you! I had actually tried that, but I must have made a syntax error in the “title:” portion. However, while I can make this work with a text string for the title as in the example above, how can I do this with a variable (variable is called “today”) instead of the “My Title” text string?
I assume something like Draft.query (“title:today”, “inbox”, [“blue”]) but that doesn’t work.
Is the use of advanced queries in scripts documented in the script reference somewhere? I haven’t seen it.
You need to construct a string that contains the value of your variable. "title:today"
will just search for the string “today” in the title. Several ways to do that in JavaScript, but most simply by concatenation, like:
let today = "TODAY-VALUE"; // however you are creating your today value
let q = "title:" + today;
// q now equals "title:TODAY-VALUE"
Success! Thank you! Incredible support as always.