Deleting all future events in an iCal calendar

Hi,

This is part of a larger script I have written which adds events to a specified calendar. To stop duplicate events I would like to delete all future events in that calendar from the current time.

this is the subroutine I have at the moment.


property defaultCal : " "


if defaultCal is " " then
	set alertResult to display alert ¬
		"This script will delete all FUTURE events to an existing iCal calendar." & return & return & "Do you have a calendar you wish to use in iCal?" buttons {"Cancel", "Yes"} as warning ¬
		default button "Yes" cancel button "Cancel" giving up after 5
else
	tell application "iCal"
		tell calendar defaultCal
			set theDate to current date
			delete (events whose start date is after theDate)
		end tell
	end tell
end if

it returns an error of:

error “iCal got an error: Can’t get date "Tuesday, 16 August 2011 9:26:57 AM".” number -1728 from date “Tuesday, 16 August 2011 9:26:57 AM”

Any ideas?

Thnx

AppleScript: Version 2.4 (128)
Browser: Safari 534.48.3
Operating System: Mac OS X (10.6)

iCal’s understanding of AppleScript leaves room for improvement :mad:
It does not understand “is after”. You have to use “>” (and probably likewise for other comparisons).

And a tip: “current date” is from Scripting Additions. It’s better to not put it within a tell block.

The correct expression is ‘comes after’. :slight_smile: Alternatively, ‘>’ (as alastor933 suggests) or ‘is greater than’.

Hmmm,

Thanks for the quick reply, I think I may have to show you my entire script to sort this problem however, that would entail a whole raft of applescript newbie errors being exposed…

I made the changes you kindly suggested however, I am still returning an error


property defaultCal : " "
property rosterYear : ""

if defaultCal is " " then
	set alertResult to display alert ¬
		"This script will delete all FUTURE events to an existing iCal calendar." & return & return & "Do you have a calendar you wish to use in iCal?" buttons {"Cancel", "Yes"} as warning ¬
		default button "Yes" cancel button "Cancel" giving up after 5
else
	set theDate to (current date) as string
	tell application "iCal"
		tell calendar defaultCal
			delete (events whose start date > theDate)
		end tell
	end tell
end if

the error is:


error "iCal got an error: Can't make \"Tuesday, 16 August 2011 7:54:34 PM\" into type date." number -1700 from "Tuesday, 16 August 2011 7:54:34 PM" to date

Is there a simple fix or do I need to change my whole approach… Thank you for the input.

whoops, just realised I had added “as string” to my set theDate line.

When I remove this the script no longer errors however, not ALL of the events are deleted from iCal?!

Is there a better way of deleting events from ical that aren’t duplicates and are only FUTURE events?

Thanks

Kev

What is a duplicate, to you? Same summary, same everything? Do you want to keep future events that are not duplicates (by your definition)?

If so, you’d have to ask iCal for each event to be created if there’s a duplicate: an existing event with same summary, date, whatever. If yes, skip event creation.
It might be faster to just delete the lot, and create new ones.

The script tries to delete a list, which it seems to find hard. Try catching the list in a variable, then delete one by one in a repeat loop.

You hit the nail on the head alastor933. My airline roster is sent from my company as an .ics file however, the UID for each event changes each time it is sent, so each event becomes duplicated whenever the .ics file is imported.

My script attempts to parse the relevent info out of each BEGIN:VEVENT item as a new event in my ical calendar “Roster”. This part of my script works well however, whenever I have a roster change (which is frequently) and download the monthly .ics file all my previous events get duplicated.

What I am attempting to do is only add future events (which I have managed) however, delete ALL previous future events so that my “Roster” calendar is a “clean sheet” forward from the instant I run the script.

You mentioned a repeat loop?

I’ve been on this for waaaay too long and this is my final hurdle. You wouldn’t happen to have an example I could “borrow”?

Thank you very much for your help.

Kev

Yes, sorting that out would be a lot of work.

Here’s how to delete future events one by one. Not fast, but not too bad when not done every 15 mins…

	tell calendar defaultCal
			set futureEvents to events whose start date > theDate
			repeat with anEvent in futureEvents
				delete anEvent
			end repeat
		end tell

That works perfectly!

Thanks so much alastor933 :smiley: