Skip to content

Instantly share code, notes, and snippets.

@goulon
Created December 14, 2023 05:13
Show Gist options
  • Save goulon/72f93194d3e780ad147b7b85741ab581 to your computer and use it in GitHub Desktop.
Save goulon/72f93194d3e780ad147b7b85741ab581 to your computer and use it in GitHub Desktop.
AppleScript to update a busy calendar based on target calendars to show times when you're busy through a shared calendar.
on run argv
-- Check if enough arguments are provided
if (count of argv) < 5 then
display dialog "Please provide the number of days to consider, minutes of margin time, the generic busy event summary, the busy calendar name, and at least one target calendar name."
return
end if
-- Extract arguments
set daysToConsider to item 1 of argv as number
set marginTimeInMinutes to item 2 of argv as number
set eventSummary to item 3 of argv
set busyCalendarName to item 4 of argv
set targetCalendarNames to items 5 through end of argv
-- Check if the word "busy" is in the busy calendar name
if busyCalendarName does not contain "busy" then
display dialog "Warning: The name of the busy calendar does not contain 'busy'."
return
end if
tell application "Calendar"
-- Check if the busy calendar exists
if not (exists calendar busyCalendarName) then
display dialog "The specified busy calendar '" & busyCalendarName & "' does not exist."
return
end if
set busyCalendar to calendar busyCalendarName
-- Clear past events from the busy calendar
set currentDate to current date
set pastEvents to (every event of busyCalendar whose end date is less than currentDate)
repeat with anEvent in pastEvents
delete anEvent
end repeat
-- Process new and updated events in target calendars
repeat with calendarName in targetCalendarNames
set theCalendar to calendar calendarName
set theEvents to (every event of theCalendar whose start date is greater than currentDate and start date is less than currentDate + daysToConsider * days)
repeat with anEvent in theEvents
log "Processing event: " & (summary of anEvent)
set eventStartDate to start date of anEvent
set eventEndDate to end date of anEvent
-- Get the day of the week (1 = Sunday, 7 = Saturday)
set dayOfWeek to weekday of eventStartDate as integer
-- Get the event start and end hours
set startHour to (hours of eventStartDate) + ((minutes of eventStartDate) / 60)
set endHour to (hours of eventEndDate) + ((minutes of eventEndDate) / 60)
-- Check if the event is during work hours on a workday
if (dayOfWeek ≥ 2 and dayOfWeek ≤ 6) and (startHour ≥ 7 and endHour ≤ 19) then
-- Calculate start and end times with a wide margin
set startTime to eventStartDate - marginTimeInMinutes * 60
set endTime to eventEndDate + marginTimeInMinutes * 60
-- Create or update the busy event
set existingBusyEvents to (every event of busyCalendar whose start date is startTime and end date is endTime)
if (count of existingBusyEvents) is 0 then
set busyEventProperties to {summary:eventSummary, start date:startTime, end date:endTime}
log "Creating busy event: " & (summary of busyEventProperties)
make new event at end of events of busyCalendar with properties busyEventProperties
else
log "Updating existing busy event."
set firstEvent to item 1 of existingBusyEvents
set summary of firstEvent to eventSummary
set start date of firstEvent to startTime
set end date of firstEvent to endTime
end if
end if
end repeat
end repeat
end tell
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment