No problem, I’m not good at JS, too but learning by doing helps…
A few thoughts:
to create todoist tasks in your todoist inbox its very straight forwart to use the Todoist Integration from drafts. You setup a credential for todoist (first call of the action will ask for your todoist credentials, afterwards you wont need to type them again into drafts until you remove the credentials in settings) and you can use the quickAdd API to push tasks into your inbox.
To Start your Script, I’d do the following:
/* create a todoist object */
var todoist = Todoist.create();
/* create an array to collect all the tasks while the script goes through your log draft */
var taskArray = [];
/* create an array to collect all the list items while the script goes through your log draft */
var listArray = [];
then the script from @agiletortoise can be used to loop through the lines and you simply add the elements to the desired array:
let lines = draft.content.split("\n"); // split current draft text into an array of lines.
for (let line of lines) {
if (line.startsWith("[ ]")) {
taskArray.push(line);
}
else if (line.startsWith("#")) {
listArray.push(line);
}
}
now, the simple part is to add all the tasks to your todoist inbox with the quickAdd API:
it will loop through the taskArray and push every element to your todoist inbox. You could also push them the right projects with the above referenced descriptions in drafts reference but this will be more complicated to do!
for(let task of taskArray){
todoist.quickAdd(task);
}
The part of your log entries marked with “#” does depend on your setup. If you e.g. have a special tag to all these drafts you can filter for them with a workspace object - I’ll go with this solution for now and explain what I think would needs to be done.
First the tag for your lists should be defined at the beginning of your script (maybe you want to change it later or share your results. so e.g. put:
const listTag = "ref_list";
… to the beginning of the script.
Then, after pushing the tasks to todoist, your script should create a temporary workspace with all your list drafts:
var result;
{
/* create the temporary workspace */
let ws = Workspace.create();
/* set the tagfilter of the workspace to your list tag */
ws.tagFilter = listTag;
/* save all resulting draft objects into "res" */
result = ws.query("all");
}
Maybe the next step can be accomplished a lot faster, too but I dont know -. You would need to collect the uuids and titles of the draft objects contained in the variable result. I’d use a map for this:
var myLists = new Map();
for(res of result){
myLists.set(res.title,res.uuid);
}
The map myLists now contains all the titles and corresponding uudis of your available lists.
Next step would be to seperate the List name (#Cust1) from the rest of your line and then check if this list already exists.
- If the list already exits, append the rest of the line to the list.
- If the list does not exist, create a new one and add the rest of the line to the new list.
for(let item of listArray){
/* split item by whitespaces - first element is the "list name" */
let curList = item.split(" ")[0];
let itemContent = item.replace(curList + " ","");
if(myLists.has(curList)){
/* the list is already existing, append the content of item to this list */
let curDraft = Draft.find(myLists.get(curList));
curDraft.content = curDraft.content + "\n - " + itemContent;
} else {
/* the list is not existing, create it, add it to myLists and append the content to the new list */
let newDraft = Draft.create();
newDraft.content = curList + "\n\n - " + itemContent;
newDraft.update();
myLists.set(newDraft.title,newDraft.uuid);
}
}
I hope I didn’t forget an important thing - I did not test all the code here, so please check it out and then report if anything does not work.
edit: If you dont want the “#” in the title, you can of course replace / remove this, too in the title and just use the “Cust1”