×

TaskPaper: Importing/Exporting

TaskPaper is a plain text to-do list creation and editing application used to define projects and their tasks. It’s simple text-format is supported by many personal management applications such as OmniFocus, and is often used as a transfer format between various applications.

TaskPaper support in OmniFocus is detailed in this TaskPaper Reference.

The following example scripts and plug-ins use the built-in URL support in OmniFocus to import and export TaskPaper data to and from your OmniFocus database.

Import Chosen TaskPaper File

In the following example script, instances of the FilePicker and FileType classes are used to read the contents of a chosen TaskPaper document and include the read data in an OmniFocus URL that will upon execution, will place the translated TaskPaper data in the OmniFocus Inbox.

omnifocus://localhost/omnijs-run?script=try%7Bvar%20picker%20%3D%20new%20FilePicker%28%29%0Apicker%2Efolders%20%3D%20false%0Apicker%2Emultiple%20%3D%20false%0Avar%20aFileType%20%3D%20new%20FileType%28%22com%2Etaskpaper%2Etext%22%29%0Apicker%2Etypes%20%3D%20%5BaFileType%5D%0Avar%20pickerPromise%20%3D%20picker%2Eshow%28%29%0ApickerPromise%2Ethen%28function%28urlsArray%29%7B%0A%09var%20fileURL%20%3D%20urlsArray%5B0%5D%0A%09fileURL%2Efetch%28function%28data%29%7B%0A%09%09var%20TaskPaperText%20%3D%20data%2EtoString%28%29%0A%09%09TaskPaperText%20%3D%20encodeURIComponent%28TaskPaperText%29%0A%09%09var%20urlStr%20%3D%20%22omnifocus%3A%2F%2F%2Fpaste%3Fcontent%3D%22%20%2B%20TaskPaperText%0A%09%09URL%2EfromString%28urlStr%29%2Eopen%28%29%0A%09%7D%29%0A%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D
Import TaskPaper File
 

var picker = new FilePicker() picker.folders = false picker.multiple = false var aFileType = new FileType("com.taskpaper.text") picker.types = [aFileType] var pickerPromise = picker.show() pickerPromise.then(function(urlsArray){ var fileURL = urlsArray[0] fileURL.fetch(function(data){ var TaskPaperText = data.toString() TaskPaperText = encodeURIComponent(TaskPaperText) var urlStr = "omnifocus:///paste?target=inbox&content=" + TaskPaperText URL.fromString(urlStr).open() }) })

Import TaskPaper Document Plug-In

The previous script is placed within an Omni Automation plug-in template to create an easy-to-use tool for selecting and importing TaskPaper documents.

Import Chosen TaskPaper Document
 

