Categories: Technology

Processing Todo and Calendar Events Using Drafts

There is a more updated version of the code in this article written to keep up with the changes in the Todoist API in 2023, you can find it here.

I had previously made a YouTube video of how I use Drafts 5 to process my meeting notes. Most people who talk to me about that post are particularly interested in how I process the note to automatically add items to my Todo list app and to my calendar. Therefore, I’m using this post to explain this.

As I mention in the video, I’ve created two custom tags TTodoist: and Cal:to identify lines in my notes as Todo items and calendar events respectively. These tags go before the item or event, for example

TTodoist: Read flagged email today

Ccal: Dinner with wife today at 7PM

The first will add an entry in my todo list for checking my flagged email and the second will create an event in my calendar to remind me of my dinner date with my wife — you have no idea how much of a lifesaver that last one is!

I then define a custom Drafts 5 action that executes a piece of code written in JavaScript. The JavaScript code processes the text of a note using regular expressions and identifies the lines that contain calendar events or todo items.

The code then uses URL schemes to send the todo items to Todoist and the calendar events to Fantastical 2, natural language processing does the rest in each of these apps.

Below is the JavaScript code, which I modified from an existing action on the action directory of Drafts, that I use to do this:

// Process Meeting Notes

// Function for removing ("Ccal:" || "TTodoist:")
function taskrep(s) {
  var f       = ('Ccal: '||'TTodoist: ')
    , r       = ''
    , re      = new RegExp(f, 'g')
    , matches = s.match(re);
  if (matches) {
    return s.replace(re,r);
  }
}

function taskrep2(s) {
  var f       = ('Ccal: '||'TTodoist: ')
    , r       = ''
    , re      = new RegExp('TTodoist: ', 'g')
    , matches = s.match(re);
  if (matches) {
    return s.replace(re,r);
  }
}

// Scan for ("Ccal:" || "TTodoist:")
var d = draft.content;
var lines = d.split("\n");
var n = '';

for (var line of lines) {
 if (line.includes("Ccal:")) {
  line = taskrep(line);
  const baseURL = "fantastical2://x-callback-url/parse/";
  
  var cb = CallbackURL.create();
 cb.baseURL = baseURL;
 cb.addParameter("sentence", line);
 cb.addParameter("add",1);
 // open and wait for result
 var success = cb.open();
 if (success) {
  console.log("Event created");
 }else{
    console.log("error!");
 }
 
  n += line + "\n";
 } else if (line.includes("TTodoist:")) {
 var x=line;
 line = taskrep2(line);
  // Todoist Action
  var credential = Credential.create("Todoist", "Todoist API");
  credential.addTextField("token", "Token");
  credential.authorize();
  // Call API
  var http = HTTP.create(); // create HTTP object
  var response = http.request({
    "url": "https://todoist.com/api/v7/quick/add",
    "method": "POST",
    "data": {
      "token":credential.getValue("token"),
      "text":line,
    }
  });
  
  n += line + "\n";
 }
 else {
  n += line + "\n";
 }
}
draft.content = n;
draft.update();

I hope you find this action useful, I use it for all my meeting notes.

Sherif Fadel Fahmy

You get what you see.

View Comments

    Recent Posts

    Exploring the Different Mount Systems for Nikkor Lenses

    Nikon, one of the leading brands in the photography world, offers a plethora of lenses…

    8 months ago

    Best Camera Lens Companies: Capturing Moments in Perfection

    Capturing perfect moments requires the perfect camera lens. Whether you're a professional photographer or an…

    8 months ago

    What is the Difference Between Nikkor FX and DX Lenses

    In the world of digital photography, Nikon is a brand that has consistently stood out…

    8 months ago

    A Comprehensive Review: Best Camera Lenses for Every Type of Photographer

    For photographers, the right camera lens can make a world of difference. Whether you're a…

    8 months ago

    Using Node.js for grades website

    As I tell my students very often, there is no Goldilocks solution. I have previously…

    8 months ago

    Unraveling the Difference: Front-End JavaScript vs Node.js

    In the realm of web development, two terms that frequently arise are front-end JavaScript and…

    9 months ago