Search code examples
macoscalendarapplescriptosx-elcapitan

Multiple Event Alarms with Calendar and Applescript?


I'm working on a some applescript to generate and add a large number of events to my calendar many of which need multiple alarms: a "default" alarm at 9 AM and a second alarm at a time stored in a variable. However, when I run it the alarms actually created are inconsistent: sometimes I get one alarm, sometimes the other, sometimes both.

Here's the sub that handles 100% of the interaction with Calendar and a test case.

on makeCalendarEvent from {eventTitle, eventStart, eventDuration, eventDescription, eventURL, alarmTime, setDefaultAlarm}
--duration (in hours, 0= instantaneous, -1= all day)
--alarmTime (date or false)
--defaultAlarm 9AM (coded here for the moment, may move the parameter up to the var block at some point)

if eventDuration = -1 then
    set isAllDay to true
    set eventDuration to 0
else
    set isAllDay to false
end if

tell application "Calendar"
    tell calendar "test"
        set newEvent to make new event at end with properties {summary:eventTitle, start date:eventStart, end date:(eventStart + (eventDuration * hours)), allday event:isAllDay, description:eventDescription, url:eventURL}
        if alarmTime is not false then
            tell newEvent
                set alarm1 to make new sound alarm at end of sound alarms with properties {trigger date:alarmTime}
            end tell
        end if

        if setDefaultAlarm is true then
            tell newEvent
                set alarm2 to make new sound alarm at end of sound alarms with properties {trigger date:(date "09:00 AM" of eventStart)}
            end tell
        end if

    end tell

end tell

end makeCalendarEvent

makeCalendarEvent from {"New Moon", date ("Monday, February 8, 2016 at 09:39:00"), 0, "", "", date ("Monday, February 8, 2016 at 09:39:00"), true}

I don't work in Applescript all that often so it is entirely possible I've just screwed up the syntax, but I've tried everything from alternate syntaxes to adding a delay between the "tell events" to using one as sound alarm and one as a display. At this point I'm wondering if I might be too close to it, or if it's one of those quirks I'm going to have to live with or find an end-run around.

Any help would be most appreciated.


Solution

  • I have tested your script with several times and I had no issue. I get the 2 alarms. I just made some changes to optimise the script itself, but it does not change your logic. Of course, I did not play with all parameters, but even with 2 alarms very closed (last test was 8:00 and 8:05am), I still get 2 alarms !

    makeCalendarEvent from {"New Moon", date ("09/02/2016 08:05:00"), 0, "", "", date ("09/02/2016 08:05:00"), true}
    
    on makeCalendarEvent from {eventTitle, eventStart, eventDuration, eventDescription, eventURL, alarmTime, setDefaultAlarm}
    --duration (in hours, 0= instantaneous, -1= all day)
    --alarmTime (date or false)
    --defaultAlarm 9AM (coded here for the moment, may move the parameter up to the var block at some point)
    set isAllDay to (eventDuration = -1)
    if eventDuration = -1 then set eventDuration to 0
    
    tell application "Calendar"
        tell calendar "test"
            set newEvent to make new event at end with properties {summary:eventTitle, start date:eventStart, end date:(eventStart + (eventDuration * hours)), allday event:isAllDay, description:eventDescription, url:eventURL}
            tell newEvent
                if alarmTime is not false then set alarm1 to make new sound alarm at end of sound alarms with properties {trigger date:alarmTime}
                if setDefaultAlarm is true then set alarm2 to make new sound alarm at end of sound alarms with properties {trigger date:(date "08:00 AM" of eventStart)}
            end tell
        end tell
    end tell    
    end makeCalendarEvent