Populate a Daily Habits List

I have a list of a few daily habits that I want to maintain. Updating this manually every day in TaskPaper was repetitive and error-prone, so I wrote a script that will populate the children of a dedicated “habits” project with my daily tasks. It will replace any entries under the project with the full list of habits, so each day I start with the whole list of habits by running this command.

I’m not sure if this is too niche of an enhancement, but in case it’s useful to others, here it is. You can customize the name of your habits project and the specific habits you want by adjusting the variables at the top of the script. A future enhancement might pull the habits list from a template file on your Mac instead of hardcoding it into the script.

Here’s the script:

const PARENT_PROJECT = "Daily Goals";

const HABITS = [
  "Meditate",
  "Exercise",
  "Language Practice",
];

function loadHabits(editor, options) {
  'use strict';

  const outline = editor.outline;

  outline.groupUndoAndChanges(function() {
    let projectMatches = outline.evaluateItemPath(`project ${options.parent_project}:`);

    if( projectMatches.length == 0 ) {
      // habits project doesn't exist, so create it
      outline.root.insertChildrenBefore(outline.createItem(`${options.parent_project}:`), outline.root.firstChild);
      projectMatches = outline.evaluateItemPath(`project ${options.parent_project}:`);
    }

    const dailyProject = projectMatches[0];
    dailyProject.removeChildren(dailyProject.children);

    dailyProject.appendChildren(
      options.habits.map(habit => outline.createItem(`- ${habit} @today`))
    );
  });
}

Application("TaskPaper").documents[0].evaluate({
  script: loadHabits.toString(),
  withOptions: {parent_project: PARENT_PROJECT, habits: HABITS}
})
2 Likes

First of all, I love the script.

Maybe it could be a good idea to add a function that checks to see if that parent project is there and if it is not, then creates it. Just in case someone tries to run it and they don’t happen to have that parent project already there. Otherwise they may not understand why the script is failing.

Just a suggestion to improve the script with those that happen to find something like this useful.

This Script has been added to wiki

Really glad you like it!

That’s a great suggestion! I just updated the script to do that.

I use Typinator, but TextExpander could also be used.