Populate Template Placeholders Script Automation?

For those interested, I altered @JBB’s script above to work without the need for a folder selection and to place the new project at top-level in the document tree.

/*{
"author": "JBB",
"targets": ["omnifocus"],
"type": "action",
"identifier": "com.jbb.omnifocus.com.template",
"version": "1.0",
"description": "Add Template Using TaskPaper Format",
"label": "Add Template…",
"mediumLabel": "Add Template Using TaskPaper Format",
"paletteLabel": "Add Template",
}*/
(() => {
var action = new PlugIn.Action(function(selection, sender) {
    var picker = new FilePicker()
    picker.folders = false
    picker.multiple = false
    var pickerPromise = picker.show()

    pickerPromise.then(function(urlsArray) {
        var fileURL = urlsArray[0]
        fileURL.fetch(function(data) {
            var taskPaperText = data.toString()
            var macros = taskPaperText.match(/«.*?»/g)
            if (macros != null && macros.length > 0) {
                const distinct = (value, index, self) => { return self.indexOf(value) === index }
                macros = macros.filter(distinct).map(function(x){return x.replace(/[«»]/g, '')})

                var inputForm = new Form()
                for (var i = 0; i < macros.length; i++) {
                    var formInput = new Form.Field.String("var" + String(i), macros[i], "")
                    inputForm.addField(formInput)
                }

                var formPromise = inputForm.show("Enter values for parameters:", "OK")

                inputForm.validate = function(formObject) {
                    return true
                }

                formPromise.then(function(formObject) {
                    for (var i = 0; i < macros.length; i++) {
                        var x = formObject.values["var" + String(i)]
                        taskPaperText = taskPaperText.replaceAll('«' + macros[i] + '»', x)
                    }

                    taskPaperText = encodeURIComponent(taskPaperText)
                    var urlStr = "omnifocus:///paste?target=projects&content=" + taskPaperText
                    URL.fromString(urlStr).open()
                });
            }
            else {
                taskPaperText = encodeURIComponent(taskPaperText)
                var urlStr = "omnifocus:///paste?target=projects&content=" + taskPaperText
                URL.fromString(urlStr).open()
            }
        })
    })
});

return action;
})();

EDIT: One note. To make this work on iOS, the taskpaper template files need to be .txt files and not .taskpaper. iOS does something weird there and doesn’t let you select them.

3 Likes