Skip to main content

Defer TaskPaper Tasks One Day with JavaScript

Published 3 months ago 1 min read

Scripting TaskPaper for fun and profit. Stoic Productivity scripting at its finest.

Since I've been experimenting with using TaskPaper again instead of Things, I've starting scripting TaskPaper a bit. Here's a fun little script that will defer @start(..) tags by one day. If the tag is tagged with @today, it'll remove the @today tag and replace it with a @start tag for the following day.

var TaskPaper = Application("TaskPaper");

function TPContextGetCurrentPath(editor, options) {
  var selection = editor.selection;
  var selectedItems = selection.selectedItems;

  editor.outline.groupUndoAndChanges(function () {
    selectedItems.forEach(function (each) {
      var attributeName = "data-" + options.tag;
      var startDate = each.getAttribute(attributeName);

      if (startDate) {
        each.setAttribute(
          attributeName,
          DateTime.format(startDate + options.offset)
        );
      } else {
        each.setAttribute(
          attributeName,
          DateTime.format("today " + options.offset)
        );
        if (each.hasAttribute("data-today")) each.removeAttribute("data-today");
      }
    });
  });

  editor.moveSelectionToItems(selection);
}

var path = TaskPaper.documents[0].evaluate({
  script: TPContextGetCurrentPath.toString(),
  withOptions: {
    tag: "start",
    offset: "+1d",
  },
});