/*{ "type": "action", "targets": ["omnifocus"], "author": "Otto Automator", "identifier": "com.omni-automation.of.import-taskpaper-file", "version": "1.0", "description": "This action will import the contents of the chosen TaskPaper file to the Inbox.", "label": "Import TaskPaper File", "shortLabel": "Import TaskPaper" }*/ (() => { var action = new PlugIn.Action(function(selection, sender){ // action code var picker = new FilePicker() picker.folders = false picker.multiple = false var aFileType = new FileType("com.taskpaper.com") picker.types = [aFileType] var pickerPromise = picker.show() pickerPromise.then(urlsArray => { var fileURL = urlsArray[0] fileURL.fetch(data => { var TaskPaperText = data.toString() TaskPaperText = encodeURIComponent(TaskPaperText) var urlStr = "omnifocus:///paste?content=" + TaskPaperText URL.fromString(urlStr).open() }) }) }); action.validate = function(selection, sender){ // validation code return true }; return action; })();

Import Chosen TaskPaper into Selected Folder

Similar to the previous plug-in, this example imports the contents of a chosen TaskPaper document into the selected OmniFocus folder:

Import TaskPaper into Selected Folder
 

/*{ "type": "action", "targets": ["omnifocus"], "author": "Otto Automator", "identifier": "com.omni-automation.of.import-taskpaper-into-selected-folder", "version": "1.0", "description": "This action will import the contents of a chosen TaskPaper document into the selected folder", "label": "Import TaskPaper File into Folder", "shortLabel": "TaskPaper to Folder" }*/ (() => { var action = new PlugIn.Action(function(selection, sender){ // action code var folder = selection.folders[0] var folderName = folder.name var picker = new FilePicker() picker.folders = false picker.multiple = false var aFileType = new FileType("com.taskpaper.com") picker.types = [aFileType] var pickerPromise = picker.show() pickerPromise.then(urlsArray => { var fileURL = urlsArray[0] fileURL.fetch(data => { var TaskPaperText = data.toString() TaskPaperText = encodeURIComponent(TaskPaperText) folderName = encodeURIComponent(folderName) var urlStr = "omnifocus:///paste?target=/folder/" + folderName + "&content=" + TaskPaperText URL.fromString(urlStr).open() }) }) }); action.validate = function(selection, sender){ // validation code return (selection.folders.length === 1) }; return action; })();

Export to “TaskPaper-compatible” Format

All Omni applications incorporate the ability to export their content using instances of the FileWrapper class that structure the content in specific formats so they can be written to file. For example, both OmniGraffle and OmniPlan support FileWrappers for exporting content to images.

OmniFocus has a plain-text FileWrapper that is similar to compatible with the data structure used by the TaskPaper application. From the OmniFocus FileWrapper documentation:

Plain Text (.txt)
This is a lightweight plain-text representation of your data, able to be opened in the text editor of your choice. OmniFocus’s plain text export is inspired by TaskPaper, the light to-do application from Hog Bay Software. As such the output should be roughly compatible and able to be imported to TaskPaper with a minimum of fuss.
com.omnigroup.omnifocus2.export-filetype.plain-text

The following script examples use the Plain-Text FileWrapper to convert OmniFocus elements to “TaskPaper-compatible” text.

Converting Objects to Plain-Text

The process of converting OmniFocus abjects to plain-text representations involves the creation of a FileWrapper instance of a specific type. The following examples demonstrate how to create such wrappers and how to use them to save the wrapper data to files.

IMPORTANT: if there are one or more elements selected in the OmniFocus interface, then the plain-text representation of those items will be the result of the script’s actions. If nothing is selected, the entire database will be extracted as plain-text.

In the following example, a wrapper is created and its contents are logged to the console as text:

omnifocus://localhost/omnijs-run?script=try%7Bvar%20fileTypeID%20%3D%20%22com%2Eomnigroup%2Eomnifocus2%2Eexport%2Dfiletype%2Eplain%2Dtext%22%0Avar%20baseName%20%3D%20%22Tasky%2DExport%22%0Avar%20wrapperPromise%20%3D%20document%2EmakeFileWrapper%28baseName%2C%20fileTypeID%29%0AwrapperPromise%2Ethen%28function%28wrapper%29%7B%0A%09var%20wrapperContents%20%3D%20wrapper%2Econtents%2EtoString%28%29%0A%09console%2Elog%28wrapperContents%29%0A%7D%29%0AwrapperPromise%2Ecatch%28function%28err%29%7B%0A%09console%2Eerror%28err%2Emessage%29%0A%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D
Export to Plain-Text
 

var fileTypeID = "com.omnigroup.omnifocus2.export-filetype.plain-text" var baseName = "Tasky-Export" var wrapperPromise = document.makeFileWrapper(baseName, fileTypeID) wrapperPromise.then(wrapper => { var wrapperContents = wrapper.contents.toString() console.log(wrapperContents) }) wrapperPromise.catch(err => { console.error(err.message) })

And a variation of the previous script that incorporates the use of a FileSaver instance to save the data to file:

omnifocus://localhost/omnijs-run?script=try%7Bvar%20fileTypeID%20%3D%20%22com%2Eomnigroup%2Eomnifocus2%2Eexport%2Dfiletype%2Eplain%2Dtext%22%0Avar%20baseName%20%3D%20%22Tasky%2DExport%22%0Avar%20wrapperPromise%20%3D%20document%2EmakeFileWrapper%28baseName%2C%20fileTypeID%29%0A%0AwrapperPromise%2Ethen%28function%28wrapper%29%7B%0A%09var%20wrapperContents%20%3D%20wrapper%2Econtents%2EtoString%28%29%0A%09console%2Elog%28wrapperContents%29%0A%09%0A%09var%20filesaver%20%3D%20new%20FileSaver%28%29%0A%09wrapper%2EpreferredFilename%20%3D%20%22Tasky%2DExport%2Etaskpaper%22%0A%09var%20fileSaverPromise%20%3D%20filesaver%2Eshow%28wrapper%29%0A%0A%09fileSaverPromise%2Ethen%28function%28urlObj%29%7B%0A%09%09console%2Elog%28urlObj%2Estring%29%0A%09%09new%20Alert%28%22FILE%20URL%22%2C%20urlObj%2Estring%29%2Eshow%28result%20%3D%3E%7B%0A%09%09%09urlObj%2Eopen%28%29%0A%09%09%7D%29%0A%09%7D%29%0A%0A%09fileSaverPromise%2Ecatch%28function%28err%29%7B%0A%09%09console%2Elog%28err%2Emessage%29%0A%09%7D%29%0A%7D%29%0A%0AwrapperPromise%2Ecatch%28function%28err%29%7B%0A%09console%2Eerror%28err%2Emessage%29%0A%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D
Save to TaskPaper File and Open
 

var fileTypeID = "com.omnigroup.omnifocus2.export-filetype.plain-text" var baseName = "Tasky-Export" var wrapperPromise = document.makeFileWrapper(baseName, fileTypeID) wrapperPromise.then(function(wrapper){ var wrapperContents = wrapper.contents.toString() console.log(wrapperContents) var filesaver = new FileSaver() wrapper.preferredFilename = "Tasky-Export.taskpaper" var fileSaverPromise = filesaver.show(wrapper) fileSaverPromise.then(urlObj => { console.log(urlObj.string) new Alert("FILE URL", urlObj.string) .show(result => {urlObj.open()}) }) fileSaverPromise.catch(function(err){ console.log(err.message) }) }) wrapperPromise.catch(function(err){ console.error(err.message) })

Export Selected Projects to TaskPaper File

Here is the previous script placed within an Omni Automation plug-in that exports the selected OmniFocus projects as a TaskPaper document.

Export Selected Projects to TaskPaper File
 

/*{ "type": "action", "targets": ["omnifocus"], "author": "Otto Automator", "identifier": "com.omni-automation.of.export-selected-projects-as-taskpaper", "version": "1.0", "description": "This action will export the selected projects as a single TaskPaper file, and then attempt to open the created file in TaskPaper.", "label": "Export Projects as TaskPaper", "shortLabel": "Export to TaskPaper" }*/ (() => { var action = new PlugIn.Action(function(selection, sender){ // action code var fileTypeID = "com.omnigroup.omnifocus2.export-filetype.plain-text" var baseName = "TaskPaper-Export" var wrapperPromise = document.makeFileWrapper(baseName, fileTypeID) wrapperPromise.then(function(wrapper){ var wrapperContents = wrapper.contents.toString() console.log(wrapperContents) var filesaver = new FileSaver() wrapper.preferredFilename = baseName + ".taskpaper" var fileSaverPromise = filesaver.show(wrapper) fileSaverPromise.then(function(urlObj){ console.log(urlObj.string) new Alert("FILE URL", urlObj.string) .show(result => {urlObj.open()}) }) fileSaverPromise.catch(function(err){ console.error(err.message) }) }) wrapperPromise.catch(function(err){ console.error(err.message) }) }); action.validate = function(selection, sender){ // validation code return (selection.projects.length > 0) }; return action; })();