iCal delete event if exists

Hello all,

I have been struggling with this for a while now and would appreciate some help!

I have an iCal calendar which I populate via an applescript from email messages. This works fine, but when an event is updated, I would like my script to delete any existing events of the same summary.


tell application "iCal"
	tell calendar "MyCalendar"
		set theEvent to first event whose summary contains "Job: 18031"
		if exists theEvent then
			delete theEvent
		end if
	end tell
end tell

The above script works perfectly if their is an event with “Job: 18031” but if not I get an AppleScript error: The variable theEvent is not defined.

Help, will be much appreciated!

Hi,

if no event is found with the boolean filter, the variable won’t be defined at all.
And if some event is found, it logically exist :wink:
You can delete the first found event easier with

tell application "iCal"
	tell calendar "MyCalendar"
		delete (first event whose summary contains "Job: 18031")
	end tell
end tell

Note: consider, that the order of events is not sorted in any way

StefanK,

Works beautifully, thank you very much!

Ideally it would delete all events. (Which I didn’t state in my original post - sorry!)

Kind regards,

Rich

For all events with the same summary use:

tell application "iCal"
	tell calendar "MyCalendar"
		delete (events whose summary contains "Job: 18031")
	end tell
end tell

Thank you once again StefanK, much appreciated!