Is It Possible to Use KM to Delete Specific Occurrence of a Recurring Apple Calendar Event?

I am using this AppleScript but I don't know why but it does not show any error yet it is not deleting the event.

When I Googled I saw many other people were facing the same issue, so I am wondering if KM can help in any way.

Here's the script:

tell application "Calendar"
   tell calendar "Test calendar"
      set theStarttime to current date --replace with the start date of the event
      set theEndtime to current date --replace with the end date of the event
      set theSummary to "Test_event" --replace with the title of the event
      set theEventList to every event where its start date -- Grammar Police ≤ theStarttime and end date ≥ theEndtime and summary contains theSummary
      if theEventList is not {} then
         set theEvent to first item of theEventList
         delete theEvent
         save
      end if
   end tell
end tell

Keyboard Maestro doesn't work with calendars, so you're stuck with AppleScript.

Ask on the MacScripter Forum and/or the Script Debugger Forum.

1 Like

After a bit of messing around...

The UI doesn't do what you think it does. It doesn't delete one event in a recurring series, it sets it as "excluded" by adding the start date and time of that occurrence to the event's excluded dates property (which is a list).

So after setting up a 3-day recurring event, selecting the middle day, and getting the events properties

Code used to do that
tell application "Calendar"
	set defaultsReply to (do shell script "defaults read com.apple.ical SelectedEvents")
		set localUIDs to {}
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, quote}
	set resultItems to text items of defaultsReply
	set AppleScript's text item delimiters to TID
	repeat with i from 1 to (count resultItems)
		if i mod 2 = 0 then set end of localUIDs to resultItems's item i
	end repeat
	return properties of event id (item 1 of localUIDs) of calendar "Work"	
end tell

...the result is (with added line breaks to make the relevant piece obvious:

{id:"0D3D0DDB-1503-4028-BA7A-38FB5A235ED4", recurrence:"FREQ=DAILY;INTERVAL=1;UNTIL=20230406T225959Z", stamp date:date "Monday, 3 April 2023 at 19:27:30", class:event, url:missing value, end date:date "Tuesday, 4 April 2023 at 14:15:00",
excluded dates:{},
description:missing value, summary:"New Event", location:missing value, allday event:false, start date:date "Tuesday, 4 April 2023 at 13:15:00", sequence:2, status:none}

After deleting the middle event through the UI then selecting the first:

{id:"0D3D0DDB-1503-4028-BA7A-38FB5A235ED4", recurrence:"FREQ=DAILY;INTERVAL=1;UNTIL=20230406T225959Z", stamp date:date "Monday, 3 April 2023 at 19:27:30", class:event, url:missing value, end date:date "Tuesday, 4 April 2023 at 14:15:00",
excluded dates:{date "Wednesday, 5 April 2023 at 13:15:00"},
description:missing value, summary:"New Event", location:missing value, allday event:false, start date:date "Tuesday, 4 April 2023 at 13:15:00", sequence:2, status:none}

So what you need to do is get the date and start time of the occurrence you want to "delete" and add it to the event's excluded dates list. This would remove the Tuesday occurrence, now leaving only the Thursday one on the calendar:

tell application "Calendar"
	set excluded dates of event id "0D3D0DDB-1503-4028-BA7A-38FB5A235ED4" of calendar "Work" to (excluded dates of event id "0D3D0DDB-1503-4028-BA7A-38FB5A235ED4" of calendar "Work" & {date "Tuesday, 4 April 2023 at 13:15:00"})
end tell

That might be enough to get you started.

2 Likes

I don't know how to use what you mentioned here because I know nothing about apple script. The code that I pasted is the one I found on the internet. I used ChatGPT to check if code is written to do what they said it does. Then tried running.

I appreciate your efforts to try to figure things out. And I am sure it would be useful for many others on this forum in coming years. I might have followed up again about 'how to implement etc' but luckily for me now I have found an easier solution and it works in my use case.

Siri shortcut is used here. Let me share the screenshot and the shortcut:

I just make sure that name of the event is copied to the clipboard before I run this siri shortcut.

It might be useful "peek behind the curtain" for what you're doing now, as well. Because the recurring event is one single event that is then "date tested" to see whether it should happen, it's efficiently stored and accessed. That efficiency will decrease as you add more entries to the excluded dates list -- if you are using "deletion" to mark that event's macro as done you could be adding an entry daily, so you may want to do a regular clean-up of past occurrences to keep your Calendar databases nice and nippy.

I've no idea if that would even be necessary, but keep it in the back of your mind should Calendar's performance start to slow.

I'm no Shortcuts guru, so do your own checks -- but a "contains" text match always worries me. What'll happen if you want to delete "Email Nige" but you also have an event "Email Nigerian Embassy" in that calendar today?

Didn't catch you there.
The events would not exceed duration of more than a day. So end date won't be any other day. And you're right, I am going to use 'deletion' to mark as done but I would only be deleting events from current day. Also, if you remember from past comment discussions, these events trigger the actual macro. So if I delete the event, macro won't launch. And I have added event deletion macro at the end of all macros.

You're right. I missed that. Thanks for pointing that out. You're awesome.

But you aren't deleting the event... You're adding an entry to the recurring event's excluded dates property.

When Calendar checks to see if a recurring event is due it will (probably -- I'm not a Calendar engineer!) a) assess the recurrence frequency and interval to see if it is due today, and if it is then b) check the excluded dates list to see if it should instead be ignored today.

While list searching is well understood and generally efficiently done, each additional entry will still add a non-zero amount of time to each check (as well as some bytes to your Calendar database). I have no idea if that will ever become significant in actual use but if, after a year of marking a dozen daily macros "done" in this way, Calendar becomes less responsive... You'll know where to start in your clean-up.

Whoa

I am mind blown. Thanks for letting me know.

  1. Hopefully these recurring events won't be recurring for months.
  2. May be if event is recurring for long, deleting all instances of a recurring event and then create a fresh identical one help.

I am not sure about this one because when you try to delete a recurring event, it only gives you option to delete future occurrences.

Screenshot 2023-04-04 at 19.21.54

so would I have to delete old occurrences manually :thinking:

I recalled now. I used 'contains' because there is a possibility that the event name would include the '#Snoozed'.
May be I can use regex or something inside siri shortcut? idk. I guess I'll have to be mindful of naming the events. But practically these events won't be large in numbers. Max a dozen, I imagine.

That's in the future with respect to the selected occurrence -- nothing to do with "now". If you select the first event of the series you can delete them all.

You'll want to name things consistently -- so always prefixing, or always postfixing, "#Snoozed" to the macro name. That'll make things easier for you, and more efficient whenever you are doing a search.

You can use regexs in Shortcuts, but for a simple string comparison like this the built-in search options are both easier to understand and (again) more efficient -- both "begins with" and "ends with" are available.

1 Like