Customizing a "selection to reminders" script for multiple lines?

#1

I think this script by @scripts4drafts for sending the line under the cursor to Reminders is fantastic.

However, I’m wondering if it’s possible to customize it so that multiple lines could be selected in a draft, then the action would parse that so that the first line would be the task title and the remaining lines would be the task note. If I was doing this to a draft, I would just use the [[title]] and [[body]] tags, but I don’t know what to do when working with a selection …

Here’s the actual script for convenience:

let [loc, len] = editor.getSelectedLineRange(),
  str = editor.getTextInRange(loc, len),
  icon = '🔘',
  reminderList = 'Inbox', // pick the Reminders list you want
  list = ReminderList.findOrCreate(reminderList),
  reminder = list.createReminder();
reminder.title = str.trim(); // trim gets rid of end of line (\n), if any
reminder.notes = new Date().toISOString(); // date or whatever you want
reminder.update();
newStr = icon + ' ' + str;
editor.setTextInRange(loc, len, newStr);
// move the cursor to the end of the edited line
editor.setSelectedRange(loc + newStr.trim().length, 0);
editor.setSelectedText('');
// activate is not required if the action is launched via a keyboard button
editor.activate();
#3

Hi @derekvan,

Thank you.

I wrote the following script so that each selected line is sent to Reminders.

let [loc, len] = editor.getSelectedLineRange(),
  str = editor.getTextInRange(loc, len).replace(/\n$/, ''),
  lines = str.split('\n'),
  icon = '🔘',
  reminderList = 'Inbox', // pick the Reminders list you want
  list = ReminderList.findOrCreate(reminderList),
  reminder = list.createReminder();
  
  lines = lines.map(function (line) {
    let reminder = list.createReminder();
    reminder.title = line.trim();
    reminder.notes = new Date().toISOString();
    reminder.update();
    return icon + ' ' + line;
});
let newStr = lines.join('\n');
editor.setTextInRange(loc, len, newStr);
// move the cursor to the end of the edited line
editor.setSelectedRange(loc + newStr.trim().length, 0);
editor.setSelectedText('');
// activate is not required if the action is launched via a keyboard button
editor.activate();

Let me write another one to answer precisely your request :slight_smile:

#4

Here it is:

let [loc, len] = editor.getSelectedLineRange(),
  str = editor.getTextInRange(loc, len).replace(/\n$/, ''),
  lines = str.split('\n'),
  title = lines[0],
  body = lines.slice(1).join('\n'),
  icon = '🔘',
  reminderList = 'Inbox', // pick the Reminders list you want
  list = ReminderList.findOrCreate(reminderList),
  reminder = list.createReminder();
  
reminder.title = title.trim(); // trim gets rid of end of line (\n), if any
reminder.notes = body;
reminder.update();
let newStr = icon + ' ' + str;
editor.setTextInRange(loc, len, newStr);
// move the cursor to the end of the edited line
editor.setSelectedRange(loc + newStr.trim().length, 0);
editor.setSelectedText('');
// activate is not required if the action is launched via a keyboard button
editor.activate();
1 Like
#5

Phenomenal! Thank you so much for doing this. This also helps me work out some more flexible techniques for grabbing selections.

1 Like
#6

you’re welcome

I published the scripts in the Action Directory: