Search code examples
eventscalendarapplescripticloudicalendar

Find event in specified calendar in AppleScript


Hallo I am pretty new to AppleScript. I try to create attendance generator via pure AppleScript (I know I would use Microsoft Excel VBA for this I wan to use AppleScript only.).

I have this kind of problem. I want to check if in current month are or are not some national holidays (calendar called "Státní svátky"). I run my code and get empty e.g., {} variable. Don't know what is wrong in my code.

-- My isholiday function; i parameter is pointer to day of month i want to test if it is or it is not a holiday
on isholiday(i)

    -- Function global variables
    set today to current date
    set tomorrow to today + 60 * 60 * 24

    -- work with calendar app
    tell application "Calendar"
        tell calendar "Státní svátky" -- national holiday calendar in my iCloud 
            set currentEvent to every event whose start date is greater than or equal to today and start date is less than or equal to tomorrow
            return currentEvent

            -- when an event occurs it should tells me right
            if currentEvent is not {} then
                display dialog "It is not empty!"
            end if
        end tell
    end tell
end isholiday

-- caling the function - i picked 23 for purpose - I did testing event for that day e.g., i know it is not 100% empty isholiday(23)

Please help me. Thank you!

-- Michael


Solution

  • Calendars that have recurring events often don't use the current year's start date. I would check the start and end dates with this...

    tell application "Calendar"
        tell calendar "Státní svátky" -- national holiday calendar in my iCloud 
            return start date of events whose summary = "a holiday name"
        end tell
    end tell
    

    EDIT
    Once you have confirmed that the start date are correct, you can use something along these lines...

    tell application "Calendar"
        tell calendar "Státní svátky" -- national holiday calendar in my iCloud 
            set today to current date
            set today's time to 0
            copy (today + days) to tomorrow
            set currentEvents to events whose start date ≥ today and end date ≤ tomorrow
        end tell
    end tell