I want to programmatically create tasks #3078
-
tldr: I want Dataview to be able to create tasks on my behalf. I have one meeting note per person. I want to automatically create a task to review that week's notes, due two days after that note was modified. I have a dataviewJS script which will find all notes that were recently modified. const startOfWeek = moment().startOf('week');
const endOfWeek = moment().endOf('week');
// Define an array of allowed paths
const disallowedPaths = ['Weekly Notes', 'Obsidian Templates', 'tmp', 'tasks']; // Specify your disallowed paths here
// Retrieve all markdown files
const files = app.vault.getMarkdownFiles();
// Function to check if file path is in any of the allowed paths
function isInDisallowedPaths(filePath) {
return disallowedPaths.some(path => filePath.includes(path));
}
// Function to get file modification time
async function getFileModifiedTime(file) {
const fileStat = await app.vault.adapter.stat(file.path);
return moment(fileStat.mtime);
}
// Function to calculate due date
function getDueDate(modifiedTime) {
let dueDate = modifiedTime.clone().add(2, 'days'); // Add 4 days to modification time
// Check if due date falls on a weekend and adjust to next Monday if so
if (dueDate.day() === 6) { // Saturday
dueDate.add(2, 'days');
} else if (dueDate.day() === 0) { // Sunday
dueDate.add(1, 'days');
}
return dueDate;
}
// Filter files modified this week and in the allowed paths
const modifiedThisWeek = [];
for (const file of files) {
// Include only files in the allowed paths
if (isInDisallowedPaths(file.path)) {
continue;
}
const mtime = await getFileModifiedTime(file);
if (mtime.isBetween(startOfWeek, endOfWeek, null, '[]')) {
const dueDate = getDueDate(mtime); // Calculate due date
modifiedThisWeek.push({ file, mtime, dueDate });
}
}
// Sort files by modification time, ascending order
modifiedThisWeek.sort((a, b) => a.mtime - b.mtime);
// Create a task list
let taskList = '';
for (const { file, mtime, dueDate } of modifiedThisWeek) {
const fileName = file.basename;
const formattedDate = mtime.format('YYYY-MM-DD'); // Format the modified date as needed
const formattedDueDate = dueDate.format('YYYY-MM-DD'); // Format the due date as needed
taskList += `- [ ] Review [[${file.path}]] [due::${formattedDueDate}] [tag:: meetings]\n`;
}
// Display the task list
dv.el('div', "# Meeting Notes\n\n" + taskList); For instance, if I had a meeting with Bob on 2024-09-12, it would render the following:
Unfortunately, the tasks plugin does not recognize these tasks. Instead of merely rendering them, I'd love to be able to call |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Required behaviour
Understood.
Yes, your understanding is correct. Your code is telling dataview to display tasks, but it is not saving the tasks in the markdown, so there is no markdown content for other plugins to recognise and act upon. Editing markdown files programmaticallySo the question is really how to edit markdown files to add content. Dataview isn't really designed to write text to Markdown files, but the Templater plugin is designed to do exactly this. I recommend reviewing the Templater documentation. If you can't work out how to do what you need, there is a helpful community in the |
Beta Was this translation helpful? Give feedback.
-
There is one use in Tasks development code of the Templater plugin to write files: |
Beta Was this translation helpful? Give feedback.
-
You're correct in that I can solve this problem with Templater. Part of my "meeting note" template now includes a task for reviewing it. Thanks for the tip! |
Beta Was this translation helpful? Give feedback.
Required behaviour
Understood.
Yes, your understanding is correct.
Your code is telling dataview to display tasks, but it is not saving the tasks in the markdown, so there is no markdown content for other plugins to recognise and act upon.
Editing markdown files…