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();
}
}