Script Error in TaskPaper 3


#1

Hi,
I happily installed the preview version and assumed that the old TaskPaper Scripts would still run. However, with every AppleScript that I have, I get a Script Error with the following Message:

Error Number: -1
Unknown error.

Any idea what went wrong, and what I need to do to fix that?


#2

TaskPaper 3 has a new scripting interface.

If you show us the source code of the TP2 script(s) that failed, we may be able to adapt it/them.

A couple of other relevant threads:



#3

Thank you for your response.

You say that TaskPaper 3 has a new scripting interface. Does this mean, that AppleScripts are not working anymore? Or is there a workaround?

In my case, all my AppleScripts fail. For example, MoveTodayEntriesToFrontOfList [1] or DayOneLogging [2].

I appreciate your help there.

[1] http://www.hogbaysoftware.com/wiki/MoveTodayEntriesToFrontOfList
[2] http://brettterpstra.com/2012/02/23/log-taskpaper-archives-to-day-one/


#4

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
    )
)

#5

Updated the DayOne script above to work with either Day One v1 or v2

Note that:

  1. The DayOne Command Line Interface tool needs to have been installed
  2. You need to make an edit to the last line of the script, indicating which DayOne version you are using, as DY2 requires an extra option at the command line, which would confuse DY1.

(I understand that Brett is taking an XML file route to placing TaskPaper and other material in DayOne now, and probably won’t have time to get around to updating his original TaskPaper to DayOne script).


#6

Thanks a million! That helps me a lot. Plus, I understand now the new scripting approach better.

The issue I have left is the following: the scripts do run in the ScriptEditor but do not appear to work from the Script menu in the Mac menu bear. Any idea?


#7

run in the ScriptEditor but do not appear to work from the Script menu in the Mac menu bar

They seem to be working from the menu bar Scripts menu here, I:

  • Saved them in .scpt format from Script Editor ( Save As > File Format > Script)
  • Placed copies in the TaskPaper scripts folder
~/Library/Scripts/Applications/TaskPaper

( or, with TaskPaper as the front application, Script Menu > Open Scripts Folder > Open TaskPaper Scripts Folder)

and ran them, with TaskPaper active, from the Script menu.

Might it be possible that you first saved them in text (.applescript) format ?

If they are not running, it might be worth looking in /Applications/Utilities/Console.app to see if any error messages are added to the log there when you try to run them.


#8

Yes, that was the problem. I have to admit, I did not know that the script file format makes any difference.

Now, I saved it as .scpt and everything works.

Thank you for your swift responses!


#9

Good !

I was just about to suggest that if you still have TaskPaper 2 on your system, and the Script Menu context was confused by that, you could make the scripts more specific by replacing all instances of the string:

Application("TaskPaper")

with the alternative form:

Application("com.hogbaysoftware.TaskPaper3")

but it doesn’t sound as if you need to do that now.