Action

OmniFocus tasks with link-back

Posted by @John4tl, Last update over 5 years ago

Parses a note for lines beginning with a dash. Makes those lines into OmniFocus tasks, and includes a link back to the Draft in the note field of the tasks.

Props to @RegularCraig for code this forked from.

Steps

  • script

    // Start every line with a - to denote it as a task
    
    const taskPrefix = "-";
    
    // Function for removing the task prefix
    function removeTaskPrefix(s) {
      var f       = (taskPrefix),
          r       = "",
          re      = new RegExp(f,"g"),
          matches = s.match(re);
    
      if (matches) {
        return s.replace(re,r);
      }
    }
    
    // Function to perform the callback url
    function doCallbackURL(url, params) {
      var cb = CallbackURL.create();
      cb.baseURL = url;
    
      for(var key in params) {
       cb.addParameter(key, params[key]);
      }
    
      var success = cb.open();
      if (success) {
        console.log("Event created");
      } else {
        console.log(cb.status);
        if (cb.status == "cancel") {
          context.cancel();
        } else {
          context.fail();
        }
      }
    }
    
    // Scan for the task prefix in the draft
    var lines = draft.content.split("\n");
    
    for (var line of lines) {
      // If the line includes the task prefix, 
      // we remove exclude it from the final notes
      if (line.startsWith(taskPrefix)) {
    
        // Remove the trigger from the line
        var task = removeTaskPrefix(line);
        var alink = draft.permalink;
        console.log(task);
        console.log(alink);
        
        // OmniFocus URL Action
        doCallbackURL("omnifocus:///add", {"name": task, "note": alink, "autosave": true});
     }
    }
    

Options

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