Delete calendar events

I’m trying to change events in my Calendar using applescript.
Creating new events is no problem. However, any attempt to delete certain events in my Calendar seems to be ignored.
For instance the following code:

set TheDate to current date
tell application “Calendar”
tell calendar “BB2”
delete (events whose start date is TheDate and summary is “Birthday”)
end tell
end tell

How should I deal with this?

Dear Frederik,

thank you very much,but unfortunately, the script ignores the delete command.
In whatever setting you specified, it seems that ‘delete’ is not accepted.

I’m puzzled.

I’m sorry, but the very first line of code is not accepted.
this first line of code is:
its deleteEventsByDateAndSummary(date “Monday, 24 October 2022 at 15:00:00”, “Voorkamer”, “BB2”)

the applescript debugger says:
invalid date and time Monday 24 October at 15:00:00
even if I delete the hour (as the event is an allday event) the same error is reported:
invalid date and time Monday 24 October.

So I made a workaround:

set TheDate to current date

and subsequently adjust TheDate to the date I want to use, reading the value of cells from FileMaker:

tell application “FileMaker Pro Advanced”
tell current layout
tell current record
set TheDay to cell “datumdag”
set TheMonth to cell “datummaand”
set TheYear to cell “datumjaar”
end tell
end tell

set the day of TheDate to TheDay
set the month of TheDate to TheMonth
set the year of TheDate to TheYear

This code is accepted, but the susbequent code does nothing, at least it does not delete the event on the selected date with summary is “Voorkamer” and in the calendar with the name “BB2”

its deleteEventsByDateAndSummary(TheDate, “Voorkamer”, “BB2”)
on deleteEventsByDateAndSummary(startDate, theSummary, calendarName)
tell application “Calendar”
tell calendar calendarName
delete (events whose (start date is startDate) and (summary is theSummary))
end tell
end tell
end deleteEventsByDateAndSummary

Oh, I forgot to mention: maybe it’s because my applescript is running on MacOs 10.13 (High Sierra)?

Hi.

Looking at Carolus’s original script, the potential issues are:

  1. Start dates in Calendar are exact to the second, so unless times are critical, a way has to be found to ignore them.
  2. Birthdays potentially occur every year. It’s not clear if a particular year’s intended here or every year. If an event’s been set as a repeating event, only the start date of its first instance is recognised in Calendar’s AppleScript implementation.
  3. After AppleScript deletes an event from Calendar, it may be necessary to quit and reopen Calendar before you see the effect.

Here’s an attempt to deal with the first of these issues:

on deleteEventsOfCalendar:calendarName bySummary:theSummary andStartDate:dateParts
	tell application "Calendar"
		set theCalendar to calendar calendarName
		set {summaries, startDates, ids} to {summary, start date, uid} of theCalendar's events
	end tell
	
	repeat with i from 1 to (count summaries)
		if ((item i of summaries = theSummary) and ({year, month, day, time} of item i of startDates contains dateParts)) then
			tell application "Calendar" to delete event id (item i of ids) of theCalendar
		end if
	end repeat
end deleteEventsOfCalendar:bySummary:andStartDate:

set calendarName to "BB2"
set theSummary to "Birthday"
-- The following list must contain values from an AppleScript date:
-- ie. the date's {year, month, day, time}. Values to be ignored can
-- be omitted, but the rest must be in the above order.
set dateParts to (current date)'s {month, day}

my deleteEventsOfCalendar:calendarName bySummary:theSummary andStartDate:dateParts

Thanks Nigel for your elaborate answer.
However, it seems that my mac consistently refuses any type of delete command.

To clarify, my agenda has on each day zero or just one event, which takes all day.
So, events are created as follows:

set EventU to “unavailable”
set TheDate to current date
tell calendar “One”
make new event at end with properties {summary:EventU, start date:TheDate, end date:TheDate, allday event:true}
end tell

this works.
But the opposite:
tell calendar “One”
delete (events whose summary equals EventU and start date is TheDate)
end tell

does not work
even the extended command:
delete (events whose summary equals EventU and start date is TheDate and end date is TheDate and allday event is true)
does not work.

Did you note my point 3 above about quitting and reopening Calendar after running your script? On my Mac, Calendar doesn’t update its display immediately when events are deleted by AppleScript. It has to be quit and then reopened before the event(s) can be seen to be gone.

