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 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;
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