Skip to content

Instantly share code, notes, and snippets.

@rmchale
Last active January 20, 2025 18:40
Show Gist options
  • Save rmchale/97cd6089ed08153367f49cf919b45703 to your computer and use it in GitHub Desktop.
Save rmchale/97cd6089ed08153367f49cf919b45703 to your computer and use it in GitHub Desktop.
An extension to draftist to quick add tasks to a specific project

This is a custom extension to draftist. It allows you to quick add tasks to a specific project.

I installed this into a seperate js file next to draftist.js. I imported into the draftist workflow via an additional require statement. ie require('draftist_extension.js').

To call it I modified the quick add tasks the reference the new ryan_ versions instead of the original. As an example modifying the Script that quick add lines from draft action calls to something like this would add a task to the my_project project.

Ryan_quickAddLinesFromCurrentDraft('my_project')
/**
* Ryan_quickAddLines - This action creates a new task for each line in the currently open Draft.
*
* @param {String} text the text which lines should be added as tasks to Todoist
* @param {String} project the project to add the task
* @return {Boolean|Number} false when adding fails, task number when adding succeeded
*/
function Ryan_quickAddLines(text, project = null) {
let todoist = new Todoist()
let lines = text.split("\n");
let createdTasksCounter = 0;
// repeat for each line
for (line of lines) {
if (line.length !== 0) {
if (!Ryan_quickAdd({
todoist: todoist,
content: line,
project: project
})) {
// if failed directly return, quickadd will display the error
return false;
} else {
createdTasksCounter++;
}
}
}
return createdTasksCounter;
}
/**
* Ryan_quickAdd - This function adds the provided string to Todoist using the quickAdd API.
* This supports the Todoist natural language which will be processed by Todoist automatically.
*
* @param {Todoist_Object|undefined} todoist_obj - if already created, otherwise the function will create its own.
* @param {string} content the task content as string
* @param {String} project the project to add the task
* @return {Boolean} true when added successfully, false when adding task failed
*/
function Ryan_quickAdd({
todoist = new Todoist(),
content,
project = null
}) {
if (project) {
content = `${content} #${project}`
}
if (!todoist.quickAdd(content)) {
let error = Draftist_getLastTodoistError(todoist)
let errorMsg = "adding tasks failed, todoist returned:\n" + error
Draftist_failAction("Quick Add", errorMsg)
return false;
} else {
return true;
}
}
/**
* Ryan_quickAddLinesFromCurrentDraft - use todoist quick add for each line of the current draft
* @param {String} project the project to add the task
*
* @return {Boolean} true when added successfully, false when adding task failed
*/
function Ryan_quickAddLinesFromCurrentDraft(project=null) {
if (draft.content.length == 0) {
Draftist_cancelAction("Add Tasks from current Draft", "Draft is blank")
return false;
} else {
let taskNumber = Ryan_quickAddLines(draft.content, project);
if (taskNumber) {
// succeeded
projectSuccess = ""
if (project) {
projectSuccess = ` to project ${project}`
}
Draftist_succeedAction("", false, "successfully added " + taskNumber + " tasks" + projectSuccess)
return true;
} else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment