Problem deleting Calendar event

Hi All,

I would like to use apple script to delete callendar events whit a certain date. Been reading last of websites on how to do this, foudn and used several suggested syntax but I keep getting the error when I try to delete an event: “error “Calendar got an error: Can’t make "7C573D4F-4BCD-4E94-A8FE-2E4FA30FAF14" into type specifier.” number -1700 from “7C573D4F-4BCD-4E94-A8FE-2E4FA30FAF14” to specifier”

So this is what I got: I create a list which contains al the events on the given date. I extract the Uid of the first event and use the command delete to delete the event. But that’s where I get the error.

What am I doing wrong?

Thanks for the help,

Maarten


tell application "Calendar"
  tell calendar "test"
    set datum to date "Saturday 10 October 2015 00:00:00"
    set theEventList to every event whose start date is equal to datum
 
    set theEvent to first item of theEventList
    set EventID to uid of theEvent
    delete EventID
 
  end tell
end tell

Hi. Your code is trying to delete a string. Use the event ID specifier.

set datum to date "Saturday 10 October 2015 00:00:00"

tell application "Calendar"
	tell calendar "test" to delete event id (((events whose start date is datum)'s item 1)'s uid)
end tell

Hi Marc,

Your code did the trick. Being a total newly on apple script. I really appreciate the help!

One more question: now only item 1 of the events is deleted. But could be there are more items on that date. Could you give me a hint once more how to solve that?

Thanks, Maarten

You originally asked only for the first uid, but you could avoid that method altogether, actually. This gets all events:.

	tell calendar "test" to delete (events whose start date is datum)

Thanks again Marc,

Although here this second code of yours also deletes only one event. I don’t know why.

And I found out that sometimes both codes you send me don’t work. But this was solved by activating Calendar.

I’m puzzled.
As I wanted to test the given script, I ran :

"10/10/2015 00:00:00"
set datum to date result
log datum --> date "samedi 10 octobre 2015 00:00:00"
tell application "Calendar"
	activate
	tell calendar "Test"
		events whose start date is datum
		log result
		(*event id 5488C5A2-E889-4A7B-9B59-0ED33109C0BD of calendar id 15796FB4-0796-43DD-ACD4-C41067C1012D, event id 416C98D5-E3BF-4BFD-A8DC-1FC7BB31AAD1 of calendar id 15796FB4-0796-43DD-ACD4-C41067C1012D*)
		
			delete (events whose start date is datum)
		
	end tell
end tell

Yes, I am lazy and don’t want to type the long date.
As you see in the comments, the date is correctly built, the script found two events, the delete instruction is correctly issued.
Alas, the two events are always displayed in the calendar.
I ran the script a second time. This time, it reported only one event: (event id 5488C5A2-E889-4A7B-9B59-0ED33109C0BD of calendar id 15796FB4-0796-43DD-ACD4-C41067C1012D) and both events are always displayed.
Of course, when I ran the script a third time, it saw no event and the delete instruction issued an error.
I quitted Calendar then ran this extended version of the script

"10/10/2015 00:00:00"
set datum to date result
log datum --> date "samedi 10 octobre 2015 00:00:00"
tell application "Calendar"
	activate
	tell calendar "Test"
		set theEvents to events whose start date is datum
		log result
		(*event id 5488C5A2-E889-4A7B-9B59-0ED33109C0BD of calendar id 15796FB4-0796-43DD-ACD4-C41067C1012D, event id 416C98D5-E3BF-4BFD-A8DC-1FC7BB31AAD1 of calendar id 15796FB4-0796-43DD-ACD4-C41067C1012D*)
		if theEvents is not {} then
			repeat with anEvent in theEvents
				log (get properties of anEvent)
				delete anEvent
			end repeat
		end if
	end tell
end tell

The events log was :
(date samedi 10 octobre 2015 00:00:00)
tell application “Calendar”
activate
get every event of calendar “Test” whose start date = date “samedi 10 octobre 2015 00:00:00”
(event id 5488C5A2-E889-4A7B-9B59-0ED33109C0BD of calendar id 15796FB4-0796-43DD-ACD4-C41067C1012D, event id 416C98D5-E3BF-4BFD-A8DC-1FC7BB31AAD1 of calendar id 15796FB4-0796-43DD-ACD4-C41067C1012D)
get properties of event id “5488C5A2-E889-4A7B-9B59-0ED33109C0BD” of calendar id “15796FB4-0796-43DD-ACD4-C41067C1012D”
(class:event, status:none, stamp date:date samedi 15 août 2015 11:38:33, allday event:true, recurrence:missing value, end date:date dimanche 11 octobre 2015 00:00:00, url:missing value, start date:date samedi 10 octobre 2015 00:00:00, description:missing value, summary:beurk, location:missing value, sequence:0, uid:5488C5A2-E889-4A7B-9B59-0ED33109C0BD, excluded dates:)
delete event id “5488C5A2-E889-4A7B-9B59-0ED33109C0BD” of calendar id “15796FB4-0796-43DD-ACD4-C41067C1012D”
get properties of event id “416C98D5-E3BF-4BFD-A8DC-1FC7BB31AAD1” of calendar id “15796FB4-0796-43DD-ACD4-C41067C1012D”
(class:event, status:none, stamp date:date samedi 15 août 2015 11:42:10, allday event:true, recurrence:missing value, end date:date dimanche 11 octobre 2015 00:00:00, url:missing value, start date:date samedi 10 octobre 2015 00:00:00, description:missing value, summary:beurk2, location:missing value, sequence:0, uid:416C98D5-E3BF-4BFD-A8DC-1FC7BB31AAD1, excluded dates:)
delete event id “416C98D5-E3BF-4BFD-A8DC-1FC7BB31AAD1” of calendar id “15796FB4-0796-43DD-ACD4-C41067C1012D”
end tell
but the events were always displayed.
When I ran the extended script a second time, the events log was :
(date samedi 10 octobre 2015 00:00:00)
tell application “Calendar”
activate
get every event of calendar “Test” whose start date = date “samedi 10 octobre 2015 00:00:00”
(**)
end tell

So, my understanding is that the script delete the events in ram but the application doesn’t “save” the changes.
I guess that we must issue an instruction to “save” the change but I don’t find which one in the dictionary.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) samedi 15 août 2015 12:16:59

I’m using Mavericks, 10.9.5, and the code works flawlessly there, with multiple”or no”events. There appears to be a bug in Yosemite’s Calendar.

Thanks,
tomorrow I will test in El Capitan.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) samedi 15 août 2015 22:19:45

Thanks Yvan for investigating this.

I’m also running Yosemite and experiencing al sorts of strange problems with calendar. Maybe it has something to do with ICloud, overruling some script functions.

Also I tried to copy events from one calendar to the other. Used this script:


tell application "Calendar"
	activate
	repeat with anEvent in (get events of calendar "A")
		move anEvent to (end of events of calendar "B")
	end repeat
end tell

But this results in this error in calendar:
"Calendar encountered a critical error while saving to databases, and recent changes may be lost. "

So I tried a different approach:


tell application "Calendar"
	activate
	set allEvents to events of calendar "A"
	repeat with theEvent in allEvents
		copy theEvent to end of events of calendar "B"
		delete theEvent
	end repeat
end tell

This results in a warning in calendar. It says that it rejects the event because it already exist.

I am wandering if these problem are also related to Yosemite.

Hello

What is the exact protocol used ?

(1) run the first script
(2) run the second script

or

(1) run the first script
(2) quit the Script Editor
(3) run the second script

If it’s the first case, try to run the second script after quitting Script Editor then restart it.

I’m quite sure that in this case, the second script wil run correctly - but the new set of events will not be saved.

