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.3",
"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"
}*/
(() => {
preferences = new Preferences()
const action = new PlugIn.Action(async function(selection, sender){
try {
// 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){
alertMessage = "Reset the stored user name?"
alert = new Alert("Confirmation Required", alertMessage)
alert.addOption("Reset")
alert.addOption("Cancel")
buttonIndex = await alert.show()
if (buttonIndex === 0){
preferences.remove("userName")
}
}
var currentUserName = preferences.readString("userName")
if(!currentUserName){
alertTitle = "1st-Run Setup"
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()
picker = new FilePicker()
picker.folders = true
picker.multiple = false
chosenFolderURL = await picker.show()
chosenFolderURL = chosenFolderURL[0].string
chosenFolderURL = chosenFolderURL.substring(8);
pathArray = chosenFolderURL.split('/')
currentUserName = pathArray[1]
preferences.write("userName", currentUserName)
}
var iCloudURL = `file:///Users/${currentUserName}/Library/Mobile%20Documents/`
}
appName = app.name
lastSegment = `iCloud~com~omnigroup~${appName}/Documents/TaskPaper%20Templates/`
combinedURLstr = iCloudURL + lastSegment
sourceFolderURL = URL.fromString(combinedURLstr)
// SEARCH THE DIRECTORY FOR MATCHING FILES
filetype = TypeIdentifier.fromPathExtension("taskpaper", false)
searchResults = await sourceFolderURL.find([filetype], false)
if (searchResults.length === 0){throw new Error("No files found.")}
// DISPLAY MENU OF FILE NAMES
menuItems = searchResults.map(function(url){
urlString = url.string
lastItem = urlString.substring(urlString.lastIndexOf('/') + 1)
lastItem = lastItem.replace(/\.[^/.]+$/, "") // file extension
return decodeURIComponent(lastItem)
})
menuIndexes = menuItems.map(function(item, index){return index})
templateMenu = new Form.Field.Option(
"template",
null,
menuIndexes,
menuItems,
0
)
destinations = ["Inbox", "Projects"]
destinationMenu = new Form.Field.Option(
"destination",
null,
[0,1],
destinations,
0
)
inputForm = new Form()
inputForm.addField(templateMenu)
inputForm.addField(destinationMenu)
formPrompt = "Template and destination:"
buttonTitle = "Continue"
formObject = await inputForm.show(formPrompt, buttonTitle)
// PROCESS FORM RESULTS
templateIndex = formObject.values['template']
chosenURL = searchResults[templateIndex]
destinationIndex = formObject.values['destination']
destination = destinations[destinationIndex].toLowerCase()
// PROCESS CHOSEN FILE
chosenURL.fetch(function(data){
TaskPaperText = data.toString()
TaskPaperText = encodeURIComponent(TaskPaperText)
urlStr = `omnifocus:///paste?target=${destination}&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){
return true
};
return action;
})();