Import Calendar Events for Today

I’ve read the threads here in the forum on importing calendar events along with trying to manipulate calendar actions based on the Calendar Drafts Script Reference… but my limited skills are preventing me from achieving my goal - which is:

Create an Action that inserts “today’s” calendar events from “all” my calendars in the following format:
Time (in 24h) + Event Title

That’s it… all I am looking for.

I’d like to avoid date and calendar pickers… and eliminate all the necessary date data…

Is this possible?

This should do what you want in a new draft, if you add an action with a single script step:


// script to get all events for
// all calendars for the next 
// day, and add to a new draft
// by tf2

// get all calendars
let cals = Calendar.getAllCalendars();

// set a header string to hold
// the result 
aDate = new Date();

aStr = aDate.getFullYear()+'-' + (pad2(aDate.getMonth()+1)) + '-'+ pad2(aDate.getDate());
result = "# Today's Agenda for ";
result += aStr + "\n\n";

// cycle thru calendars
for (let cal of cals) {
  if (cal) {
    // set today & tomorrow
    let today = new Date();
    let tomorrow = new Date(today);
    tomorrow.setDate(tomorrow.getDate() + 1);
    // get all events
    let events = cal.events(today, tomorrow);
    // cycle thru each event
    for (let event of events) {
      // get start date
      let start = event.startDate;
      // get hour, minutes
      let st_hour = start.getHours();
      let st_min = pad2(start.getMinutes());
      // add to result
      result += st_hour+":"+st_min+" — ";
      result += event.title;
      result += "\n";
    }
  }
}

let d = new Draft();
d.content = result;
d.addTag("agenda");
d.update();

// function to ensure minutes 
// are two digits; from
// https://electrictoolbox.com/pad-number-two-digits-javascript/
function pad2(number) {
  return (number < 10 ? '0' : '') + number
}

4 Likes

Amazing! Is it possible to prepend on the end of each line the name of the calendar?

@tf2 This is brilliant - thank you so much.

Reading through it helps me understand the process.

Curious about two parts:

  1. Researching “Appending Script to Current Draft” appears to have limitations. If “Append” is not an option - could the script simply add the results to the clipboard?

For example - running the script returns:

15:30 — Meeting 1
18:30 — Meeting 2

I could then paste the result directly into the draft I want to see it in.

  1. What date parameter would I change in order to get tomorrows events?

Many thanks again!

Easy to do. In the below version, I added a setting (preCal) at the top. If you change it from false to true, it will prepend the calendar to each event.


// version 0.1.1 (20210526)
// script to get all events for
// all calendars for the next 
// day, and add to a new draft
// by tf2

// SETTINGS

// true=>prepend calendar name 
let preCal = false;

// get all calendars
let cals = Calendar.getAllCalendars();

// set a header string to hold
// the result 
aDate = new Date();

aStr = aDate.getFullYear()+'-' + (pad2(aDate.getMonth()+1)) + '-'+ pad2(aDate.getDate());
result = "# Today's Agenda for ";
result += aStr + "\n\n";

// cycle thru calendars
for (let cal of cals) {
  if (cal) {
    // set today & tomorrow
    let today = new Date();
    let tomorrow = new Date(today);
    tomorrow.setDate(tomorrow.getDate() + 1);
    // get all events
    let events = cal.events(today, tomorrow);
    // cycle thru each event
    for (let event of events) {
      // get start date
      let start = event.startDate;
      // get hour, minutes
      let st_hour = start.getHours();
      let st_min = pad2(start.getMinutes());
      // add to result
      if (preCal) {
        result += cal.title;
        result += " — ";
      }
      result += st_hour+":"+st_min+" — ";
      result += event.title;
      result += "\n";
    }
  }
}

let d = new Draft();
d.content = result;
d.addTag("agenda");
d.update();

// function to ensure minutes 
// are two digits; from
// https://electrictoolbox.com/pad-number-two-digits-javascript/
function pad2(number) {
  return (number < 10 ? '0' : '') + number
}

Working through today and tomorrow:

Days = 24 hours…

Changing this line:

to tomorrow.setDate(tomorrow.getDate() + 2); = 48 hours… > helpful if planning for tomorrow and more than 24 is required to capture tomorrows events.

Still working though either appending or capturing the result to the clipboard…

Was able to add the events results to the clipboard by following @sylumer code to post the resulting string to the clipboard.

Here is my modified version of @tf2 script <thank you!>

I can’t figure out how to completely eliminate the date from the first line - but I can delete it manually:

// script to get all events for
// all calendars for the next 
// day, and add to a new draft
// by tf2

// get all calendars
let cals = Calendar.getAllCalendars();

// set a header string to hold
// the result 
aDate = new Date();

