Populate Template Placeholders Script Automation?

Hi there, has anyone attempted to re-create Curt Clifton’s Populate Template Placeholders script for the new automation functionality? I’d love to crib it if anyone has.

1 Like

Yes, I’d love this too…
I mailed Curt directly, but unfortunately he isn’t able to create a JS version.

There’s this one wot I wrote:

Any idea how to add dates onto tasks and the project itself? E.g. Add 1 day wait before Pizza Delivery?

Sadly it doesn’t set dates, that’s one of the downsides of the template itself being a project IN OmniFocus rather than an external TaskPaper/text template.

I used to use the iOS Drafts app and then the Shortcuts app to maintain external taskpaper templates and that would allow smart date insertion. But I found I’d keep changing project/tag structures and forget to update my templates.

That’s why I’ve ended up with this solution of the templates living in the app: If for example I rename or move a tag that’s used by the template, the template gets updated by OmniFocus itself and remains valid.

If you really need smart dates then maybe something like Rosmary Orchards Drafts system (also iOS based) might be more useful: https://rosemaryorchard.com/blog/using-drafts-5-taskpaper-with-omnifocus/

1 Like

Thanks - yes looking at the Taskpaper option that looks like exactly the sort of thing I was after. Thanks!

A taskpaper option that works with JS would be top-notch as it’d be cross-platform. Shortcuts etc are unfortunately not…

Cross platform templates, with fields etc, are the one thing I really want to see automated.

I modified the example script on the Omni automation website that imports a Taskpaper file to do a find/replace (prompting the user) on variables in the file. My convention is to enclose the variables as follows: «var».

It’s just a few extra lines of code. A nice side benefit, aside from being cross-platform, is the Javascript is much faster than the old AppleScript that served the same purpose.

/*{
"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 folder = selection.folders[0]
	var folderName = folder.name
	
	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()
			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)
					folderName = encodeURIComponent(folderName)
					var urlStr = "omnifocus:///paste?target=/folder/" + folderName +  "&content=" + taskPaperText
					URL.fromString(urlStr).open()					
				});				
			}
			else {
				taskPaperText = encodeURIComponent(taskPaperText)
				folderName = encodeURIComponent(folderName)
				var urlStr = "omnifocus:///paste?target=/folder/" + folderName +  "&content=" + taskPaperText
				URL.fromString(urlStr).open()
			}
		})
	})
});

action.validate = function(selection, sender) {
	return (selection.folders.length === 1)
};

return action;

})();

if you copy this script don’t forget the small part below the script…

2 Likes