Action

sort daily journal

Posted by @FlohGro, Last update almost 5 years ago - Unlisted

Sort the Daily Journal.

Please adapt the constant values to Identity the list items and change the tag of your lists in the script.

Steps

  • script

    // list & task usage script
    
    const listTag = "test_list";
    const listSign = "*";
    const taskSign = "+";
    
    /* create a todoist object */
    var todoist = Todoist.create();
    /* create an array to collect all the tasks while the script goes through your log draft */
    var taskArray = [];
    /* create an array to collect all the list items while the script goes through your log draft */
    var listArray = [];
    
    let lines = draft.content.split("\n"); // split current draft text into an array of lines.
    
    for (let line of lines) {
      if (line.startsWith(taskSign)) {
        line = line.replace(taskSign + " ", "");
        taskArray.push(line);
      } else if (line.startsWith(listSign)) {
      	 line = line.replace(listSign + " ","");
        listArray.push(line);
      }
    }
    
    for (let task of taskArray) {
    
      todoist.quickAdd(task);
    }
    
    var result; {
      /* create the temporary workspace */
      let ws = Workspace.create();
      /* set the tagfilter of the workspace to your list tag */
      ws.tagFilter = listTag;
      /* save all resulting draft objects into “res” */
      result = ws.query("all");
    }
    
    var myLists = new Map();
    for (res of result) {
      myLists.set(res.title, res.uuid);
    }
    
    for (let item of listArray) {
      /* split item by whitespaces - first element is the “list name” */
      let curList = item.split(" ")[0];
      //alert("curList: " + curList);
      let itemContent = item.replace(curList + " ", "");
      if (myLists.has(curList)) {
        /* the list is already existing, append the content of item to this list */
        
        let curDraft = Draft.find(myLists.get(curList));
        curDraft.content = curDraft.content + "\n - " + itemContent;
        curDraft.update();
      } else {
        /* the list is not existing, create it, add it to myLists and append the content to the new list */
        let newDraft = Draft.create();
        newDraft.content = "# " + curList + "\n\n - " + itemContent;
        newDraft.addTag(listTag);
        newDraft.update();
        myLists.set(newDraft.title, newDraft.uuid);
        //alert("newDraft.title " + newDraft.title);
      }
    }

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.