Action

Smart Draft to OF

Last update almost 4 years ago - Unlisted

Work in progress parser which will help create smart tags on your TaskPaper formatted draft, then leverages Rosemary Orchard’s action to publish to OmniFocus.

Steps

  • script

    // See online documentation for examples
    // https://docs.getdrafts.com/docs/actions/scripting
    
    // Get list of people from Stored Draft to better parse the text for the actual people I need to tag
    
    let peopleDraft = JSON.parse(Draft.find("D832E9CF-05CB-4CA2-B9BB-7D1BCE4A2675").processTemplate("[[body]]"));
    
    let input = editor.getSelectedText();
    
    
    let draftLines = input.split("\n");
    let updatedContent = "";
    for (let i = 0; i < draftLines.length; i++) {
      let updatedLine = newTaskWithContexts(draftLines[i]);
      draftLines[i] = updatedLine;
      updatedContent += draftLines[i] + "\n";
    }
    editor.setSelectedText(updatedContent);
    
    function parsePeople (inputLine) {
      
      let results = [];
      for (const key of Object.keys(peopleDraft)) {
        let regexp = new RegExp("\\b" + key + "\\b");
        let personExists = regexp.test(inputLine);
        if (personExists) {
        //   console.log ("Person: " + key + " links to " + peopleDraft[key] + " which is needed in this task")
        //   results.push("People: " + peopleDraft[key]);
          results.push(key);
        }
      }
      return results;
      
    }
    
    function getPeopleTags(inputLine) {
        let keys = parsePeople(inputLine);
        let results = [];
        for (const key of keys) {
            results.push("People: " + peopleDraft[key]);
        }
        return results;
    }
    
    function isWaitingForTask (inputLine) {
    //   let personFound = peopleDraft[inputArray[1].trim()];
      let personFound = parsePeople(inputLine);
      let strippedTask = inputLine.substring(2);
      if (personFound != null) {
        for (let i = 0; i < personFound.length; i++) {
            if (strippedTask.startsWith(personFound[i])){
                console.log ("Waiting For: " + personFound[i]); 
                return true;
            }
        }
      }
    	return false;
    }
    
    function parseOutDueDate (inputArray) {
      let lastItem = inputArray[inputArray.length-1].trim();
      for (let i = 0; i < daysOfWeek.length; i++) {
        if (lastItem.toLowerCase() == daysOfWeek[i].toLowerCase()) {
           due = "@due(" + daysOfWeek[i] + ")";  
           return due;
        }
      }
    }
    
    function isEmailTask(inputString) {
        
        if (!isWaitingForTask(inputString)) {
            let inputArray = inputString.trim().split(" ");
            let firstWord = inputArray[1].toLowerCase();
            if (firstWord == "send" || firstWord == "email" || firstWord == "message") {
                return true;
        }
      }
      return false;
    }
    
    
    function smartParseOutDueDate (inputArray) {
      let indexIn = inputArray.lastIndexOf("in");
      let indexBy = inputArray.lastIndexOf("by");
      let indexOn = inputArray.lastIndexOf("on");
      let indexAt = inputArray.lastIndexOf("at");
    
      let lastIndex = Math.max.apply(Math, [indexIn, indexBy, indexOn, indexAt]);
    //   console.log ("Max index was: " + lastIndex + " for " + inputArray[lastIndex]);
      let result = "";
      if (lastIndex > 0 && lastIndex < inputArray.length-1) {
        result = "@due(";
        for (let i = lastIndex+1; i < inputArray.length; i++) {
          result += inputArray[i] + " ";
        }
        result = result.trim() + ")";
      }
      return result;
    }
    
    
    function newTaskWithContexts (inputString) {
      let inputArray = inputString.trim().split(" ");
      let result = inputString;
      if (inputArray[0] == "-") {
        let tags = "@tags(";
        if (isWaitingForTask(inputString)){
          tags+="Waiting,";
        } else if (isEmailTask(inputString)){
          tags+="Email,";
        }
        let peopleArr = getPeopleTags(inputString.trim());
        for (let i = 0; i < peopleArr.length; i++) {
          if (i != peopleArr.length - 1){
            tags+= peopleArr[i]+",";
          } else {
            tags+= peopleArr[i];
          }
        }
        tags += ")";
        let due = smartParseOutDueDate(inputArray);
        result = inputString + " " + tags + " " + due;
      }
      console.log(result);
      return result;
    }
    
  • includeAction

    name
    Taskpaper to OF

Options

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