aStr = aDate.getFullYear()+'-' + (pad2(aDate.getMonth()+1)) + '-'+ pad2(aDate.getDate());
result = "";
result += aStr + "\n";

// cycle thru calendars
for (let cal of cals) {
  if (cal) {
    // set today & tomorrow
    let today = new Date();
    let tomorrow = new Date(today);
    tomorrow.setDate(tomorrow.getDate() + 2);
    // get all events
    let events = cal.events(today, tomorrow);
    // cycle thru each event
    for (let event of events) {
      // get start date
      let start = event.startDate;
      // get hour, minutes
      let st_hour = start.getHours();
      let st_min = pad2(start.getMinutes());
      // add to result
      result += st_hour+":"+st_min+" ";
      result += event.title;
      result += "\n";
    }
  }
}

let d = new Draft();
d.content = result;
app.setClipboard(result);
console.log(result);
d.update();

// function to ensure minutes 
// are two digits; from
// https://electrictoolbox.com/pad-number-two-digits-javascript/
function pad2(number) {
  return (number < 10 ? '0' : '') + number
}

Here’s a revised version that does several things by changing values at the top of the script:

  1. Set whether to prepend the calendar name to each line

  2. Set a date range to display events from, as an offset from today’s date, in days (0 is today, 1 is tomorrow, etc.) — note that this handles month ends nicely, so May 31 + 1 day should give you June 1 instead of May 32, thanks to a function I found online.

  3. Customizes the header based on the date range you pick, with a special “today” value if you set the offsets to 0 and 1

  4. Choose whether to append the result to the current draft or create a new draft.

Enjoy.


// version 0.1.2 (20210526.2)
// script to get all events for
// all calendars for the next 
// day, and add to a new draft
// by tf2

// SETTINGS

// prepend calendar name 
// to each line: set true
let preCal = false;

// append agenda to current
// draft: set true
// new draft: set false
let append = true;

// date offset determined the
// range of days for which
// events are shown. Number is
// added to today's date; 0 means
// today, 1 means tomorrow, etc
let st = 0;
let en = 1;
// set today's date
let aDate = new Date();
// set start date
let sDate = aDate.addDays(st);
let eDate = aDate.addDays(en)

// get all calendars
let cals = Calendar.getAllCalendars();

// set header
// set a header string to hold
// the result 
let sStr = sDate.getFullYear()+'-' + (pad2(aDate.getMonth()+1)) + '-'+ pad2(aDate.getDate());
let eStr = eDate.getFullYear()+'-' + (pad2(aDate.getMonth()+1)) + '-'+ pad2(aDate.getDate());
result = "# Agenda for ";
// customize header for today
if (st == 0 && en == 1){
  result += "today\n\n";
}
else {
  result += sStr + " to " + eStr + "\n\n";
}

// cycle thru calendars
for (let cal of cals) {
  if (cal) {
    // set today & tomorrow
    let startD = new Date();
    startD.setDate(startD.getDate() + st);
    let endD = new Date(startD);
    endD.setDate(endD.getDate() + en);
    // get all events
    let events = cal.events(startD, endD);
    // cycle thru each event
    for (let event of events) {
      // get start date
      let start = event.startDate;
      // get hour, minutes
      let st_hour = start.getHours();
      let st_min = pad2(start.getMinutes());
      // add to result
      if (preCal) {
        result += cal.title;
        result += " — ";
      }
      result += st_hour+":"+st_min+" — ";
      result += event.title;
      result += "\n";
    }
  }
}

// append to a new draft or
// create a new one, using
// append setting from top
if (append) {
  result = "\n"+result;
  draft.append(result);
  draft.addTag("agenda");
  draft.update;
}
else {
  let d = new Draft();
  d.content = result;
  d.addTag("agenda");
  d.update();
}

// function to ensure dates
// and times are padded 
// to two digits; from
// https://electrictoolbox.com/pad-number-two-digits-javascript/
function pad2(number) {
  return (number < 10 ? '0' : '') + number
}

// function to add days to
// a date, from:
// https://stackoverflow.com/questions/563406/add-days-to-javascript-date
Date.prototype.addDays = function(days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
}


2 Likes

You already did a great job by providing such powerful automation. However, I have another suggestion/request. Are you interested to expand your script to include reminders from Apple Reminders and create a section with heading: Due today or something similar?
This way you could get your mini dashboard for today which will include both events and reminders😊

Great idea. I can probably do this, but maybe not until tonight. (This is more or less how I’m teaching myself Javascript, so thank you!)

2 Likes

