Hey all, I was hoping I could get some critique on a script. This is my first attempt at scripting TaskPaper with JavaScript so I’m mostly curious if anything here could be more idiomatic or failure-tolerant.
Goal: the script archives “done” tasks to an Archive project, but only if that task is a “top-level” task. This is maybe an overly simplistic definition of top-level, but I’m assuming any task with a depth of 2 is a child of a project, and not a child of a project-less task. (This script lets me archive without disconnecting children from their parent tasks.)
var TaskPaper = Application('TaskPaper');
function fn(editor, options) {
var outline = editor.outline;
var archive = outline.evaluateItemPath("//Archive:")[0];
outline.groupUndoAndChanges(() => {
if (typeof archive === "undefined") {
archive = outline.createItem("Archive:");
outline.root.appendChildren(archive);
}
var topLevelItems = outline.evaluateItemPath("@done except Archive//*").filter(i => i.depth === 2)
topLevelItems.map(item => {
item.removeFromParent();
archive.appendChildren(item)
});
});
}
TaskPaper.documents[0].evaluate({
script: fn.toString()
})
Thank you!