Files
homelab/calendar-sync.applescript
2025-10-19 17:02:16 -05:00

161 lines
5.0 KiB
AppleScript

(*
Calendar Sync Script
Synchronizes calendar events between two calendar accounts for the current day.
Reads events from a source calendar and mirrors them to a destination calendar,
including removal of events that no longer exist in the source.
*)
-- Configuration Properties
property sourceAccountName : "Work Account"
property sourceCalendarName : "Main Calendar"
property destinationAccountName : "Personal Account"
property destinationCalendarName : "Synced Events"
property enableLogging : true
property enableDebugMode : false
-- Logging levels
property LOG_ERROR : 1
property LOG_WARNING : 2
property LOG_INFO : 3
property LOG_DEBUG : 4
-- Global variables for tracking sync state
global syncResults
global errorList
-- Initialize sync results record
on initializeSyncResults()
set syncResults to {eventsCreated:0, eventsUpdated:0, eventsRemoved:0, eventsSkipped:0, errors:{}}
set errorList to {}
end initializeSyncResults
-- Main entry point
on run
try
-- Initialize logging and sync tracking
initializeSyncResults()
logMessage("Starting Calendar Sync", LOG_INFO)
-- Display configuration
logMessage("Source: " & sourceAccountName & " -> " & sourceCalendarName, LOG_INFO)
logMessage("Destination: " & destinationAccountName & " -> " & destinationCalendarName, LOG_INFO)
-- TODO: Implement main sync workflow
logMessage("Calendar sync setup complete", LOG_INFO)
on error errorMessage number errorNumber
handleError("Main execution error: " & errorMessage, errorNumber)
end try
end run
-- Logging and Error Handling Framework
-- Log a message with specified level
on logMessage(message, level)
if not enableLogging then return
set levelText to getLevelText(level)
set timestamp to (current date) as string
set logEntry to "[" & timestamp & "] " & levelText & ": " & message
-- Display to user (can be modified to write to file if needed)
if level LOG_WARNING or enableDebugMode then
display notification message with title "Calendar Sync " & levelText
end if
-- Always log errors and warnings to console
if level LOG_WARNING then
log logEntry
else if enableDebugMode then
log logEntry
end if
end logMessage
-- Get text representation of log level
on getLevelText(level)
if level = LOG_ERROR then
return "ERROR"
else if level = LOG_WARNING then
return "WARNING"
else if level = LOG_INFO then
return "INFO"
else if level = LOG_DEBUG then
return "DEBUG"
else
return "UNKNOWN"
end if
end getLevelText
-- Handle errors with logging and user feedback
on handleError(errorMessage, errorNumber)
set fullErrorMessage to errorMessage & " (Error " & errorNumber & ")"
-- Log the error
logMessage(fullErrorMessage, LOG_ERROR)
-- Add to error list for reporting
set end of errorList to fullErrorMessage
-- Display user-friendly error dialog
display dialog "Calendar Sync Error: " & errorMessage buttons {"OK"} default button "OK" with icon stop
end handleError
-- Display progress to user
on displayProgress(currentItem, totalItems, itemDescription)
if not enableLogging then return
set progressPercent to round ((currentItem / totalItems) * 100)
set progressMessage to "Processing " & itemDescription & " (" & currentItem & " of " & totalItems & " - " & progressPercent & "%)"
logMessage(progressMessage, LOG_INFO)
end displayProgress
-- Display final sync summary
on showSyncSummary()
set summaryMessage to "Sync Complete!" & return & return
set summaryMessage to summaryMessage & "Events Created: " & (eventsCreated of syncResults) & return
set summaryMessage to summaryMessage & "Events Updated: " & (eventsUpdated of syncResults) & return
set summaryMessage to summaryMessage & "Events Removed: " & (eventsRemoved of syncResults) & return
set summaryMessage to summaryMessage & "Events Skipped: " & (eventsSkipped of syncResults) & return
if (count of errorList) > 0 then
set summaryMessage to summaryMessage & return & "Errors encountered: " & (count of errorList)
end if
logMessage(summaryMessage, LOG_INFO)
display dialog summaryMessage buttons {"OK"} default button "OK" with icon note
end showSyncSummary
-- Validate configuration before starting sync
on validateConfiguration()
logMessage("Validating configuration...", LOG_DEBUG)
-- Check that required properties are set
if sourceAccountName = "" or sourceCalendarName = "" then
handleError("Source calendar configuration is incomplete", -1)
return false
end if
if destinationAccountName = "" or destinationCalendarName = "" then
handleError("Destination calendar configuration is incomplete", -2)
return false
end if
logMessage("Configuration validation passed", LOG_DEBUG)
return true
end validateConfiguration
-- Utility function to get current date for today's events
on getCurrentDate()
return current date
end getCurrentDate
-- Utility function to create date range for current day
on getCurrentDayRange()
set today to getCurrentDate()
set startOfDay to date (short date string of today)
set endOfDay to startOfDay + (24 * hours) - 1
return {startDate:startOfDay, endDate:endOfDay}
end getCurrentDayRange