Action

Send Tasks to Focus

Posted by jochi, Last update about 4 years ago

Add the selected text or entire contents of the draft to Focus.

  • Draft (or current selection) will be added as a tasks in Focus.
  • Unformatted text, simpletask, and markdown list items will be added as tasks
  • Markdown headings, struck-out, completed simpletask items, and empty lines will be ignored.

Supported Parameters

Links

  • Markdown links are recognized and will be linked in Focus, e.g. [Task to do](omnifocus:///task/123456)
  • By default, each task will link back to the source Draft

Estimate Examples

  • Minutes: @estimate(10m) will be interpreted as 10 minutes
  • Sessions: @estimate(2s) will be interpreted as 2 sessions

Notes

  • Text following the pipe character “|” will be stored in the Notes of the Focus task

Steps

  • script

    const d = draft;
    const task_list = d.processTemplate("[[selection]]");
    const base_url = "focusapp://add";
    const source_url = d.processTemplate("[[draft_open_url]]");
    const estimate_regex = /@estimate\((\d+)(.)\)/; 
    var link_url;
    var tasks = task_list.split(/\n/).reverse();
    for (var task of tasks) {
    
      if (task.match(/(^#)|(^\s*$)|(^- \[x\])|(^- [.] $)|(.*?~~.*?~~.*?)/)) { // ignore lines that are struck out, have completed tasks, or are markdown headings or empty
        continue;
      } else {
        task = task.replace(/^- (\[ ] )?(.*)/,"$2");
        var estimate = task.match(estimate_regex)[1]; // If there is an estimate assign for inclusion 
        var unit = task.match(estimate_regex)[2]; // Get the unit 
    
        console.log(task.match(estimate_regex)[1]+task.match(estimate_regex)[2]);
    
        var note = task.split(/\|/)[1]; // If there is a note, assign for inclusion
        task = task.replace(/ @estimate\(.*?\)/, "").replace(/\|.*/, ""); // Delete estimate tag and note
        if (task.match(/\[.*?\]\(.*?\)/)) {
          link_url = getLink(task);
          task = task.replace(/\[(.*?)\]\(.*?\)/,"$1");
        } else {
          link_url = source_url;
        }
        
        var cb = CallbackURL.create();
        cb.baseURL = base_url;
        cb.addParameter("title", task);
        cb.addParameter("sourceURL", link_url);
        var session_type = (unit == "m") ? "minutesEstimate" : "sessionEstimate"; 
        if (unit) { cb.addParameter(session_type, estimate); } 
        if (note) { cb.addParameter("note", note); }
    
        // open and wait for result
    
        var success = cb.open();
        if (!success) {
          if (cb.status == "cancel") {
            context.cancel();
            break;
          } else {
            context.fail();
            break;
          }
        }
        
      }
    }
    
    var focus_start =  CallbackURL.create();
    focus_start.baseURL = "focusapp://start-timer";
    
    let p = Prompt.create();
    p.title = "Start Session";
    p.message = "Do you want to start a Focus session?";
    p.isCancellable = true;
    
    p.addButton("Start Session", 1);
    
    let didSelect = p.show();
    
    if (p.buttonPressed == 1) {
    	focus_start.open();
    }
    
    function getLink(task) {
      return task.match(/\((.*?)\)/)[1];
    }
    
    

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.