Converting a Drafts Simple List to a format appropriate to be sent to Todoist

Not sure if anyone can help on this one. I think it may be in the Javascript/Regex zone which hovers way above my head:-)

I enjoy using the Simple List facility in Drafts. I use the “Toggle” and “move to bottom” actions available in the directory so that my scratch list (always the same UUID) is nice and tidy. It’s great with keyboard shortcuts.

However, sometimes I’m left with a few unchecked items at the top of the list which should ideally be sent to Todoist. However, as they have the "- [ ] ", 6-character flag at the start, they need to have these removed before sending the list to Todoist via one of the API actions.

So, I’m wondering if an action, composed of maybe the following steps, would be feasible?

  1. Duplicate the checklist draft
  2. Do a regex replace "- [ ] " with nothing
  3. Remove all completed tasks "- [x] "
  4. Send remaining items to Todoist via API

An even more elegant/preferred approach would be:

  1. Select only/all lines from Draft that start with "- [ ] "
  2. Remove "- [ ] " from the selected lines
  3. Send revised selected lines to Todoist Inbox via API, multi-line action
  4. Mark all tasks in the original draft which were "- [ ] " before selection, now as "- [x] "
  5. Move newly-completed tasks to the bottom of draft

Any input would be appreciated on whether or not this looks feasible.

I made it for you, based on someone else’s action :slight_smile:
Unchecked Tasks to Todoist

It doesn’t create a new Draft, it just goes through the current one, takes any tasks which are not complete, and adds them to Todoist for you :slight_smile: Checking them off and moving them is not something I planned into the script, but would be a relatively easy extension!

That’s great. Many thanks for taking the time. Much appreciated.

I just need to work out a way of incorporating steps 4 and 5 into the action. Without those bits, I’m likely to tie myself up in knots with tasks duplicated across Drafts and Todoist. Keen to avoid obviously.

Could you link me to the action you are using to put completed tasks at the bottom?

Yes, it’s this one.

https://actions.getdrafts.com/a/1HP

So it’s only reallt step 4 that I need help with.

1 Like

So this script sets the tasks as completed:

var credential = Credential.create("Todoist", "Todoist API");

credential.addTextField("token", "Token");

credential.authorize();

// check to see if draft is blank
var content = editor.getText();
var check = content.length;

if (content.length == 0) {
	alert("Draft is blank");
	context.cancel("Draft was blank");
}

// Call API for each line in a draft
// split draft and loop over lines
var lines = content.split("\n");

var http = HTTP.create(); // create HTTP object

var params = {"token":credential.getValue("token")};

for (var line of lines) {

	if (line.startsWith("- [ ] ")) {
		task = line.replace("- [ ] ", "");
		params["text"] = task;
		
		var response = http.request({
		  	"url": "https://todoist.com/api/v7/quick/add",
		  	"method": "POST",
		  	"data": params
		});
		
		if (response.success) {
			console.log(response.statusCode);
			content = content.replace(line, "- [x] " + task);
		} else {
		  	console.log(response.statusCode);
		  	console.log(response.error);
		}
	}
}
editor.setText(content);

And then I would have your action use the “include action” and run the other one :slight_smile:

Great stuff. Thanks for your help. Will give this a go. Best of luck too with the new podcast with Mr Sparks. Can’t wait for the deep-dives on Javascript :wink:

Just to flag, I’ve tested and it works great. However, if bolting on the “move to end” script as a run in action, it then doesn’t work. Running the 2 separately, they work fine.

Rather than including it in a step, add an extra line to the end of the first script to queue the action instead.

The documentation for queueAction() can be found here - http://reference.getdrafts.com/objects/App.html

It’ll probably just be that the Draft isn’t up to date in the way it is being referenced until the whole action completes.

Thanks for the advice. However, me being an idiot (stong on vision/low on tools), means I haven’t got a clue what this means.:grin:

Oh :frowning:

In that case can you share the two actions?

So it’s the script as shared by Rosemary above and then also this one in the directory.

https://actions.getdrafts.com/a/1HP

So I added the following line after Rosemary’s script. I actually put it in a separate script action so it’s more obvious.

app.queueAction(Action.find("Move done tasks to end"), draft);

This line says to call your “Move done tasks to end” action after the current action has completed processing.

I have a Todoist account and have given it a quick sense check. The first acion is marking “simple” incomplete tasks (specifically only those set as bulleted line tasks with the check at the start) as complete after pushing them to the Todoist inbox. The acction will then queue up the reordering action that puts those aforementioned completed tasks plus any others previously completed to the end of the draft.

1 Like

Thanks for your help, but I’m not getting the same results.

The tasks get sent to Todoist. The checkboxes briefly change to each showing an “x” within the brackets, but almost instantly, revert back to unchecked. They also don’t move to the end.

As, mentioned above, I know zilch about Javascript. Just to be clear, I took your 2-script action and run it “as is.” I also added a further step, “Include action,” for the “Move done tasks to end,” to see if that made any difference. Again, it didn’t work.

It’s no problem for me to run the 2 separate actions (>Todoist>mark as checked) and (move to bottom).

Ok. I actually had an extra bit on your second script just to confirn it was being run. I’ve removed that, and also got nothing happening.

I’ve made a one character amendment to the original and it looks like it’s working to me. It just didn’t appear to be executing the script function in that second action when called by queueing rather than direct; I guess that maybe included an implicit execution?

Anyway. Rename your old second action (as a backup) and try this revised version.

https://actions.getdrafts.com/a/1Lx

Still can’t get this to work I’m afraid. As above, I’ll stick with the 2 separate actions as diminishing returns have kicked in here on time v return. Cheers.

No worries.

For what it’s worth, here’s what I see when I run it.

Thanks for posting the video. Still no joy my end though.