Plug-In: Import TaskPaper Template (iCloud)
This plug-in will display a menu of TaskPaper templates stored within in the OmniFocus iCloud folder, and then import the chosen template.
INSTRUCTIONS: TaskPaper template files (ending in “.taskpaper”) must be stored in a folder titled “TaskPaper Templates” you create within the default OmniFocus iCloud folder. When selected, the plug-in will scan the folder and present a menu/list of the found template files. Select the TaskPaper template to be imported and select the desired import destination, either Inbox or Projects.

IMPORTANT: On a macOS device, the name of the home directory is required for the plug-in to correctly locate the folder “TaskPaper Templates” you created in the OmniFocus iCloud folder. The first time this plug-in is run, it will present a chooser dialog from which you select the Home directory. The plug-in will store the folder name and not ask again.

NOTE: If you want to reset the stored name, hold down the Control key before selecting this plug-in from the Automation menu.

*iCloudURL based upon idea by Marc A. Kastner. Thank you Marc!
Return to: OmniFocus Plug-In Collection
Import TaskPaper Template
/*{
"type": "action",
"targets": ["omnifocus"],
"author": "Otto Automator",
"identifier": "com.omni-automation.of.import-icloud-taskpaper-template",
"version": "2.1",
"description": "This plug-in will present a menu of the TaskPaper files stored in iCloud > OmniFocus > TaskPaper Templates folder. The chosen template will be imported.",
"label": "Import TaskPaper Template",
"shortLabel": "TaskPaper",
"paletteLabel": "TaskPaper",
"image": "square.and.arrow.down.on.square"
}*/
(() => {
let preferences = new Preferences()
const action = new PlugIn.Action(function(selection, sender){
// action code
(async () => {
// GENERATE URL FOR “TaskPaper Templates” FOLDER WITHIN THE CURRENT OMNI APP ICLOUD FOLDER
if (Device.current.iOS){
var iCloudURL = "file:///private/var/mobile/
Library/ Mobile%20Documents/" } else if (Device.current.mac){
if (app.controlKeyDown){
let alertMessage = "Reset the stored user name?"
let alert = new Alert("Confirmation Required", alertMessage)
alert.addOption("Reset")
alert.addOption("Cancel")
let buttonIndex = await alert.show()
if (buttonIndex === 0){
preferences.remove("userName")
}
}
var currentUserName = preferences.readString("userName")
if(!currentUserName){
let alertTitle = "1st-Run Setup"
let alertMessage = "This plug-in scans for TaskPaper templates with a “.taskpaper” extension, stored in a “TaskPaper Templates” folder placed by you within the OmniFocus folder on your iCloud drive.\n\nIn the forthcoming chooser dialog, please select your Home folder."
await new Alert(alertTitle, alertMessage).show()
let picker = new FilePicker()
picker.folders = true
picker.multiple = false
let chosenFolderURL = await picker.show()
chosenFolderURL = chosenFolderURL[0].string
chosenFolderURL = chosenFolderURL.substring(8);
let pathArray = chosenFolderURL.split('/')
currentUserName = pathArray[1]
preferences.write("userName", currentUserName)
}
var iCloudURL = `file:///Users/${currentUserName}/
Library/ Mobile%20Documents/` }
let appName = app.name
let lastSegment = `iCloud~com~omnigroup~${appName}/Documents/
TaskPaper%20Templates/` let combinedURLstr = iCloudURL + lastSegment
let sourceFolderURL = URL.fromString(combinedURLstr)
// SEARCH THE DIRECTORY FOR MATCHING FILES
let filetype = new TypeIdentifier.fromPathExtension("taskpaper", false)
let searchResults = await sourceFolderURL.find([filetype], false)
if (searchResults.length === 0){throw new Error("No files found.")}
// DISPLAY MENU OF FILE NAMES
let menuItems = searchResults.map(function(url){
let urlString = url.string
let lastItem = urlString.substring(urlString.lastIndexOf('/') + 1)
lastItem = lastItem.replace(/\.[^/.]+$/, "") // file extension
return decodeURIComponent(lastItem)
})
let menuIndexes = menuItems.map(function(item, index){return index})
let templateMenu = new Form.Field.Option(
"template",
null,
menuIndexes,
menuItems,
0
)
let destinations = ["Inbox", "Projects"]
let destinationMenu = new Form.Field.Option(
"destination",
null,
[0,1],
destinations,
0
)
let inputForm = new Form()
inputForm.addField(templateMenu)
inputForm.addField(destinationMenu)
let formPrompt = "Template and destination:"
let buttonTitle = "Continue"
let formObject = await inputForm.show(formPrompt, buttonTitle)
// PROCESS FORM RESULTS
let templateIndex = formObject.values['template']
let chosenURL = searchResults[templateIndex]
let destinationIndex = formObject.values['destination']
let destination = destinations[destinationIndex].toLowerCase()
// PROCESS CHOSEN FILE
chosenURL.fetch(function(data){
let TaskPaperText = data.toString()
TaskPaperText = encodeURIComponent(TaskPaperText)
let urlStr = `omnifocus:///paste?target=${destination}&content=${TaskPaperText}`
URL.fromString(urlStr).open()
})
})().catch(err => {
new Alert(err.name, err.message).show()
console.error(err)
})
});
action.validate = function(selection, sender){
// validation code
// selection options: tasks, projects, folders, tags, databaseObjects, allObjects
return true
};
return action;
})();