Script: Import OPML (or BML) files into TaskPaper 3


#1

Shows a file chooser dialog, and should be able to import several files at once.

For installation and use, see:

https://guide.taskpaper.com/customizing-taskpaper/creating-scripts.html

// Import OPML or BML outlines into TaskPaper 3
// Rough draft 0.02

(function () {
    'use strict';

    // TASKPAPER CONTEXT

    function importFile(editor, options) {
        var outline = editor.outline,
            mbType = options.type;

        outline.groupUndoAndChanges(function () {
            outline.root.appendChildren(
                ItemSerializer
                .deserializeItems(
                    options.content,
                    outline,
                    ItemSerializer[
                        (mbType ? mbType.toUpperCase() : 'TEXT') + 'MimeType'
                    ]
                )
            )
        });
    }

    // JAVASCRIPT FOR AUTOMATION CONTEXT

    // readFile :: FilePath -> maybe String
    function readFile(strPath) {
        var error = $(),
            str = ObjC.unwrap(
                $.NSString.stringWithContentsOfFileEncodingError(
                    $(strPath)
                    .stringByStandardizingPath,
                    $.NSUTF8StringEncoding,
                    error
                )
            );

        return error.code || str;
    }

    // fileExtension :: String -> maybe String
    function fileExtension(strPath) {
        var lstParts = strPath.split('.');

        return lstParts.length > 1 ? (
            lstParts.pop().toLowerCase()
        ) : undefined;
    }

    var a = Application('System Events'),
        sa = (a.includeStandardAdditions = true, a.activate(), a),

        lstChosen = sa.chooseFile({
            withPrompt: 'Choose OPML or BML file(s) to import to TaskPaper 3',
            ofType: ['public.text'],
            multipleSelectionsAllowed: true,
        }),

        tp3 = Application('Taskpaper'),
        ds = tp3.documents;

    return lstChosen.map(function (oPath) {
        var d = (ds.push(tp3.Document()), ds[0]),
            strPath = oPath.toString(),
            lstKnown = ['opml', 'bml'],
            iExtn = lstKnown.indexOf(fileExtension(strPath)),
            strType = (iExtn !== -1 ? (
                lstKnown[iExtn ? 1 : 0] // ftml = bml
            ) : undefined);

        return d.evaluate({
            script: importFile.toString(),
            withOptions: {
                type: strType,
                content: readFile(strPath)
            }
        });
    });
})();


#2

Awesome - thanks!


#3

The link in the OP is 404


#4

Thanks ! Changed above to the new url:
https://guide.taskpaper.com/customizing-taskpaper/creating-scripts.html


#5

Thanks.

Error on run (after selecting OPML file):

Line 62

tp3 = Application('com.hogbaysoftware.TaskPaper3'),


#6

Should be like the other scripts that doesn’t work because of the change in the application id name… Not tested, but just changing the name to ‘TaskPaper’ should do it:
tp3 = Application('TaskPaper')


#7

Thanks ! Updated above to

Application('Taskpaper')

#8

I tried to import an opml that was created by exporting from Omnioutliner. The script allows me to open to this file, but then I get a result of [undefined]. I don’t see myfilename.taskpaper anywhere, so I’m guessing that something went wrong. How can I troubleshoot this?

Edit: nevermind, it created a new Taskpaper window with the content in it, but the conversion seems a bit iffy. Not much better than copying and pasting the contents.


#9

My file wasn’t very large so I ended up opening it in my text editor and adding the markup there (mostly : for the sections and the - for the items). The import script was not adding these, but I did only try the one file.