Script: Export to OPML or BML (HTML outline)


#1

An export script for TaskPaper 3 which uses the built-in ItemSerializer object.

To use the script, adjust the option at the end (‘opml’ or ‘bml’) and install and run as in:

http://guide.taskpaper.com/using_scripts.html

(function (dctOptions) {
    'use strict';

    // TASKPAPER CONTEXT

    function serialized(editor, options) {
        return ItemSerializer.serializeItems(
            editor.outline.items,
            editor,
            ItemSerializer[
                options.format.toUpperCase() + 'MimeType'
            ]
        );
    }

    // JAVASCRIPT FOR AUTOMATION CONTEXT
    // writeFile :: FilePath -> String -> IO ()
    function writeFile(strPath, strText) {
        $.NSString.alloc.initWithUTF8String(strText)
            .writeToFileAtomicallyEncodingError(
                $(strPath)
                .stringByStandardizingPath, false,
                $.NSUTF8StringEncoding, null
            );
    }

    function exportFromTP3(docTP3, strPath, strFormat) {
        var strSerial = docTP3.evaluate({
            script: serialized.toString(),
            withOptions: {
                format: strFormat
            }
        });

        return (
            writeFile(strPath, strSerial),
            strSerial
        );
    }

    var tp3 = Application('com.hogbaysoftware.TaskPaper3'),
        ds = tp3.documents;

    if (ds.length) {
        var d = ds[0],
            a = Application('System Events'),
            sa = (a.includeStandardAdditions = true, a),

            pathFile = d.file(),
            lstParts = pathFile ? pathFile.toString()
            .split('/') : undefined,

            strFolder = lstParts ? (
                lstParts.slice(0, -1)
                .join('/') + '/'
            ) : undefined,

            strFormat = dctOptions.format.toLowerCase(),
            lstSupported = ['opml', 'bml', 'text'],
            strKnownFormat = (
                lstSupported
                .indexOf(strFormat) !== -1 ? strFormat : undefined
            );

        if (strKnownFormat) {
            sa.activate();
            var pathWrite = sa.chooseFileName({
                withPrompt: 'Save as ' + strKnownFormat.toUpperCase(),
                defaultName: strFolder ? (
                    lstParts.slice(-1)[0].split('.')[0] + '.' +
                    strKnownFormat
                ) : 'Untitled.' + strKnownFormat,
                defaultLocation: strFolder ? (
                    Path(strFolder)
                ) : sa.pathTo('desktop')
            });

            return pathWrite ? (
                exportFromTP3(d, pathWrite.toString(), strKnownFormat)
            ) : undefined;
        } else {
            return "Unknown format: '" + dctOptions.format + "'" +
                "\nExpected: ( '" + lstSupported.join("' | '") + "' )";
        };

    } else return undefined;

})({
    format: 'opml'
});

// 'bml' or 'BML'  (HTML outline e.g. FoldingText for Atom)
// 'opml' or 'OPML'


Export to HTML/RTF (for use with Google Doc)
#3

Does this script still work with TaskPaper 3.5? I have used it in the past and it worked extremely well. But I just tried it and the resultant file is not able to be opened.


#4

It looks as if the serializer built into the Preview build may only be outputting the BML outline format at the moment - for OPML you may need to use an earlier build, or the next release …

(In the meanwhile, if you do need to generate OPML from the current build, it is possible that an amended version of the script, with this alternative version of the serialized() function at the top - could be worth experimenting with:

    function serialized(editor, options) {
        return ItemSerializer.serializeItems(
            editor.outline.items,
            "text/opml+xml"
        );
    }

#5

Thanks for the update. I exported about a dozen files with the alternate version of serialized() and they all worked fine except for one. It exported correctly, but would not stop running. All I had to do though was quit Script Editor.

Either way everything went well. Thanks for the help.


#6

I hate to keep asking about the script, but I tried to run it and it stated that on Line 38: Application can’t be found. I am using the 3.5 update so I am guessing the name got changed?

Does anyone know what it should be changed to?


#7

Change line

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

to:

var tp3 = Application('TaskPaper'),

Here’s the full script:

(function (dctOptions) {
    'use strict';

    // TASKPAPER CONTEXT

    function serialized(editor, options) {
        return ItemSerializer.serializeItems(
            editor.outline.items,
            editor,
            ItemSerializer[
                options.format.toUpperCase() + 'MimeType'
            ]
        );
    }

    // JAVASCRIPT FOR AUTOMATION CONTEXT
    // writeFile :: FilePath -> String -> IO ()
    function writeFile(strPath, strText) {
        $.NSString.alloc.initWithUTF8String(strText)
            .writeToFileAtomicallyEncodingError(
                $(strPath)
                .stringByStandardizingPath, true,
                $.NSUTF8StringEncoding, null
            );
    }

    function exportFromTP3(docTP3, strPath, strFormat) {
        var strSerial = docTP3.evaluate({
            script: serialized.toString(),
            withOptions: {
                format: strFormat
            }
        });

        return (
            writeFile(strPath, strSerial),
            strSerial
        );
    }

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

    if (ds.length) {
        var d = ds[0],
            a = Application('System Events'),
            sa = (a.includeStandardAdditions = true, a),

            pathFile = d.file(),
            lstParts = pathFile ? pathFile.toString()
            .split('/') : undefined,

            strFolder = lstParts ? (
                lstParts.slice(0, -1)
                .join('/') + '/'
            ) : undefined,

            strFormat = dctOptions.format.toLowerCase(),
            lstSupported = ['opml', 'bml', 'text'],
            strKnownFormat = (
                lstSupported
                .indexOf(strFormat) !== -1 ? strFormat : undefined
            );

        if (strKnownFormat) {
            sa.activate();
            var pathWrite = sa.chooseFileName({
                withPrompt: 'Save as ' + strKnownFormat.toUpperCase(),
                defaultName: strFolder ? (
                    lstParts.slice(-1)[0].split('.')[0] + '.' +
                    strKnownFormat
                ) : 'Untitled.' + strKnownFormat,
                defaultLocation: strFolder ? (
                    Path(strFolder)
                ) : sa.pathTo('desktop')
            });

            return pathWrite ? (
                exportFromTP3(d, pathWrite.toString(), strKnownFormat)
            ) : undefined;
        } else {
            return "Unknown format: '" + dctOptions.format + "'" +
                "\nExpected: ( '" + lstSupported.join("' | '") + "' )";
        };

    } else return undefined;

})({
    format: 'opml'
});

// 'bml' or 'BML'  (HTML outline e.g. FoldingText for Atom)
// 'opml' or 'OPML'

#8

Thanks for the extremely quick reply! This worked flawlessly.

As you can see, I use this all the time and it has been indispensable as I move files to DevonThink. I appreciate your work on this.

Thanks again


#9

FWIW the app id has now changed to com.hogbaysoftware.TaskPaper3.direct

but clearly more prudent to keep to the more generic Application('TaskPaper'), except in code which specifically wants to check which version it is running in.


#10

The link is 404


#11

Thanks:

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