Refer to draft via queryByTitle

I need help on an action. I want to insert text at a specified line in a certain draft. I have annual, journal-like drafts with a title + current year (“My Journal 2023”). Everything works fine, but I cannot figure out how refer to the draft.

let query = "Title " + year;
let mydraft = Draft.queryByTitle(query);
mydraft.insert(text, 1)

Is this even possible?

Thank you!

I think you want to automate inserting a wiki-style link to this other draft with a known name, like [[My Journal 2023]]…is that right?

To do that, you would not need to query for the other draft, just build the text for the known name, something like:

// this would put "[[My Journal 2023]]" in `text` variable
let text = "[[My Journal " + new Date().getFullYear() + "]]"

As far as inserting that at a specific line number in the draft, that would require some amount of smarts to do reliably. Are you working with existing drafts that you know always have content?

Hey, thanks for your quick response!

No, I do not want to insert a link, but I want to add text to “My Journal”. The “text” variable is actually a list of the current week, that part of the action works fine. On a weekly basis, I want to add the current week on top of my journal draft. When I replace mydraft.insert(text, 1) with draft.insert(text, 1), it does exactly what I want, but only if the journal-draft is open. If I refer to the journal-draft via UUID, I would have to adapt the script every year. So I wondered if there is another solution. Does this make it clearer?

Yes, sure.

Your main problem, then, is that queryByTitle does not return a draft, but an array of drafts, because you could easily have more than one with a matching title. If you trust that you are only going to find one, you can just grab the first draft in the array and address it, like:

let mydraft = Draft.queryByTitle(query)[0]

Thank you, I didn’t see that (I’m not a programmer). Unfortunately, it still doesn’t work. I think “mydraft” is correctly addressed now, but the following mydraft.insert(text, 1) doesn’t do anything. I tried

draft.insert(mydraft.displayTitle, 1)

just for testing, and this works correctly, duplicates the title line, but

mydraft.insert(mydraft.displayTitle, 1)

doesn’t do anything. Maybe I am still misunderstanding something?

Could you post your full script? Hard to debug snippets.

Gladly, even though there is some German words and copy/pasted code in it :grinning:

var heute = new Date();
var dd = String(heute.getDate()).padStart(2, '0');
var mm = String(heute.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = heute.getFullYear();

const wochentag = new Array("Mo", "Di", "Mi", "Do", "Fr", "Sa", "So")
const woche = Array.from(Array(7).keys()).map((idx) => {
    const d = new Date(); 
    d.setDate(d.getDate() - d.getDay() + idx + 1); 
    return String(d.getDate()).padStart(2, '0') + '.' + mm + '.';

function zuordnung() {
    return wochentag.flatMap(function (val, i) {
      if (woche[i]) {
        return [val + ' ' + woche[i] + ' - '];
      return [val];

let liste = '\n' + zuordnung(wochentag, woche).join('\n');

let query = "Journal culinaire " + yyyy;
let journal = Draft.queryByTitle(query)[0];

// draft.insert(liste, 1) // works
// draft.insert(journal.displayTitle, 1) // works
journal.insert(liste, 1) // doen't work

It probably does work, but you would need to call journal.update() at the end of your script to save changes or they would be discarded at the end of the action.

Wow. Amazing, thank you for your help, I really appreciate that! By the way: This action is part of my recipe organization toolkit, I have several actions and workspaces. I thought about sharing it here, but, as you can see, I am more of a hobby programmer (cook better than I code). Then, this is all German, templates, tags etc. Anyway, thanks again, you are doing an amazing job!

Thanks, glad I could help! Sounds like a cool workflow.