Condition in Script to continue or run a different Action?

Summary - how can I add a condition to a script to check for a tag? If the tag doesn’t exist, how can I call another action, else continue with the script?

I have two Reminders-related actions that I wish to combine.

The first action uses the draft title and sends all lines prefixed with [ ] to a new item in new (if not exist) list in reminders, named after the draft title.

The second action is the vanilla, send each line as a separate reminder to the reminder’s list.

Both work just fine, except I sometimes get the two mixed up. There are very specific circumstances in which I would run one vs the other, and so I’d like to put that into a single reminders action.

The conditions are quite simple.

  • If draft contains a tag named “+active”…
  • Then, run the script that creates a new list with items prefixed with [ ].
  • Else, run the vanilla, each line to an item in the Reminders list.

What I’m thinking is that I could have a condition in the script to check for the tag, and if the tag exists, continue tot he script. Else, call the other action (which I’d hide in another actions list, I guess)

For reference, here is the script I’m using:

// See online documentation for examples
// http://getdrafts.com/scripting
// Start every line with [ ] to denote it as a task

const taskPrefix = “[ ]”;

// Function to perform the callback url
function doCallbackURL(url, params) {
var cb = CallbackURL.create();
cb.baseURL = url;

for(var key in params) {
cb.addParameter(key, params[key]);
}

var success = cb.open();
if (success) {
console.log(“Event created”);
} else {
console.log(cb.status);
if (cb.status == “cancel”) {
context.cancel();
} else {
context.fail();
}
}
}

// Scan for the task prefix in the draft
var lines = draft.content.split("\n");

for (var line of lines) {
// If the line includes the task prefix,
// we remove exclude it from the final notes
if (line.startsWith(taskPrefix)) {

// Remove the trigger from the line
var task = line.replace("[ ]","");
task = task.trim();

reminderList = draft.title, // pick the Reminders list you want
list = ReminderList.findOrCreate(reminderList),
reminder = list.createReminder();
reminder.title = task.trim(); // trim gets rid of end of line (\n), if any
// -
reminder.title = task.replace(/[ ] /,"");
// -
reminder.notes = draft.title;
reminder.url = draft.permalink;
reminder.update();
}
}

The branching logic would look like:

if (draft.tags.includes("+active")) {
    // do one thing
}
else {
    // do other thing
}

You could just incorporate both actions into one script with the branching logic, but if you really want to keep it in different actions, use app.queueAction(action, draft) (docs) to call another action.

1 Like

Thank you. I tried adding more tags (like +hold and +stop), but the script wouldn’t execute properly. Is this the correct syntax?

if (draft.tags.includes("+active"||"+hold"||"+stop")) {
// do one thing

Note that I’m testing on the latest Mac beta (so much easier to edit these scripts, there…)

Includes takes just one string parameter.

This should help:

I think the use of some could be the easiest to implement for your scenario.

Thanks for the link. This is the solution I got working:

if (draft.tags.includes("+active") || draft.tags.includes("+hold") || draft.tags.includes("+stop")) {
// do one thing

1 Like