If I have some time avaiulable I will test on my side but I am not sure that I will be able to do that today.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) dimanche 16 août 2015 11:23:34

Hello

At this time, I am running El Capitan.

I ran this script :

"3/10/2015 00:00"
set date3 to date result
"10/10/2015 00:00"
set date10 to date result
tell application "Calendar"
	activate
	set allEventsB3 to events of calendar "B" whose start date is date3
	log result
	set allEventsB10 to events of calendar "B" whose start date is date10
	log result
	set allEventsT3 to events of calendar "Test" whose start date is date3
	log result
	set allEventsT10 to events of calendar "Test" whose start date is date10
	log result
	
	
	set allEvents to events of calendar "Test"
	repeat with theEvent in allEvents
		copy theEvent to end of events of calendar "B"
		delete theEvent
	end repeat
	
	set allEventsB3 to events of calendar "B" whose start date is date3
	log result
	set allEventsB10 to events of calendar "B" whose start date is date10
	log result
	set allEventsT3 to events of calendar "Test" whose start date is date3
	log result
	set allEventsT10 to events of calendar "Test" whose start date is date10
	log result
end tell

The Events log was :

tell application "Calendar"
	activate
	get every event of calendar "B" whose start date = date "samedi 3 octobre 2015 00:00:00"
	(**)
	get every event of calendar "B" whose start date = date "samedi 10 octobre 2015 00:00:00"
	(**)
	get every event of calendar "Test" whose start date = date "samedi 3 octobre 2015 00:00:00"
	(*event id F8BC3FED-E82E-4496-931A-99910B24A84B of calendar id 0FC305E0-CFB2-41AE-AF13-E136A441E419*)
	get every event of calendar "Test" whose start date = date "samedi 10 octobre 2015 00:00:00"
	(*event id 6C8A815A-F3C7-468D-A2E8-0A4FCF4B0624 of calendar id 0FC305E0-CFB2-41AE-AF13-E136A441E419, event id 7D64BB00-CC5E-458B-B968-8432F1B7BDCA of calendar id 0FC305E0-CFB2-41AE-AF13-E136A441E419*)
	get every event of calendar "Test"
	duplicate event id "F8BC3FED-E82E-4496-931A-99910B24A84B" of calendar id "0FC305E0-CFB2-41AE-AF13-E136A441E419" to end of every event of calendar "B"
	delete event id "F8BC3FED-E82E-4496-931A-99910B24A84B" of calendar id "0FC305E0-CFB2-41AE-AF13-E136A441E419"
	duplicate event id "6C8A815A-F3C7-468D-A2E8-0A4FCF4B0624" of calendar id "0FC305E0-CFB2-41AE-AF13-E136A441E419" to end of every event of calendar "B"
	delete event id "6C8A815A-F3C7-468D-A2E8-0A4FCF4B0624" of calendar id "0FC305E0-CFB2-41AE-AF13-E136A441E419"
	duplicate event id "7D64BB00-CC5E-458B-B968-8432F1B7BDCA" of calendar id "0FC305E0-CFB2-41AE-AF13-E136A441E419" to end of every event of calendar "B"
	delete event id "7D64BB00-CC5E-458B-B968-8432F1B7BDCA" of calendar id "0FC305E0-CFB2-41AE-AF13-E136A441E419"
	get every event of calendar "B" whose start date = date "samedi 3 octobre 2015 00:00:00"
	(*event id F8BC3FED-E82E-4496-931A-99910B24A84B of calendar id 66377CEC-F5DB-43BB-9628-7A475885929C*)
	get every event of calendar "B" whose start date = date "samedi 10 octobre 2015 00:00:00"
	(*event id 6C8A815A-F3C7-468D-A2E8-0A4FCF4B0624 of calendar id 66377CEC-F5DB-43BB-9628-7A475885929C, event id 7D64BB00-CC5E-458B-B968-8432F1B7BDCA of calendar id 66377CEC-F5DB-43BB-9628-7A475885929C*)
	get every event of calendar "Test" whose start date = date "samedi 3 octobre 2015 00:00:00"
	(**)
	get every event of calendar "Test" whose start date = date "samedi 10 octobre 2015 00:00:00"
	(**)
