A better way to achieve the same? Populating Alfred's clipboard history

I have an Alfred workflow that interacts with Todoist on the web/desktop to quickly enter a parent task and its associated subtasks from the clipboard. Alfred presses the modifier keys at the correct time pasting the contents of Alfred’s clipboard history in where applicable, a la Keyboard Maestro.

I’ve managed to use a draft in Drafts to get the parent task from it - {Clipboard:1} in Alfred - and then all subtasks {Clipboard:0} onto Alfred’s clipboard history as required.

The way I have done this is have an action that runs 2 distinct actions as action steps on the draft. The first copies to the clipboard, [[line|1]] - the parent task - and the second [[line|2..]] - all the subtasks.

As I say, this works by populating the clipboard as desired to facilitate the workflow, but is there a more elegant way of doing it? i.e., negating the need for a total of 3 actions to achieve the same output?

Over to you, scripting geniuses😀

If you use the createTask method, you can create a parent task, and then use the ID of the object it creates to create newsub tasks by setting the parent ID when creating subsequent tasks.

So you would use line 1 in drafts to create the first task, and then the subsequent lines to create the sub tasks based on your description.

I’m on a PC right now, so this is totally untested, but I imagine it would be something along the lines of:

// Create Todoist instance
let td = Todoist.create();

// Create first task
let tdParent = td.createTask({"content": draft.displayTitle});

// Get the body of the draft as an array of lines
let astrBody = draft.lines.shift();

// For each line create a sub task for the parent
astrBody.map(strLine => td.createTask({"content": strLine, "parent_id": tdParent.parent_id});

You can call the Drafts action you create to do this from Alfred, but given you are presumably in Drafts already to enter or select the draft, using the action directly is probably most beneficial.

Great stuff. Thanks for your help. I’m getting the error, as per the below.

Script Error: SyntaxError: Unexpected token ';'. Expected ')' to end an argument list. Line number: 11, Column undefined

I’m in no particular rush for this, so perhaps when you are on Drafts at some point and can spot the issue, let me know. I’ve played about with changing the ending of the script, but no dice.


// Create Todoist instance
let td = Todoist.create();

// Create first task
let tdParent = td.createTask({"content": draft.displayTitle});

// Get the body of the draft as an array of lines
let astrBody = draft.lines;
astrBody.shift();

// For each line create a sub task for the parent
astrBody.map(strLine => td.createTask({"content": strLine, "parent_id": tdParent.id}));

One typo and two corrections and it seems to work for me.

Script Error: ReferenceError: Can’t find variable: td
Line number: 2, Column 18

That’s just a copy and paste error on my part - in my defense I was posting from my sick bed at the time :smile:.

If you look back at the first script you can see the first line of script defines it - hence the error noting it fails on line 2… the definition had to be the first line as the original script did complete without that error and is the same as before.

I’ve updated the second script above to include it.

Many thanks and hope you are feeling better soon. I should have spotted it last night, but after being tied up on solving something else, had gone a bit cross-eyed. This works as expected, so thanks so much for the input.