use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "EventKit"
set startDate to current application's NSDate's |date|()
set theResult to its deleteEventFromCalendar:"Work" eventLocation:"Using applescript"
return {theResult, -(startDate's timeIntervalSinceNow())}
on deleteEventFromCalendar:calName eventLocation:theLocation
-- create event store and get the OK to access Calendars
set theEKEventStore to current application's EKEventStore's alloc()'s init()
theEKEventStore's requestAccessToEntityType:0 completion:(missing value)
-- check if app has access; this will still occur the first time you OK authorization
set authorizationStatus to current application's EKEventStore's authorizationStatusForEntityType:0 -- work around enum bug
if authorizationStatus is not 3 then
display dialog "Access must be given in System Preferences" & linefeed & "-> Security & Privacy first." buttons {"OK"} default button 1
tell application "System Preferences"
activate
tell pane id "com.apple.preference.security" to reveal anchor "Privacy"
end tell
error number -128
end if
-- get calendars that can store events
set theCalendars to theEKEventStore's calendarsForEntityType:0
-- get the calendars whose title is in the passed list of names
set theNSPredicate to current application's NSPredicate's predicateWithFormat_("title == %@", calName)
set calsToSearch to theCalendars's filteredArrayUsingPredicate:theNSPredicate
-- create start date and end date for occurences
set nowDate to current application's NSDate's |date|()
-- get start of day then end of day
set startNSDate to current application's NSCalendar's currentCalendar()'s startOfDayForDate:nowDate
set endNSDate to current application's NSDate's dateWithTimeInterval:(1 * days - 1) sinceDate:startNSDate
-- find matching events
set thePred to theEKEventStore's predicateForEventsWithStartDate:startNSDate endDate:endNSDate calendars:calsToSearch
set theEvents to (theEKEventStore's eventsMatchingPredicate:thePred)
repeat with i from 1 to count of theEvents
if ((item i of theEvents)'s location()'s isEqualToString:theLocation) as boolean then
(theEKEventStore's removeEvent:(item i of theEvents) span:1 commit:true |error|:(reference))
return true
exit repeat
end if
end repeat
return false
end deleteEventFromCalendar:eventLocation: