Script Error in TaskPaper 3

You can still run AppleScripts, but they will need a bit of rewriting.

See, for example, Scripting alternatives for TP3: AppleScript and JavaScript for Applications

and Help > User’s Guide.

Looking, for example, at the script above for moving the @today entries of the selected project up to the front, you could experiment with:

  • pasting the following script into Script Editor,
  • setting the language tab at top left to JavaScript,
  • placing your cursor somewhere in the tasks of the target project in TaskPaper 3 Preview,

and running the script
(check what it does carefully – it’s just a purely illustrative first draft based on my quick reading of what the original is doing)

function run() {

    function taskPaperScript(editor, options) {

        // PROJECT OF SELECTION ?

        var ps = editor.selection.startItem
            .evaluateItemPath(
                'ancestor-or-self::@type=project[-1]'
            ),
            project = ps.length ? ps[0] : undefined,

            // @TODAY TASKS OF PROJECT ?

            todayTasks = project ? project
            .evaluateItemPath(
                '//@today'
            ) : [];

        // OUTLINE EDIT BRINGING @TODAY TO FRONT ?
        if (todayTasks.length > 0) {
            var outline = editor.outline;
      
            outline.groupUndoAndChanges(function () {
                outline.insertItemsBefore(
                    todayTasks, project.firstChild
                );
            });
        }
    }

    // EVALUATE taskPaperScript IN FRONT TP3 DOCUMENT
    var ds = Application("TaskPaper")
        .documents,
        varResult = ds.length ? ds[0].evaluate({
            script: taskPaperScript.toString(),
        }) : false;
}

TaskPaper 3 to Day One (ver 1 or 2)

The script below translates Brett Terpstra’s AppleScript version, aiming to:

  • Move @done items to an Archive project
  • Check that they have @project(strProjectName) tag
  • Remove tags considered to be line noise for archival purposes
  • Output an archivable string suitable for Day One
  • Send that string to Day One through the Day One CLI tool (which needs to be installed).
// VER 0.03
// A JavaScript for Applications translation of an orginal script by Brett Terpstra
// http://brettterpstra.com/2012/02/23/log-taskpaper-archives-to-day-one/
// For TaskPaper 3, and DayOne version 1 or 2

(function (intDayOneVersion) { // 1 or 2
    'use strict';

    // TASKPAPER 3 EVALUATION CONTEXT:

    function fn(editor, options) {

        // Task with some of its tags removed
        function tagsPruned(oTask, lstNoiseTags) {
            return lstNoiseTags.reduce(function (t, strTag) {
                var strAttrib = 'data-' + strTag;

                if (t.hasAttribute(strAttrib)) {
                    t.removeAttribute(strAttrib);
                }
                return t;
            }, oTask);
        }

        // Task with a @project(strProjName) tag added (if not found)
        function projectTagged(oTask) {
            if (!oTask.hasAttribute('data-project')) {
                var project = taskProject(oTask),
                    strProj = project ? (
                        project.bodyString.slice(0, -1)
                        .trim()
                    ) : '';

                if (strProj) oTask.setAttribute('data-project', strProj);
            }
            return oTask;
        }

        // Immediately enclosing project of the task
        function taskProject(oTask) {
            var p = oTask,
                blnFound = false;
            while (!(blnFound = (p.getAttribute('data-type') === 'project'))) {
                p = p.parent;
            }
            return blnFound ? p : undefined;
        }


        // ARCHIVE AND LOG

        var outline = editor.outline,

            // EXISTING OR NEW ARCHIVE PROJECT ?
            as = editor.outline.evaluateItemPath(
                '/(Archive and @type=project)[-1]'
            ),
            archive = as.length > 0 ? as[0] : (function () {
                var newArchive = outline.createItem('Archive:');

                outline.groupUndoAndChanges(function () {
                    outline.root.appendChildren(
                        newArchive
                    );
                });
                return newArchive;
            })(),

            // COMPLETED BUT UNARCHIVED TASKS ?
            tasks = outline.evaluateItemPath('@done except /Archive//*');


        // ARCHIVED AND LISTED
        if (tasks.length > 0) {
            var lstNoise = options.tagsToStrip;

            // project-tagged and noise-tag-pruned
            outline.groupUndoAndChanges(function () {
                outline.insertItemsBefore(
                    tasks.map(function (x) {
                        return projectTagged(tagsPruned(x,
                            lstNoise));
                    }), archive.firstChild
                );
            });

            // loggable string
            return tasks.map(function (t) {
                    return t.bodyString;
                })
                .join('\n');
        } else return '';
    }

    function quotedForm(s) {
        return "'" + s.replace("'", "'\\''") + "'";
    }


    // JS FOR APPLICATIONS EVALUATION CONTEXT:

    var blnFileNameAsHeader = true,

        ds = Application("TaskPaper")
        .documents,
        d = ds.length ? ds[0] : undefined,

        strHeader = blnFileNameAsHeader && d ? '## ' + d.name() + '\n\n' :
        '',

        strArchive = d ? d.evaluate({
            script: fn.toString(),
            withOptions: {
                tagsToStrip: ['na', 'next', 'priority', 'waiting'],
                fileNameAsHeader: true
            }
        }) : '',
        strLog = strArchive ? (
            strHeader + strArchive
        ) : '';


    // Submit the string to Day One through the command line tool at:
    //      http://dayoneapp.com/faq/#commandlineinterface
    // Day One Ver 1 may need:
    // Run `ln -s "/Applications/Day One/Day One.app/Contents/MacOS/dayone" /usr/local/bin/dayone` 
    // to make it available on that path

    // Day One Ver 2:
    // -- The CLI may initially generates error messages complaining of the lack of a default location.
    // You may need to run:
    // $ alias dayone='/usr/local/bin/dayone -j=\"~/Library/Group\ Containers/5U8NS4GX82.dayoneapp2/Data/Auto\ Import/Default\ Journal.dayone\"'


    if (strLog) {
        var a = Application.currentApplication();

        (a.includeStandardAdditions = true, a)
        .doShellScript(
            'echo ' + quotedForm(strLog) + ' | ' +
            '/usr/local/bin/dayone' + (intDayOneVersion > 1 ? (
                ' -j="~/Library/Group\ Containers/5U8NS4GX82.dayoneapp2/Data/Auto\ Import/Default\ Journal.dayone"'
            ) : '') + ' new'
        );
    }

    return strLog;

})(
    parseInt( // or simply replace these four lines with 1 or 2 (Day One version)
        Application("Day One")
        .version()
        .charAt(0), 10
    )
)
2 Likes