Action

Email Todoist

Posted by Joel Berger, Last update almost 7 years ago - Unlisted

This action will email any Todoist project listed in a file on iCloud Drive. This way, you can send multiple tasks to different projects from a single draft.

Script demonstrates FileManager, Email, and RegEx concepts.

Steps

  • script

    //Create address file in the Drafts5 folder on iCloud Drive named TodoistEmail.txt in the format keyword,project address
    //Inbox,add.task.1232158.137565710.5462696edd413dcc@todoist.net
    //..
    //Due dates use the Todoist email date format: <date yourdate>
    
    //Check to see if draft is blank
    var content = draft.content;
    var check = content.length;
    if (content.length == 0) {
    	alert("Draft is blank");
    	context.cancel("Draft was blank");
    }
    //If content, remove blank lines from draft
    else {
      draft.content = draft.content.replace(/$\s*\n+/gm, "\n");
      draft.update();
    }
    
    //Read addresses from file in iCloud
    var fmCloud = FileManager.createCloud(); // iCloud
    var emailAddresses = fmCloud.read("/TodoistEmail.txt");
    if (emailAddresses == "undefined") {
      console.log("File not found");
      context.fail();
    }
    
    //Send email for each line in a draft
    
    //Split draft and loop over lines
    var lines = draft.content.split("\n");
    for (var line of lines) {
       
       //Extract first project from line
       var projectName = '';
       var task = '';
       var regexMatch = line.match(/#\w+\s*/);
       if (regexMatch) {
          projectName = regexMatch.toString().match(/\w+/);
          //Remove project name and trailing spaces from line
          task = line.replace(regexMatch.toString(), '');
       }
       else {
          //Default project to Inbox if none provided
          projectName = "Inbox";
          task = line;
       }
    
       //Get project address
       var regex = new RegExp(projectName + ",\\S+", "i");
       var addressLine = emailAddresses.match(regex);
       if (addressLine) {
          regex = new RegExp(projectName + ",", "i");
          var emailAddress = addressLine.toString().replace(regex, '');
       }
       else {
          console.log("Address not found");
          context.fail();
       }
    
    	//Create and email line to Todoist
    	var mail = Mail.create();
    	mail.toRecipients = [emailAddress];
    	mail.subject = task;
    	mail.body = " ";
    	mail.sendInBackground = true;
    	var success = mail.send();
    	
    	if (success) {
    		console.log("Task emailed to Todoist");
    	}
    	else { //Something went wrong or was cancelled
    	  	console.log(mail.status);
    	  	if (mail.status == "userCancelled") {
    			context.cancel();
    		}
    		else {
    			context.fail();
    		}
    	}
    }

Options

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