Calendar access question

question

#1
var testData = '[{"name":"Work","date":"Monday, Jan 21, 2019","start":"07:00 AM","end":"03:30 PM"},{"name":"First Break","date":"Monday, Jan 21, 2019","start":"10:00 AM","end":"10:15 AM"},{"name":"Lunch","date":"Monday, Jan 21, 2019","start":"11:45 AM","end":"12:15 PM"},{"name":"Second Break","date":"Monday, Jan 21, 2019","start":"01:45 PM","end":"02:00 PM"},{"name":"Work","date":"Wednesday, Jan 23, 2019","start":"07:00 AM","end":"03:30 PM"},{"name":"First Break","date":"Wednesday, Jan 23, 2019","start":"08:00 AM","end":"08:15 AM"},{"name":"Lunch","date":"Wednesday, Jan 23, 2019","start":"10:45 AM","end":"11:15 AM"},{"name":"Second Break","date":"Wednesday, Jan 23, 2019","start":"12:45 PM","end":"01:00 PM"}]'


function displayEvents(events) {
  let uiTable = new UITable();
  let i;
  for (i = 0; i < events.length; i++) {    
    let uiTableRow = new UITableRow();
    let event = events[i];
    console.log ('Event: ' + event.title);
    let titleCell = uiTableRow.addText(event.title, event.calendar.title);
    titleCell.widthWeight = 80;
    uiTableRow.backgroundColor = new Color(event.calendar.color.hex, event.calendar.color.alpha);
uiTableRow.height = 40;
uiTableRow.cellSpacing = 10;
uiTable.addRow(uiTableRow);
  }
  QuickLook.present(uiTable);
}

function handleEvent(myEvent) {
  let startTime = new Date(myEvent.date + " 1:00 AM")
  let endTime = new Date(myEvent.date + " 11:30 PM")
  console.log(endTime)
  var eventList = await CalendarEvent.between(startTime, endTime, calList)
  displayEvents(eventList)
}

// start main logic =================

// var shortcutInput = JSON.parse(Pasteboard.paste());
var shortcutInput = JSON.parse(testData);
console.log(shortcutInput[0]);
let calList = [];
calList.push(await Calendar.forEventsByTitle("Ryan"));
calList.push(await Calendar.forEventsByTitle("Alice Daycare"));
calList.push(await Calendar.forEventsByTitle("Calendar"));
shortcutInput.forEach(handleEvent);
// var eventList = await CalendarEvent.tomorrow(calList);
// displayEvents(eventList);

I get

SyntaxError: Unexpected identifier 'CalendarEvent'. Expected ';' after variable declaration. 

When I run this script. This is for the line in the handleEvent function. Are CalendarEvents just not allowed to be called in functions? Is that a limitation of JavaScript promises?


#2

Your code is probably perfectly fine, but I haven’t checked the whole thing.

The only problem is that you are using the await statement inside a synchronous function. You have to declare the function with the async keyword, then it should work.

Note that then this function is not being waited upon and the code after the function call is executed immediately


#3

i.e.

...
async function handleEvent(myEvent) {
...

#4

Thank you, I’ve been reading about how to handle promises but I missed that.

Thanks again.