Action

TaskPaper > OmniFocus URL > draft

Posted by Jon Mitchell, Last update over 5 years ago

Derived from Rosemary Orchard’s TaskPaper to OmniFocus action, this action encodes TaskPaper as an OmniFocus URL, then creates a new draft containing that URL. You can then copy/paste or send it wherever you want — such as the Notes field in an OmniFocus task — and opening the URL will create the project or tasks in OmniFocus on command.

Rosemary’s action sends the TaskPaper straight through to OmniFocus. This one lets you store it as a link you can trigger later however you want.

OmniFocus has a “Copy as TaskPaper” command built right in, so you don’t even have to write the template in TaskPaper; you can just build it like you normally would inside OmniFocus, then “Copy as TaskPaper,” then paste that into Drafts. If you want to use defer/due dates, though, set them first, then once you convert it to TaskPaper in Drafts, change the absolute date stamps to relative dates, e.g. “Fri 17:00”. That will set the dates on the new tasks and projects to the upcoming Friday, based on when you click the link.

Putting an encoded-TaskPaper URL in the notes field was OmniGroup CEO Ken Case’s suggestion for me as a way to make a project recur conditionally. For example, I have a complex project I need to do every Friday, but only if I’m home that weekend. So I made one weekly task called “Home this week? [launch project in note]”, with the URL-encoded template in the notes field. That way, whether on Mac or iOS, I can create the project with one tap if I’m going to be home.

Steps

  • script

    // See online documentation for examples
    // http://getdrafts.com/scripting
    
    var p = Prompt.create();
    
    p.addButton("New Project");
    p.addButton("New Project in Folder");
    p.addButton("New Tasks in Project");
    p.addButton("New Tasks in Inbox");
    
    var didSelect = p.show();
    
    if (didSelect === false) {
    		cancel("User cancelled the script");
    	}
    
    const baseURL = "omnifocus:///paste";
    
    //set where the taskpaper should be saved
    var target = "";
    switch (p.buttonPressed) {
    	case "New Project":
    		target = "projects";
    		break;
    	case "New Project in Folder":
    		var p2 = Prompt.create()
    		p2.addTextField("folderName", "Folder Name", "");
    		p2.isCancellable = false;
    		p2.addButton("OK");
    		p2.show();
    		target = "/folder/" + encodeURI(p2.fieldValues["folderName"]);
    		break;
    	case "New Tasks in Project":
    		var p2 = Prompt.create()
    		p2.addTextField("projectName", "Project Name", "");
    		p2.isCancellable = false;
    		p2.addButton("OK");
    		p2.show();
    		target += "/task/" + encodeURI(p2.fieldValues["projectName"]);
    		break;
    	case "New Tasks in Inbox":
    		target = "inbox";
    		break; 
    }
    
    //replace any placeholder if they exist
    var known_placeholders = {};
    var placeholders = [];
    
    var draftsContent = editor.getText();
    var matches = draftsContent.match(/«(.+?)»/g);
    
    for (match in matches) {
    	var placeholder = matches[match];
    	known_placeholders[placeholder] = null;
    	placeholders.push(placeholder);
    }
    
    if (placeholders.length === 0) {
    	var alert = Prompt.create();
    	alert.title = "No template placeholders were found.";
    	alert.body = "If your project text has placeholders (that look like «this»), this script will prompt for values you'd like to substitute for them.";
    	var alertCancelled = alert.addButton("Continue Anyway");
    	if (alertCancelled === false) {
    		cancel("User cancelled the script");
    	}
    }	else {
    	for (var placeholder in known_placeholders) {
    		var showPlaceholder = placeholder.replace("«", "");
    		showPlaceholder = showPlaceholder.replace("»", "");
    		var placeholderQuery = Prompt.create();
    		placeholderQuery.title = placeholder;
    		placeholderQuery.addTextField("placeholder", "", showPlaceholder);
    		placeholderQuery.addButton("OK");
    		placeholderQuery.isCancellable = false;
    		placeholderQuery.show();
    
    		draftsContent = draftsContent.replace(new RegExp(placeholder, 'g'), placeholderQuery.fieldValues["placeholder"]);
    	}
    }
    
    //copy final URL to clipboard
    var endUrl = baseURL + "?target=" + encodeURI(target) + "&content=" + encodeURI(draftsContent);
    var d = Draft.create();
    d.content = endUrl;
    d.update();

Options

  • After Success Archive
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.