Help with drafts action (JavaScript action)

I’m using Drafts and ’d like to add a text at the end of each sentence like the example below. However, I’ve got no skills in js and can’t find any other way (it needs to be done within drafts, ie not in Shortcuts).

Any help? Thank you!!

For example if I have:

Item 1
Item 2
Item 3

The action would add

Item 1 #test_proj @test_label 31/03/2020
Item 2 #test_proj @test_label 31/03/20k20
Item 3 #test_proj @test_label 31/03/2020

Where:
#test_proj —> fixed text
@test_label —> fixed text
31/03/2020 —> last day of current month (or ideally along whether it’s current or next month and calculates last day of that month)

PS- this could be an action or embedded into the action I’m using to send it to Todoist (takem from the drafts’ action repository)

// create task in Todoist inbox for each line in the draft

let lines = draft.content.split("\n");

let todoist = Todoist.create();
let ctErrors = 0;

for (let line of lines) {
if (line.length > 0) {
let success = todoist.quickAdd(line);
if (success) {
console.log("Todoist task created: " + line);
}
else {
ctErrors++;
console.log("Todoist error: " + todoist.lastError);
}
}
}

if (ctErrors > 0) {
context.fail();
}

This is a great first project to try coding in JS, and you’re right that the Todoist script gives you most of what you’d need.

To fix it up, you’d want to do something like:

let endOfMonth = Date.today().moveToLastDayOfMonth().toString("DD/MM/yyyy");
let lineAddition = "#test_proj @test_label " + endOfMonth;

Then add the following just below the if (line.length > 0) { line:

line += lineAddition;

I haven’t really checked that, but hopefully it’s enough to point you in the right direction.

1 Like

Thanks a lot @cpac!! Perfect!

The only bit I changed was the DD/MM/yyyy to dd/MM/yyyy :wink:

1 Like