end tell

As you may see, the script claims that the three events available are correctly moved from Test to B.
Alas, the GUI doesn’t claims the same. It state that the event from 3/10/2015 and the first one from 10/10/2015 are moved but that the 2nd one from 10/10/2015 is always in Test.

I quitted Calendar then restarted it. The GUI continue to display the second event dated 10/10/2015 as belonging to Test but when I run the script, the three events are reported as belonging to B.

Good point, it appears that in El Capitan delete an event does the job which was not done in Yosemite.
Bad point, the GUI is not completely reflecting the changes and it’s annoying because we don’t know which event belong to a given calendar.

When I will have finished programmed test in El Capitan, I will return in Yosemite to test the behavior of the script.

Yvan KOENIG running El Capitan 10.11.0 in French (VALLAURIS, France) dimanche 16 août 2015 15:26:41

I’m back in Yosemite.

Running the same script, I got the same events log than in El Capitan
BUT this time,
the two events which were dated 10/10/2015 in Test are really moved in B and the GUI reflects the changes.
the event dated 3/10/2015 in Test is always in Test.

The pig-headed fool stroke again.

I added a single instruction in the script which became :

"3/10/2015 00:00"
set date3 to date result
"10/10/2015 00:00"
set date10 to date result
tell application "Calendar"
	activate
	set allEventsB3 to events of calendar "B" whose start date is date3
	log result
	set allEventsB10 to events of calendar "B" whose start date is date10
	log result
	set allEventsT3 to events of calendar "Test" whose start date is date3
	log result
	set allEventsT10 to events of calendar "Test" whose start date is date10
	log result
	
	
	set allEvents to events of calendar "Test"
	repeat with theEvent in allEvents
		copy theEvent to end of events of calendar "B"
		delete theEvent
		tell me to delay 0.1 # ADDED
	end repeat
	
	
	set allEventsB3 to events of calendar "B" whose start date is date3
	log result
	set allEventsB10 to events of calendar "B" whose start date is date10
	log result
	set allEventsT3 to events of calendar "Test" whose start date is date3
	log result
	set allEventsT10 to events of calendar "Test" whose start date is date10
	log result
end tell

The Events log was the same but this time I got the result which I got with the original in El Capitan.
the event dated 3/10/2015 and the 2nd event dated 10/10/2015 moved from Test to B but the first event dated 10/10/2015 remained in Test.

Will have to write a report but it will be difficult to be clear with my poor - old - scholar English.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) dimanche 16 août 2015 15:52:26

Thanks Yvan, for all your effort.

I am going to give it a rest, until problems with calendar in applescript are solved in Yosemite. For now I just use some manual work arounds.

Gr. Maarten

Hi guys,
just found your discussion and I think this might be an El Capitan issue. I’m having the same problem when I run the following:


set MyTestDate to date "Thursday, 17 September 2015 at 10:30:00 AM"
tell application "Calendar"
	tell calendar "Home"
		set MyEvent to every event whose start date is MyTestDate
		delete item 1 of MyEvent
	end tell
end tell

The event gets deleted according to the script because when I run it again, the script finds no events with that start date. However, when I look at Calendar I find that the event is still there, even when I restart Calendar.

Did you guys come up with any solutions to this?
Regards,
David

Hi all.
I think you have to refresh the Calendar.app if it is open (should update if you launch it after the delete).

--reload an open or close calendar and update events
try
	tell application "Calendar" to activate
	tell application "System Events"
		tell process "Calendar"
			keystroke "r" using {command down, shift down}
		end tell
	end tell
on error errorMsg
	return ("AsReload Applescript error: " & errorMsg) as text
end try