Converting an AppleScript for repeating tasks to JavaScript

I’ve used TaskPaper 2 for years and I love the app. When I got TaskPaper 2, I taught myself some basics of AppleScript and wrote a very useful script for my workflow. Becuase TaskPaper 2 doesn’t work with the newest Mac software, I upgraded to TaskPaper 3. I just spent the past few hours figuring out how to customize TaskPaper 3’s StyleSheet, but I’m completely lost when it comes to converting my old AppleScript into JavaScript. Can someone write the JavaScript for me? I would be extremely grateful and other people might also benefit from it. The script is useful when one has a large project (e.g. writing a dissertation) and has to repeat the same tasks (e.g. writing 500 words) on a regular basis in order to accomplish it. Then, when the repeating task is complete, one can add an additional @done tag to the task to delete it when the script is run.

Here’s the AppleScript:

tell application "TaskPaper"
tell front document
set selected_entries to get entries
repeat with each in selected_entries
if (exists tag named “repeat” of each) and (exists tag named “done” of each) then
delete (tag named “today” of each)
end if
end repeat
end tell
end tell

tell application "TaskPaper"
tell front document
set selected_entries to get entries
repeat with each in selected_entries
if (exists tag named “repeat” of each) and (exists tag named “done” of each) then
delete (tag named “done” of each)
end if
end repeat
end tell
end tell

tell application "TaskPaper"
tell front document
set selected_entries to get entries
repeat with each in selected_entries
if (exists tag named “done” of each) then
delete each
end if
end repeat
end tell
end tell

Here is a rough draft for you to test with dummy data.

The deletion of @done items is disabled by default - you would need to edit false to true at the bottom of the script, if you are sure that you really prefer deletion to archiving.

If this doesn’t yet do what you need, then it can readily be adjusted.

For the mechanics of installation and use, see:
https://guide.taskpaper.com/using-taskpaper/using-scripts.html

(function (options) {
    'use strict';

    // TASKPAPER CONTEXT

    function TaskPaperContext(editor, options) {
        var outline = editor.outline,
            blnZapDone = options.deleteDone;

        var lstPossibleDeletions = [];

        outline.groupUndoAndChanges(function () {
            outline.items.forEach(function (item) {

                if (item.hasAttribute('data-done')) {
                    if (item.hasAttribute('data-repeat')) {
                        item.removeAttribute('data-today');
                        item.removeAttribute('data-done');
                    } else {
                        if (blnZapDone) {
                            // Might an archive script be better ?
                            // item deletion is disabled by default
                            // at the foot of this script ...
                            // (set deleteDone: to true if you are sure)
                            //
                            // UNDO is CMD Z

                            // OUCH ...
                            lstPossibleDeletions.push(item);
                        }
                    }
                }

            });

            if (blnZapDone && lstPossibleDeletions.length > 0) {
                outline.removeItems(
                    lstPossibleDeletions
                );
            }
        });

    }

    // JAVASCRIPT FOR AUTOMATION CONTEXT

    var tp3 = Application('TaskPaper'),
        ds = tp3.documents;

    return ds.length ? ds[0].evaluate({
        script: TaskPaperContext.toString(),
        withOptions: options
    }) : undefined;

})({
    deleteDone: false
});

Thank you so much for your help! I’ve tested it and it works perfectly! I think that it’s everything that I need at this point. You made my day. :smile:

2 Likes