Drafts Action Directory

Task in Todoist with options

Posted by agiletortoise, Last updated: 2018-07-15 13:42:26 UTC

Create a task in Todoist first prompting for options like project and labels.

For more Todoist examples, visit the Todoist Integration Guide.

Install

Steps

  • script

    // Prompt to select Todoist project and labels
    // Draft as task in Todoist
    
    // utility function
    let mapIds = (arr, key, valKey) => {
    	let result = {};
    	arr.forEach(o => {
    		result[o[key]] = o[valKey];
    	})
    	return result;
    }
    
    let f = () => {
    	// create Todoist object and load project and label lists
    	let todoist = Todoist.create();
    	let projects = todoist.getProjects();
    	let labels = todoist.getLabels();
    
    	if (!projects || !labels) { 
    		console.log("Unable to retreive data from Todoist.");
    		return false; 
    	}
    
    	// get values to use in Task and optional comment
    	let content = draft.processTemplate("[[title]]");
    	let commentContent = draft.processTemplate("[[body]]");
    
    	// create prompt
    	let p = Prompt.create();
    	p.title = "Create Todoist Task";
    
    	// add content
    	p.addTextField("content", "Task", content);
    	p.addTextView("comment", "Comments", commentContent, {"height": 60});
    	
    	// add due string
    	p.addTextField("due_string", "Due", "");
    
    	// add projects
    	let projectLookup = mapIds(projects, "name", "id");
    	let projectNames = Object.keys(projectLookup);
    	p.addPicker("project", "Project", [projectNames], [0]);
    
    	// add labels
    	let labelLookup = mapIds(labels, "name", "id");
    	if (labels.length > 0) {
    		p.addSelect("labels", "Assign labels", Object.keys(labelLookup), [], true);
    	}
    	
    	p.addButton("Create Task");
    	if (!p.show()) {
    		context.cancel();
    		return true;
    	}
    
    	// read values from prompt
    	let projectIndex = p.fieldValues["project"][0];
    	let projectName = projectNames[projectIndex];
    	let projectID = projectLookup[projectName];
    
    	let labelIDs = [];
    	if (labels.length > 0) {
    		let labelNames = p.fieldValues["labels"];
    		labelIDs = labelNames.map(n => labelLookup[n]);
    	}
    		
    	content = p.fieldValues["content"];
    	commentContent = p.fieldValues["comment"];
    	let due_string = p.fieldValues["due_string"];
    
    	if (content.length == 0) {
    		alert("Task content required");
    		return false;
    	}
    
    	// create task in Todoist
    	let task = todoist.createTask({
    		"content": content,
    		"project_id": projectID,
    		"label_ids": labelIDs,
    		"due_string": due_string
    	});
    	if (!task) { return false }
    
    	// add body as comment on new task if necessary
    	if (commentContent.length > 0) {
    		let task_id = task["id"];
    		let comment = todoist.createComment({
    			"task_id": task_id,
    			"content": commentContent
    		});
    		if (!comment) {
    			console.log("Unable to create task comment");
    		}
    	}
    
    	console.log("Todoist task created");
    	return true;
    }
    
    if (!f()) {
    	context.fail();
    }
    
    

Options

  • After Success Archive
    Notification Info
    Log Level Info

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.