QuickAdd and Tasks : How to insert tags in the tasks line modal?

What I’m trying to do

Hello !
I would like to create a task using Quickadd capture and the Tasks modal form via the API, and within the name of the task directly implement specifics tags (#Pro #Perso).
For which reason ? Because for each of those tags, I bind a custom hotkey to a QuickAdd command running with a dedicated capture format. And that’s where I got struggled, after roaming everywhere I don’t find a way to write the proper code to run that. Each time I have tried something either the tags appear before the checkbox making it deactivated either after the Tasks dates but unlikely disabling some dataview queries like “due next week”.

I wish I could have it like that:

  • New task #Todo #Pro :heavy_plus_sign: 2024-12-07

Things I have tried

Here is the code I pasted in QuickAdd into the capture format section, and trying to use the JS replace() method:

const tasksApi = this.app.plugins.plugins['obsidian-tasks-plugin'].apiV1;
let taskLine = await tasksApi.createTaskLineModal();
const tags = "#Todo #Perso";
let result = taskLine.replace(`${taskLine}`, `${tags}${taskLine}`);
return result

If anyone has any suggestions, that would be very helpful. thanks !

I can barely decipher any javascript, so use with caution, but this seems to work for me:

```js quickadd
const theTag = " #📖 ";

const task = await this.app.plugins.plugins['obsidian-tasks-plugin'].apiV1.createTaskLineModal();

// Create a regular expression to match any of the specified characters
const insertBeforeCharacters = /[🔺⏫🔼🔽⏬🛫➕⏳📅✅❌🔁🏁⛔🆔]/;

// Insert the tag before the first occurrence of any insert character
const updatedTask = task.replace(insertBeforeCharacters, `${theTag}$&`);

return updatedTask;

Better:

const Suffix = "#📖";
const Prefix = "📖 ";

let theTask = await this.app.plugins.plugins['obsidian-tasks-plugin'].apiV1.createTaskLineModal();

// Insert Suffix before specified regex characters
theTask = theTask.replace(/[🔺⏫🔼🔽⏬🛫➕⏳📅✅❌🔁🏁⛔🆔]/, ` ${Suffix} $&`);

// Insert Prefix after "- [<character>]" pattern
theTask = theTask.replace(/\- \[.\] /, `$& ${Prefix}`);

// Append Suffix at the end of the line if no insert characters were found
if (!theTask.includes(Suffix)) {
  theTask = theTask.replace(/$/, ` ${Suffix}`);
}

return theTask;
  • Allows to choose whether to put the tags or text at the beginning of the task line or after
  • FIX: adds tags when no task emoji are assigned

Then perhaps you should disclose where you got this code from.

Thanks for being helpful, but if you’re using an LLM, please leave the answer to others. By all means use it yourself to help learn.

I got it by a mix of:

  1. reading plug-in docs, (and forum/github posts, including this one)
  2. trying to write it
  3. asking AI to assess my attempt/ snippet of an attempt
  4. testing it,
  5. adjusting it,
  6. and try, try all of the above again until it worked, as best as I can tell.
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.