I’ve altered the script, which now quits the Calendar and subsequently activates the Calendar again.
But to no avail.
All events that are created in my Calendar are allday events
And I do not understand.
I’ve even made new events only with a start date and summary, but the result is the same.
Any delete effort is ignored.
code:

set TheDate to current date

tell application “FileMaker Pro Advanced”
tell current layout
tell current record
set TheDay to cell “datumdag”
set TheMonth to cell “datummaand”
set TheYear to cell “datumjaar”
set NaamVoor to cell “naamVK”
set NaamAchter to cell “naamAK”
set NaamTuin to cell “naamTK”
end tell
end tell
end tell

-- this causes whatever the current date is, to adjust TheDate to the values selected in the cells of the record of FMP

set the day of TheDate to TheDay
set the month of TheDate to TheMonth
set the year of TheDate to TheYear

set EventU to “unavailable”

tell application “Calendar”
activate
tell calendar “BBVK”
make new event at end with properties {summary:EventU, start date:TheDate, allday event:true}
end tell
tell calendar “BBAK”
make new event at end with properties {summary:EventU, start date:TheDate, allday event:true}
end tell
tell calendar “BBTK”
make new event at end with properties {summary:EventU, start date:TheDate, allday event:true}
end tell
end tell

tell application “Calendar”
quit
end tell

tell application “Calendar”
activate
tell calendar “BBVK”
delete events whose (start date is TheDate) and (summary is EventU)
end tell
end tell
– this should delete the event which was just made.

tell application “FileMaker Pro Advanced”
activate
end tell

grrrrrrr

Hi Carolus.

  1. Try deleting the events before quitting and reopening Calendar. :slight_smile:

  2. It would be good if you could wrap your AppleScript code in MacScripter’s [applescript] and [/applescript] tags when posting here. They make the code appear in a box with a clickable link, as in Fredrik71’s posts and mine. There’s a button for them above the text field when you post. Thanks.

