Skip to content

Instantly share code, notes, and snippets.

@FeralFlora
Last active February 28, 2025 13:32
Show Gist options
  • Save FeralFlora/48816a3536f5dce092017ba8b8f6ac76 to your computer and use it in GitHub Desktop.
Save FeralFlora/48816a3536f5dce092017ba8b8f6ac76 to your computer and use it in GitHub Desktop.
Function for Obsidian to return tasks with GTD tags like #todo/next-action, then #todo/inbox if no tasks were found with the first tag, and so on.

Takes a specific tag as a parameter, so you can find tasks with any tag. Requires Dataview to work. Returns the dataview results, which can then be passed to a Templater suggester and then toggleTask.js or insertBlockEmbed.js.

async function nextActions(userTag = null) {
let actionTags = ["todo/next-action", "todo/inbox", "todo/someday-maybe"]
// const exclusion = "";
let nextActions;
let chosenTag = "";
if (userTag) {
actionTags.unshift(userTag);
};
for (const tag of actionTags) {
nextActions = await runQuery(tag);
if (nextActions.length > 0) {
chosenTag = tag;
if (userTag && chosenTag !== userTag) {
new Notice(`Found no tasks tagged ${userTag}, suggesting task from ${chosenTag} instead`, 5000);
}
new Notice(`Found ${nextActions.length} tasks tagged ${tag}`, 5000);
break;
}
};
return [nextActions, chosenTag] || null;
};
async function runQuery(actionTag) {
const dv = app.plugins.plugins["dataview"].api;
const path = '""';
const where = `where !completed and contains(tags, "${actionTag}")`;
const query = `TASK from ${path} ${where} sort due desc`;
const DQL = await dv.tryQuery(query);
const results = DQL.values;
return results;
}
module.exports = nextActions;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment