Creating calendar event from clipboard

Hey guys!

I am very novice when it comes to programming but I can usually figure things out given enough time, but I have been struggling with a problem for a while now and could use some help.

I am trying to write a script that will take my clipboard, which contains a few lines of event titles that i want to create an event. So far I’m able to get the clipboard content, put it into an array line by line, but when trying to create an event is where I am having issues.

Originally when i tried to run it i received the error:
“Error on line 11:16: No calendar has been set.”

So I tried to set the calendar but I cannot figure it out. Based on some stackoverflow posts and looking at the documentation for scriptable I was heading down the path of using addEvent.calendar but you can see its wrong and not working.

I’m hoping someone can help me fix the code so that it creates an event in the calendar that I choose. Thanks.


let clipboardContent = Pasteboard.paste();
let addEvent = new CalendarEvent();

calendarEvents = clipboardContent.split("\n");
console.log(calendarEvents[0]);

for (i = 0; i < calendarEvents.length; i++) {
  addEvent.title = calendarEvents[i]
  addEvent.calendar = "work"
  addEvent.save()
}

Take a look at the calendar object and the calendarEvet object in the documentation. The calendar property of the event is specified as a calendar object, not the title of a calendar object, which is what you have used above.

Thanks for the feedback. I realize how little i know about Object Oriented Programming… you’re right the calendar property is an object. So I thought I would be able to do this:
addEvent.calendar.title = “Work”;

The error I received was:
TypeError: null is not an object (evaluating ‘addEvent.calendar.title = “Work”’)

I need to sit down and do some basic learning of OOP, so I can understand it better. I feel like this should be the way it works, but I’m wrong. Any hints you can give me of how to fix this? Thanks.

Your still working from the wrong end with properties by the looks of that. You need to assign an actual object.

I think you probably (i.e. I haven’t tested this) want something like this.

addEvent.calendar = await Calendar.forEventsByTitle("work");