Dear Nigel and Fredrik,
Thanks. I’ve made the script like Fredrik suggested.
First error was: cannot make a date from
start date is (date "Thursday etc
So, I had to make an adjustment: I deleted the word date before Thursday:


tell calendar "MyCal"
       delete (events whose (start date is ("Thursday, 27 October 2022 at 00:00:00")) and (summary is "MyEvent"))

That code is accepted.
However, if I run the script, one would expect that a new calendar is created with the name MyCal.
That does not happen.
In stead, no new calendar is created but the subsequent action (create event with the name MyEvent) IS created, but in one of the current calendars.
The additional delete action can not be executed. It says:
Calendar got an error:
“Thursday, 2y October 2022 at 00:00:00”
can not be converted into type date
Unknown error -1700

So, it seems that the issue IS how to address the date setting, where the details of the date are stored in separate FileMaker cells.
FM contains separate cells for: day, month, year, hours, minutes, seconds

Hi Carolus.

“Thursday, 27 October 2022 at 00:00:00” is text.
date “Thursday, 27 October 2022 at 00:00:00” is a date specifier. You don’t get a date object unless you keep the date keyword. However, in AppleScript, the text part of a date specifier has to have the same format as the user preferences set on your own computer. Are your preferences, by any chance, something other than day-month-year and 24-hour time? If so, the date text in the script needs to be rearranged to match your user preferences.

Another thing that occurs to me — although this isn’t happening on my own Mac — is that, since you have the date specifier in the tell block for Calendar, Calendar may be trying to resolve the specifier itself and being unable to do so. You could try setting a variable to the date and using that inside the tell block:

set theDate to date "Thursday 27 October 2022 at 00:00:00" -- Edit to your own format if necessary.
tell application "Calendar"
	tell calendar "MyCal"
		delete (events whose start date is theDate and summary is "MyEvent")
	end tell
end tell

Dear Nigel,
I see. I’ve changed the data specifier in Dutch - the language set on my computer.
so after the make new event command


   tell calendar "BBVK"
       make new event at end with properties {start date:TheDate, end date:TheDate, summary:"unavailable", allday event:true}
   end tell

in which TheDate is first set to current date and subsequently, day, month, year, hours, minutes and seconds are read from the cells in FileMakerPro
This results into a new event in calendar BBVK on whatever date I’ve selected in FMP.
For this test I’ve selected monday 24 october 2022 in FMP

But then …


  tell calendar "BBVK"
       delete (events whose (start date is (date "Maandag, 24 oktober 2022")) and (summary is "unavailable"))
  end tell

This command is ignored. No error, but nothing happens.
The same if I use;


       delete (events whose (start date is (date "Maandag, 24 oktober 2022 om 00:00:00")) and (summary is "unavailable"))

or


      delete (events whose (start date is (date "Maandag, 24 oktober 2022 om 0 uur")) and (summary is "unavailable"))

Dear Fredrik,
I’m … exhausted.
I understand that the format of the time of an event is crucial.
I have made cells in (records of) FMP which contain the separate parts of a date including time.
These parts are put in the date i want to use to change, create or delete an event.
And each and every event in my agenda is an allday event. So by definition, the time of any event should be 00:00:00
That’s why I have:


tell application "FileMaker Pro Advanced"
  tell current layout
    tell current record
	set MyDay     to cell "datumdag"
	set MyMonth to cell "datummaand"
	set MyYear    to cell "datumjaar"
	set MyHour	to cell "datumtijduren"
	set MyMinutes	to cell "datumtijdminuten"
	set MySeconds	to cell "datumtijdseconden"
    end tell
  end tell
end tell

Subsequently:


set the day of TheDate to MyDay
set the month of TheDate to MyMonth
set the year of TheDate to MyYear
set the time of TheDate to MyHour

I noted that the date format is dayname, number, nameofmonth, year
I’ve changed that notation format in FMP, so last Monday would be presented as:
Maandag 24 oktober 2022 (as my system preferences are in Dutch).
That is OK.
Creating an event with these properties is no problem.
But beyond that I’m lost.
The events in the Calendar, apparently, are created on a certain time, although the calendar itself notes that it’s an allday event.
I can’t possibly know the exact times of all the events in my calendars.
And I do not know how to change the time of certain event into 00:00:00

Hi Carolus.

My script in post #13 makes that unnecessary, but may need reworking for your particular requirements. At the moment, I’m not clear what your requirements are. Presumably your end goal isn’t to create calendar events simply to delete them again immediately in the same script.

You’re OK creating events from the data in your FileMaker database, so that’s no problem.

When you delete events, do you want the deletion script to get the event date from the current FileMaker record or do you want to type the date into a dialog when you run the script? If you make this clear, I’ll try to adapt my script accordingly.

Dear Nigel and Fredrik,
Firstly, thanks for your insisting support.
Secondly, let me shortly describe my little system.

I’ve made a reservation system in FMP: that works fine.
Incoming requests, checking wether something is available (or not) and emailing the person who is asking info about a reservation, is handled in this FMP dbase using the internal scripting option in which - if necessary - I’ve included the scriptstep: Applescript (native).

That is where the Agenda comes in.

The availability (when reservations are made - or cancelled or changed) in the FMP dBase on my local mahine, should be synchronized with an external agenda. I’ve made that agenda - and again that works fine.

The issue is, that until now I have to synchronize the FMP database manually with the Agenda.
That’s is why I would like to include a separate FMP script which is run if a reservation is made or changed on a particular date.
That script includes reading the necessary data in FMP en subsequently tries to locate in the Agenda whether this reservation has to be created, changed or deleted. And these final steps are Applescript steps.

Hi Carolus.

Is this anything like what you need? I haven’t included the code for creating the events, but have included setting the date object for this as it’s useful in both processes.

I’d be grateful if you’d set up suitable events and try this script as it is in order to make sure it works before incorporating it into your own script.

tell application "FileMaker Pro Advanced"
	tell current layout
		tell current record
			set TheDay to cell "datumdag"
			set TheMonth to cell "datummaand"
			set TheYear to cell "datumjaar"
			set NaamVoor to cell  "naamVK"
			set NaamAchter to cell  "naamAK"
			set NaamTuin to cell  "naamTK"
		end tell
	end tell
end tell

-- Set an AppleScript date for creating event(s). The time's irrelevant with all-day events.
-- When changing a date's properties to make another date,
-- its day should be set to a value below 29 first to avoid possible overflows
-- into the following month with the intermediate mixed settings.
set theDate to (current date)
tell theDate to set {its day, its year, its month, its day} to {1, MyYear, MyMonth, MyDay}

-- Get the date's year, month, and day values to use for identifying existing events.
set dateParts to theDate's {year, month, day}

-- Set the summary for which to search.
set eventU to "unavailable"

-- Delete matching event(s) from the calendar(s).
-- I've assumed that NaamVoor, NameAchter, and NaameTuin contain the calendars' names.
set calendarName to naamVoor
-- Alternatively, uncomment the following repeat/end repeat lines to delete events from all three calendars.
-- repeat with calendarName in {naamVoor, NameAchter, NaameTuin}
tell application "Calendar"
	set theCalendar to calendar calendarName
	set {summaries, startDates, ids} to {summary, start date, uid} of theCalendar's events
end tell

repeat with i from 1 to (count summaries)
	if ((item i of summaries = eventU) and ({year, month, day} of (get item i of startDates) = dateParts)) then
		tell application "Calendar" to delete event id (item i of ids) of theCalendar
	end if
end repeat
-- end repeat

-- If necessary (and if it works!), quit and reopen Calendar in order to update its display.
tell application "Calendar" to quit
tell application "System Events"
	repeat while (application process "Calendar" exists)
		delay 0.2
	end repeat
end tell
tell application "Calendar" to activate

Dear Nigel, thanks again.
Maybe I’ve not clarified that I have several Calendars which have on each and every day no event or just one event. Sort of binary system.
So the whole repeat commands are - I guess - not necessary.
Based on the info I read in FMP, I have to create in the appropriate Calendar an event or delete the one wich is already there.

Well, I tried and tried but it does not work. The bottle neck is - still - in the delete command.
I’ve named that particular event: “summaryofevent” and it is present in calendar CalGreen.
Though I do not get an error message, and the whole script is performed, the event “summaryofevent” is not deleted:


tell application "Calendar"
	set theCalendar to calendar CalGreen
	set {summaries, startDates, ids} to {summary, start date, uid} of theCalendar's events

	repeat with i from 1 to (count summaries)
		if ((item i of summaries = "summaryofevent") and ({year, month, day} of (get item i of startDates) equals dateParts)) then
			delete event id (item i of ids) of calendar CalGreen
		end if
	end repeat
end tell

Annoying …

Hello,

I thought I just reply to this thread rather than starting my own as the topic is so similar but the sysggestions here did not help in my case.

I am trying to write a script that copies events form one calendar (a synced Kerio calendar) in iCal to another (a synced Google calendar) so that I can easily see the events in the Kerio calendar on my Android phone (annoyingly you can’t sync a Kerio Calendar with Google Calendar and the Kerio app does not work on my phone).

I have the following script which did work but now fails to delete events in the destination Google Calendar:

 set theStartDate to (current date) - (8 * days)
 set hours of theStartDate to 0
 set minutes of theStartDate to 0
 set seconds of theStartDate to 0
 set theEndDate to (current date) + (29 * days) - 1

 set sourceCalendarTitle to "Kerio"
 set destinationCalendarTitle to "KerioSync"

tell application "Calendar"
	set destinationCalendar to (first calendar where its title = destinationCalendarTitle)
	set sourceCalendar to (first calendar where its title = sourceCalendarTitle)
	
	tell destinationCalendar
		set calendarEvents to (every event where its start date is greater than or equal to theStartDate and end date is less than or equal to theEndDate)
		set numberOfEvents to count calendarEvents
		repeat with i from 1 to numberOfEvents
			set anEvent to item i of calendarEvents
			delete anEvent
		end repeat
	end tell
	
	tell sourceCalendar
		set sourceEvents to (every event where its start date is greater than or equal to theStartDate and end date is less than or equal to theEndDate)
		repeat with i from 1 to count sourceEvents
			set anEvent to item i of sourceEvents
			set tmpDescription to description of anEvent
			if tmpDescription = missing value then
				set tmpDescription to ""
			end if
			
			if recurrence of anEvent = missing value then
				make new event at destinationCalendar with properties {summary:summary of anEvent, start date:start date of anEvent, end date:end date of anEvent, description:tmpDescription, allday event:allday event of anEvent}
			end if
		end repeat
	end tell
end tell

Here is an example error:

error "Calendar got an error: Failed to delete event [EKEvent <0x243abc0>, title:Fly to Poland, localUID:F4140221-9FB2-4386-96A1-033AB8924F49, sharedUID:7BB917BC-AD2D-460B-96B4-C93A8D605A96] with error [{
    NSLocalizedDescription = \"An unexpected error occurred.\";
}]" number -10025

I am running macOS 12.6.6 (21G646).

Any help would be very much appreciated.

Thanks,
Ben

One of the reasons that some event cannot be deleted may be that if the calendar is not editable (not yours, linked from somewhere, hosted on other computer).

If the calendar is not editable, you should create local copy of it. Then, you can delete the original calendar & edit its local copy (delete its events as well).