delete calendar events

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"))

@Carolus
When you make a event in any Calendar you could check the start date and end date property.
To be able to delete a event in your calendar you have to make sure that the time is correct.
In other words what is your events hour, minutes, seconds.

When you use the property: allday event:true in one of your script the start date and the end date
time in hours, minutes and seconds become 00:00:00. The reason for that is allday property is the hole day from midnight to midnight next day from your start date (day, month,year).

So its important to know what your variables return before you could feed a function or be
able to debug it when it doesn’t work.

The AppleScript date format could be little tricky for inputs. So we need instead to make a string
and tell AppleScript to covert it for us. To do so we set the date property before the string.

ex.

set myDate to date “27/10/22 00:00”

27 is the day
10 is the month
22 is the year
00 is the hour
00 is the minute

When the property date is before the string AppleScript know how to convert that data to a format
AppleScript could understand.

ex.

set dateString to “27/10/22 00:00”
set appleScriptDate to date dateString

or

set timeString to “”
set dateString to “27/10/22” & timeString
set appleScriptDate to date dateString

or

set timeString to “00:30:10”
set dateString to “27/10/22” & space & timeString
set appleScriptDate to date dateString

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

If you know the day and month… of your event

You could ask for the start date of the event that day… so in reality you do not have to know it.

  1. Get all events for some day and month
  2. Get the start date for whose events with specific summary
  3. delete whose events

if you run its properties in event tell block you get all that information.

Here is an example how you could get the start date from a summary.
In other words if you know the summary you could get the start date.

set targetSummary to "New Event 1"

tell application "Calendar"
	tell calendar "MyCal"
		set allEvents to every event
		repeat with anEvent in allEvents
			set theSummary to summary of contents of anEvent
			if theSummary is targetSummary then
				set theStartDate to start date of contents of anEvent
			end if
		end repeat
		return theStartDate
	end tell
end tell

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 …

To be clear in your first script in this topic you use 2 checking…start date and summary.
The start date alone have year, month, day, hours, minutes and seconds

The summary

So in your first script you hade 6 + 1 = 7 arguments to match any event.
The reason it doesn’t work for you is that any of whose is wrong.

It looks to me… you maybe have difficulty to delete a event, when it works in Monterey.