Some more settings and functionality, mostly to incorporate reminders:

  • set remind in the settings section to change how reminders are handled:
    • false (no quotes) ignores Reminders altogether
    • ”inc” (with quotes) retrieves just incomplete tasks
    • ”comp” (with quotes) retrieves just completed tasks
    • ”due” (with quotes) retrieves incomplete tasks that have due dates on or after the start date (same as for events, set separately, st)
  • set preRem to true (no quotes) to add the name of the Reminder list each reminder belongs to
  • if you use MultiMarkdown or some other syntaxes, the headings should stand out and the checkboxes should be tappable; however, marking a task as done in the draft won’t affect the original reminder in any way.

// version 0.1.3 (20210526.3)
// script to get all events for
// all calendars for the next 
// day, and add to a new draft
// by tf2

// SETTINGS

// append agenda to current
// draft: set true
// new draft: set false
let append = true;

// date offset determined the
// range of days for which
// events are shown. Number is
// added to today's date; 0 means
// today, 1 means tomorrow, etc
let st = 0;
let en = 1;
// set today's date
let aDate = new Date();
let aaDate = new Date();
// set start date
let sDate = aDate.addDays(st);
let eDate = aaDate.addDays(en)

// include Reminders?
// no = false (no quotes)
// "inc" = all incomplete tasks
// "due" = incomplete tasks due in
//		range set above
// "comp" = completed tasks
// "all" = all tasks
var remind = "due";

// prepend calendar name
// or reminder list 
// to each line: set true
let preCal = false;
let preRem = false;


// START SCRIPT PROPER

// set header
// set a header string to hold
// the result 
let sStr = sDate.getFullYear()+'-' + (pad2(sDate.getMonth()+1)) + '-'+ pad2(sDate.getDate());
let eStr = eDate.getFullYear()+'-' + (pad2(eDate.getMonth()+1)) + '-'+ pad2(eDate.getDate());
result = "# Agenda for ";
// customize header for today
if (st == 0 && en == 1){
  result += "today\n\n";
}
else {
  result += sStr + " to " + eStr + "\n\n";
}

// get all calendars
let cals = Calendar.getAllCalendars();

// cycle thru calendars
for (let cal of cals) {
  if (cal) {
    // set today & tomorrow
    let startD = new Date();
    startD.setDate(startD.getDate() + st);
    let endD = new Date(startD);
    endD.setDate(endD.getDate() + en);
    // get all events
    let events = cal.events(startD, endD);
    // cycle thru each event
    for (let event of events) {
      // get start date
      let start = event.startDate;
      // get hour, minutes
      let st_hour = start.getHours();
      let st_min = pad2(start.getMinutes());
      // add to result
      if (preCal) {
        result += cal.title;
        result += " — ";
      }
      result += st_hour+":"+st_min+" — ";
      result += event.title;
      result += "\n";
    }
  }
}

// retrieve reminders if remind
// is set to true at top
if (remind) {
  let rLists = ReminderList.getAllReminderLists();
  let remResult = "";
  for (let rList in rLists){
    if (remind == "all"){
      var rems = rLists[rList].tasks;
    }
    else if (remind == "comp") {
      var rems = rLists[rList].completeTasks;
    }
    else { // incomplete + due
      var rems = rLists[rList].incompleteTasks;
    }
    for (let rem in rems) {
      let val = rems[rem].isCompleted
      if (val == true) {
      	var symb = "- [x] ";
      }
      else {
        var symb = "- [ ] ";
      }
      let text = "";
      if (preRem = true) {
        text += rLists[rList].title+": "; 
      }
      text += rems[rem].title;
      let dueD = rems[rem].dueDate;
      if (remind == "due") {
      	if (dueD) {
          if (dueD.valueOf() >= sDate.valueOf()){
            let remDstr = dueD.getFullYear() + "-" + (dueD.getMonth()+1) + "-" + dueD.getDate();
            remResult += symb + text + " (due: " + remDstr + ")\n";
          }          
        }
      }
      else { // all incomplete tasks
        remResult += symb + text + "\n";
      }
    } // end rem in rems
  } // end rList in rLists
  if (remResult != "") {
    result += "\n## Reminders\n\n" + remResult;
  }
} // end if remind

// append to a new draft or
// create a new one, using
// append setting from top
if (append) {
  result = "\n"+result;
  draft.append(result);
  draft.addTag("agenda");
  draft.update;
}
else {
  let d = new Draft();
  d.content = result;
  d.addTag("agenda");
  d.update();
}

// function to ensure dates
// and times are padded 
// to two digits; from
// https://electrictoolbox.com/pad-number-two-digits-javascript/
function pad2(number) {
  return (number < 10 ? '0' : '') + number
}

// function to add days to
// a date, from:
// https://stackoverflow.com/questions/563406/add-days-to-javascript-date
Date.prototype.addDays = function(days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
}

3 Likes