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?).
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.
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.
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.