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.
Import TaskPaper File
(async () => {
try {
picker = new FilePicker()
picker.folders = false
picker.multiple = false
aFileType = new FileType("com.taskpaper.text")
picker.types = [aFileType]
urlsArray = await picker.show()
fileURL = urlsArray[0]
fileURL.fetch(function(data){
TaskPaperText = data.toString()
TaskPaperText = encodeURIComponent(TaskPaperText)
urlStr = `omnifocus:///paste?target=inbox&content=${TaskPaperText}`
URL.fromString(urlStr).open()
})
}
catch(err){
if(!err.causedByUserCancelling){
console.error(err.name, err.message)
new Alert(err.name, err.message).show()
}
}
})();
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.1",
"description": "This action will import the contents of the chosen Taskpaper file to the Inbox.",
"label": "Import Taskpaper File",
"shortLabel": "Import Taskpaper",
"paletteLabel": "Toolbar Item Title",
"image": "t.square"
}*/
(() => {
const action = new PlugIn.Action(async function(selection, sender){
try {
picker = new FilePicker()
picker.folders = false
picker.multiple = false
aFileType = new FileType("com.taskpaper.text")
picker.types = [aFileType]
urlsArray = await picker.show()
fileURL = urlsArray[0]
fileURL.fetch(function(data){
TaskPaperText = data.toString()
TaskPaperText = encodeURIComponent(TaskPaperText)
urlStr = `omnifocus:///paste?target=inbox&content=${TaskPaperText}`
URL.fromString(urlStr).open()
})
}
catch(err){
if(!err.causedByUserCancelling){
console.error(err.name, err.message)
new Alert(err.name, err.message).show()
}
}
});
action.validate = function(selection, sender){
// selection options: tasks, projects, folders, tags, databaseObjects, allObjects
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.1",
"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",
"paletteLabel": "Taskpaper to Folder",
"image":"plus.rectangle.on.folder"
}*/
(() => {
const action = new PlugIn.Action(async function(selection, sender){
try {
folder = selection.folders[0]
folderName = folder.name
picker = new FilePicker()
picker.folders = false
picker.multiple = false
aFileType = new FileType("com.taskpaper.text")
picker.types = [aFileType]
urlsArray = await picker.show()
fileURL = urlsArray[0]
fileURL.fetch(function(data){
taskpaperText = data.toString()
taskpaperText = encodeURIComponent(taskpaperText)
folderName = encodeURIComponent(folderName)
urlStr = "omnifocus:///paste?target=/folder/" + folderName + "&content=" + taskpaperText
console.log(urlStr)
URL.fromString(urlStr).open()
})
}
catch(err){
if(!err.causedByUserCancelling){
console.error(err.name, err.message)
new Alert(err.name, err.message).show()
}
}
});
action.validate = function(selection, sender){
// selection options: tasks, projects, folders, tags, databaseObjects, allObjects
return (selection.folders.length === 1)
};
return action;
})();
Importing Taskpaper Templates
TIP: Be sure to check out the plug-in for importing Taskpaper templates from your iCloud OmniFocus folder.
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.
In the following example, a wrapper is created and its contents are logged to the console as text:
Export to Clipboard as Plain-Text
(async () => {
try {
fileTypeID = "com.omnigroup.omnifocus2.export-filetype.plain-text"
baseName = "Tasky-Export"
wrapper = await document.makeFileWrapper(baseName, fileTypeID)
wrapperContents = wrapper.contents.toString()
console.log(wrapperContents)
Pasteboard.general.string = wrapperContents
new Alert("Completion", "Contents are on the clipboard.").show()
}
catch(err){
new Alert(err.name, err.message).show()
}
})();
And a variation of the previous script that incorporates the use of a FileSaver instance to save the data to file:
Save to File and Open
(async () => {
try {
// SAVE CONTENTS
fileTypeID = "com.omnigroup.omnifocus2.export-filetype.plain-text"
baseName = "Tasky-Export"
wrapper = await document.makeFileWrapper(baseName, fileTypeID)
wrapper.preferredFilename = "Tasky-Export.taskpaper"
wrapperContents = wrapper.contents.toString()
console.log(wrapperContents)
// PROMPT FOR DESTINATION
filesaver = new FileSaver()
urlObj = await filesaver.show(wrapper)
console.log(urlObj.string)
alert = new Alert("FILE URL", urlObj.string)
result = await alert.show()
urlObj.open()
}
catch(err){
new Alert(err.name, err.message).show()
}
})();
Here’s a variation of the export script that saves the export to the local OmniFocus Documents folder and opens the exported Taskpaper file:
Save to OmniFocus Documents folder and Open
(async () => {
try {
fileTypeID = "com.omnigroup.omnifocus2.export-filetype.plain-text"
baseName = "Tasky-Export"
wrapper = await document.makeFileWrapper(baseName, fileTypeID)
textData = wrapper.contents
console.log(textData)
folderURL = URL.documentsDirectory
fileName = "Tasky-Export.taskpaper"
fileURL = folderURL.appendingPathComponent(fileName)
wrapper = FileWrapper.withContents(fileName, textData)
wrapper.write(fileURL, [FileWrapper.WritingOptions.Atomic], null)
alert = new Alert("FILE URL", fileURL.string)
result = await alert.show()
fileURL.open()
}
catch(err){
new Alert(err.name, err.message).show()
}
})();
Export to TaskPaper File
Here is the previous script placed within an Omni Automation plug-in that exports the OmniFocus window contents as a TaskPaper document.
Export Contents to TaskPaper File
/*{
"type": "action",
"targets": ["omnifocus"],
"author": "Otto Automator",
"identifier": "com.omni-automation.of.export-as-taskpaper",
"version": "1.1",
"description": "This action will export the window contents or selection as a single TaskPaper file, and then attempt to open the created file in TaskPaper.",
"label": "Export Contents or Selection as TaskPaper",
"shortLabel": "Export to TaskPaper",
"paletteLabel": "Export to TaskPaper",
"image": "doc.text"
}*/
(() => {
const action = new PlugIn.Action(async function(selection, sender){
try {
fileTypeID = "com.omnigroup.omnifocus2.export-filetype.plain-text"
baseName = "Tasky-Export"
wrapper = await document.makeFileWrapper(baseName, fileTypeID)
wrapper.preferredFilename = "Taskpaper-Export.taskpaper"
wrapperContents = wrapper.contents.toString()
console.log(wrapperContents)
filesaver = new FileSaver()
fileURL = await filesaver.show(wrapper)
console.log(fileURL.string)
alert = new Alert("FILE URL", fileURL.string)
alert.addOption("Open File")
alert.addOption("Done")
buttonIndex = await alert.show()
if(buttonIndex === 0){fileURL.open()}
}
catch(err){
if(!err.causedByUserCancelling){
console.error(err.name, err.message)
new Alert(err.name, err.message).show()
}
}
});
action.validate = function(selection, sender){
return true
};
return action;
})();