hs.eventkit
ModuleRead calendar events, and read and change reminders, from the macOS Calendar and Reminders databases (EventKit).
Events need full access to calendars: call hs.permissions.requestCalendars() to ask for it,
and hs.permissions.checkCalendars() to see whether it has been granted. Reminders need access
to reminders, from hs.permissions.requestReminders() and hs.permissions.checkReminders().
Every method here throws (or, for one that returns a Promise, rejects) while the access it
needs hasn't been granted.
Events are HSCalendarEvent objects, and reminders are HSReminder objects.
Finding meeting links for today
hs.eventkit.eventsToday().forEach(e => {
const text = [e.location, e.notes, e.url].join(" ")
const link = text.match(/https:\/\/[\w.-]*(zoom\.us|teams\.microsoft\.com)\/\S+/)
if (link) console.log(e.title + ": " + link[0])
})
Adding a reminder and ticking it off
const r = hs.eventkit.createReminder({
title: "Water the plants",
dueDate: new Date(Date.now() + 3600000),
alarmDates: [new Date(Date.now() + 3600000)]
})
// ...later
r.update({ isCompleted: true })
Types
This module provides the following types:
Object Types
Plain JavaScript objects that this module's methods and properties take or return:
HSReminderChanges
Properties of a reminder to set, for HSReminder.update() and hs.eventkit.createReminder()
| Field | Type | Description |
|---|---|---|
| title optional | string | The reminder's title |
| notes optional | string | null | Its notes, or null to clear them |
| url optional | string | null | Its URL, or null to clear it |
| dueDate optional | Date | null | When it's due, or null to clear it (which also clears isAllDay). Keeps the reminder's isAllDay unless that's set too |
| isAllDay optional | boolean | true makes the reminder due on its due date's day, rather than at its time. Needs a due date |
| priority optional | "none" | "low" | "medium" | "high" | Its priority |
| isCompleted optional | boolean | Whether it's done, which also sets or clears completionDate |
| alarmDates optional | Date[] | When to alert about it, replacing its timed alerts; [] removes them |
| listID optional | string | The ID of the list to put it in |
Properties
This module has no properties.
Methods
hs.eventkit.calendars() -> object[]
The calendars that hold events.
Each entry is an object with id, title and source (the account, e.g. "iCloud") keys.
hs.eventkit.calendars() -> {id: string, title: string, source: string | null}[]
| Field | Type | Description |
|---|---|---|
| id | string | |
| title | string | |
| source | string | null |
try {
hs.eventkit.calendars().forEach(c => console.log(c.source + ": " + c.title))
} catch (err) {
console.error(err.message)
}
hs.eventkit.events(start, end, calendarIDs) -> HSCalendarEvent[]
The events that overlap a date range, sorted by start date.
EventKit only searches up to four years at a time; a longer range is cut short.
hs.eventkit.events(start, end, calendarIDs) -> HSCalendarEvent[]
| Name | Type | Description |
|---|---|---|
| start | Date | The start of the range |
| end | Date | The end of the range |
| calendarIDs optional | [String] | The ids of the calendars to search (default: all of them) |
try {
const now = new Date()
const later = new Date(now.getTime() + 3600000)
hs.eventkit.events(now, later).forEach(e => console.log(e.title))
} catch (err) {
console.error(err.message)
}
hs.eventkit.search(query, start, end, calendarIDs) -> HSCalendarEvent[]
The events in a date range whose title, location, notes or URL contain some text (ignoring case), sorted by start date.
hs.eventkit.search(query, start, end, calendarIDs) -> HSCalendarEvent[]
| Name | Type | Description |
|---|---|---|
| query | string | The text to look for |
| start | Date | The start of the range |
| end | Date | The end of the range |
| calendarIDs optional | [String] | The ids of the calendars to search (default: all of them) |
try {
const now = new Date()
const nextWeek = new Date(now.getTime() + 7 * 86400000)
hs.eventkit.search("zoom.us", now, nextWeek).forEach(e => console.log(e.title))
} catch (err) {
console.error(err.message)
}
hs.eventkit.eventsToday(calendarIDs) -> HSCalendarEvent[]
The events that overlap today (from midnight to midnight, including any that have already finished), sorted by start date.
hs.eventkit.eventsToday(calendarIDs) -> HSCalendarEvent[]
| Name | Type | Description |
|---|---|---|
| calendarIDs optional | [String] | The ids of the calendars to search (default: all of them) |
try {
hs.eventkit.eventsToday().forEach(e => console.log(e.startDate.toLocaleTimeString() + " " + e.title))
} catch (err) {
console.error(err.message)
}
hs.eventkit.eventsTomorrow(calendarIDs) -> HSCalendarEvent[]
The events that overlap tomorrow (from midnight to midnight), sorted by start date.
hs.eventkit.eventsTomorrow(calendarIDs) -> HSCalendarEvent[]
| Name | Type | Description |
|---|---|---|
| calendarIDs optional | [String] | The ids of the calendars to search (default: all of them) |
try {
console.log(hs.eventkit.eventsTomorrow().length + " events tomorrow")
} catch (err) {
console.error(err.message)
}
hs.eventkit.reminderLists() -> object[]
The lists that hold reminders.
Each entry is an object with id, title, source (the account, e.g. "iCloud") and
isDefault (whether new reminders go there unless told otherwise) keys.
hs.eventkit.reminderLists() -> {id: string, title: string, source: string | null, isDefault: boolean}[]
| Field | Type | Description |
|---|---|---|
| id | string | |
| title | string | |
| source | string | null | |
| isDefault | boolean |
try {
hs.eventkit.reminderLists().forEach(l => console.log(l.source + ": " + l.title))
} catch (err) {
console.error(err.message)
}
hs.eventkit.reminders(listIDs) -> Promise<HSReminder[]>
All reminders, completed or not.
Sorted like incompleteReminders().
hs.eventkit.reminders(listIDs) -> Promise<HSReminder[]>
| Name | Type | Description |
|---|---|---|
| listIDs optional | [String] | The ids of the lists to search (default: all of them) |
hs.eventkit.reminders().then(r => console.log(r.length + " reminders"))
hs.eventkit.incompleteReminders(listIDs) -> Promise<HSReminder[]>
The reminders that haven't been completed, sorted by due date (those without one last), then by when they were created.
hs.eventkit.incompleteReminders(listIDs) -> Promise<HSReminder[]>
| Name | Type | Description |
|---|---|---|
| listIDs optional | [String] | The ids of the lists to search (default: all of them) |
hs.eventkit.incompleteReminders().then(reminders => {
reminders.forEach(r => console.log(r.title + (r.dueDate ? " (due " + r.dueDate.toLocaleString() + ")" : "")))
})
hs.eventkit.completedReminders(start, end, listIDs) -> Promise<HSReminder[]>
The reminders that were completed during a date range, sorted by when they were completed.
hs.eventkit.completedReminders(start, end, listIDs) -> Promise<HSReminder[]>
| Name | Type | Description |
|---|---|---|
| start | Date | The start of the range |
| end | Date | The end of the range |
| listIDs optional | [String] | The ids of the lists to search (default: all of them) |
const weekAgo = new Date(Date.now() - 7 * 86400000)
hs.eventkit.completedReminders(weekAgo, new Date()).then(r => console.log(r.length + " done this week"))
hs.eventkit.reminder(id) -> HSReminder
The reminder with an identifier, such as one saved from a reminder's id earlier.
hs.eventkit.reminder(id) -> HSReminder
| Name | Type | Description |
|---|---|---|
| id | string | The reminder's id |
null if there isn't one with that idtry {
const r = hs.eventkit.reminder(hs.settings.get("reminderID"))
if (r && !r.isCompleted) console.log("Still to do: " + r.title)
} catch (err) {
console.error(err.message)
}
hs.eventkit.createReminder(properties) -> HSReminder
Creates and saves a new reminder.
properties must have a title, and can have any of the other keys HSReminder.update()
takes. Without a listID, the reminder goes in the default list for new reminders.
A due date doesn't produce a notification by itself; add alarmDates for that.
hs.eventkit.createReminder(properties) -> HSReminder
| Name | Type | Description |
|---|---|---|
| properties | HSReminderChanges & {title: string} | The new reminder's properties |
try {
const tomorrow = new Date(Date.now() + 86400000)
hs.eventkit.createReminder({ title: "Call the dentist", dueDate: tomorrow, isAllDay: true, priority: "high" })
} catch (err) {
console.error(err.message)
}