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

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)
            }
        });
    });
})();

Awesome - thanks!

The link in the OP is 404

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

Thanks.

Error on run (after selecting OPML file):

Line 62

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

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

1 Like

Thanks ! Updated above to

Application('Taskpaper')

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.

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.

Hi, I have the latest version of TaskPaper (3.8.17) and this script doesn’t seem to work. Has anything changed?
Or how can I import OPML files?
I use XMind 8 as mind map maker and export in OPML.

I follow the steps described here and in the official Wiki on how to implement the Scripts. When I try to test the Script from “Srcript Editor App”, it doesn’t let me select the OPML file.

MacOS version: Monterey 12.3

Assuming (always the first things to check) that you have:

  1. Copied the whole of the script, scrolling right down to the final })();, and
  2. set the language selector to JavaScript (at top left of Script Editor)

then I would expect it to be working.

(It is here)

Could you expand a bit on :

"doesn’t let me select the OPML file"

Here I am able, for example, to select the file with an .opml extension and import it as TaskPaper.

1 Like

Hello again, thanks for your quick reply.

I have checked that the script is well copied to the end. When I click on run it opens the window to select the file, but the OPML file is not allowed to be selected.

I attach a screenshot.

Thanks – that’s helpful.

I’m running an earlier version of macOS here, and I wonder whether public.text is a mismatch for opml files, either in the context of Monterey, or in the context of the way that XMind are writing out their opml files.

What I suggest is that at the stage of the script where you see:

ofType: ['public.text']

that you try adjusting it either to:

ofType: ['public.text', 'public.xml']

(allowing for the possibility that the system considers those files to be XML)

or

// ofType: ['public.text']

in other words, commenting out the type restriction entirely (preceding it with a // JavaScript comment prefix) so that any file could be selected.

If you still get no joy, perhaps you could zip up and post here an anonymised sample opml file from the XMind app that you are using ?

Ok, I have done what you say.
I have commented on the line of ofType: ['public.text'] and now I can choose the file.

I’m sending you a screenshot of it, because for some reason it doesn’t recognise the nodes as projects.
I also attach my OPML file.

OPML file:
Tareas HogarAbitat.opml.zip (1.2 KB)

Thanks – the opml generated by XMind encodes nesting structure (parent ⇄ child connection), but doesn’t encode anything corresponding to TaskPaper’s project ⇄ task ⇄ note distinctions.

1 Like

XMind has the following export files:

txt, html, doc, pdf, odt, rtf, opml, mpp, svg, ppt, odp, xls, csv, ods

Do you think I can effectively try one of these other formats?
Or once it is already imported as OPML and does not recognise the nodes, what should I do to convert it into the TaskPaper structure?

I have added : (colon) to the main Nodes and - (dash) to the tasks, directly in Xmind.
When importing again it recognises them.

@complexpoint I appreciate your help and the time you took to answer my questions.

Best regards.

1 Like