I had previously shared with you the Drafts script that I use to process my meeting notes. Since the last time I shared it, the Todoist API changed, so I have updated the code to work now (2023). Please find the code below.

// Process Meeting Notes

// Function for removing ("Cacl" and "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 ("TThings:" || "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;
    //this is the line that contains the text of the task
		line = taskrep2(line);
    
    //add a todoist variable
    var todoist = Todoist.create();

		//let the task content be equat to the line we will add
		let taskContent = line;

		//this is where we add the line and wait for the response 
		let createTaskResponse = todoist.createTask({
  	  "content": taskContent,
		})

		//handle error codes by printing appropriate message, do the same for success 
		if (!createTaskResponse) {
  	  let message = "Failed to add task to todoist: " + todoist.lastError;
   	 	console.log(message)
    	context.fail(message);
    	app.displayErrorMessage(message);
  	} else {
      let message = "successfully added task in todoist";
      console.log(message)
      // return relevant parameters of the
  	}

		if (!todoist.lastError) {
    	app.displaySuccessMessage("successfully added task :)");
		}
	
		n += line + "\n";
	}
	else {
		n += line + "\n";
	}
}
draft.content = n;
draft.update();

1 Comment

Comments are closed.