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