Hi,
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)
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?
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?
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:
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?
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!