Delete event iCal with specific uid

Hey everyone!!

Can anybody tell me what is wrong with this script? Everything seems to be working. I have pulled out of a string the string value unique uid for a particular event iCal, and then I am telling the application to delete the event with that uid. It is saying that it cannot get that event. Is it because it is a string???

What is the solution???

Thanks in advance…

tell application "iCal"
		set n to 1
		set y to count of every item in myChoice
		repeat until n is y + 1
			set charCount to count of characters in item n in myChoice
			set eventName to item n in myChoice
			set eventIDChar to characters (charCount - 35) thru (charCount) of eventName
			set eventID to eventIDChar as string
			tell calendar "Chris"
				if exists (event whose uid is eventID) then
					delete (event whose uid is eventID)
				else
					tell calendar "Joanne"
						delete (event whose uid is eventID)
					end tell
				end if
			end tell
			set n to n + 1
		end repeat
	end tell

tell application “iCal”
get every event of calendar “Chris” whose summary contains {“A & B”}
→ {event id “FCB9E82B-DEDD-46DD-8AC7-01BA9C690642” of calendar id “D2B5F6F2-0F47-4D34-9CE7-2F20A64EB86B”, event id “D5ED9966-81A3-416F-9203-A14BF8B98549” of calendar id “D2B5F6F2-0F47-4D34-9CE7-2F20A64EB86B”, event id “2853225A-87D0-4E06-8C84-68F31C2FA3D5” of calendar id “D2B5F6F2-0F47-4D34-9CE7-2F20A64EB86B”, event id “4D25E0DF-6EC8-4D6E-80D8-8DC42DFB3CB8” of calendar id “D2B5F6F2-0F47-4D34-9CE7-2F20A64EB86B”}
get uid of event id “FCB9E82B-DEDD-46DD-8AC7-01BA9C690642” of calendar id “D2B5F6F2-0F47-4D34-9CE7-2F20A64EB86B”
→ “FCB9E82B-DEDD-46DD-8AC7-01BA9C690642”
get start date of event id “FCB9E82B-DEDD-46DD-8AC7-01BA9C690642” of calendar id “D2B5F6F2-0F47-4D34-9CE7-2F20A64EB86B”
→ date “Thursday, 11 November 2010 08:15:00”
get summary of event id “FCB9E82B-DEDD-46DD-8AC7-01BA9C690642” of calendar id “D2B5F6F2-0F47-4D34-9CE7-2F20A64EB86B”
→ “A & B”
get uid of event id “D5ED9966-81A3-416F-9203-A14BF8B98549” of calendar id “D2B5F6F2-0F47-4D34-9CE7-2F20A64EB86B”
→ “D5ED9966-81A3-416F-9203-A14BF8B98549”
get start date of event id “D5ED9966-81A3-416F-9203-A14BF8B98549” of calendar id “D2B5F6F2-0F47-4D34-9CE7-2F20A64EB86B”
→ date “Thursday, 11 November 2010 01:00:00”
get summary of event id “D5ED9966-81A3-416F-9203-A14BF8B98549” of calendar id “D2B5F6F2-0F47-4D34-9CE7-2F20A64EB86B”
→ “A & B”
get uid of event id “2853225A-87D0-4E06-8C84-68F31C2FA3D5” of calendar id “D2B5F6F2-0F47-4D34-9CE7-2F20A64EB86B”
→ “2853225A-87D0-4E06-8C84-68F31C2FA3D5”
get start date of event id “2853225A-87D0-4E06-8C84-68F31C2FA3D5” of calendar id “D2B5F6F2-0F47-4D34-9CE7-2F20A64EB86B”
→ date “Thursday, 11 November 2010 01:45:00”
get summary of event id “2853225A-87D0-4E06-8C84-68F31C2FA3D5” of calendar id “D2B5F6F2-0F47-4D34-9CE7-2F20A64EB86B”
→ “A & B”
get uid of event id “4D25E0DF-6EC8-4D6E-80D8-8DC42DFB3CB8” of calendar id “D2B5F6F2-0F47-4D34-9CE7-2F20A64EB86B”
→ “4D25E0DF-6EC8-4D6E-80D8-8DC42DFB3CB8”
get start date of event id “4D25E0DF-6EC8-4D6E-80D8-8DC42DFB3CB8” of calendar id “D2B5F6F2-0F47-4D34-9CE7-2F20A64EB86B”
→ date “Thursday, 11 November 2010 13:15:00”
get summary of event id “4D25E0DF-6EC8-4D6E-80D8-8DC42DFB3CB8” of calendar id “D2B5F6F2-0F47-4D34-9CE7-2F20A64EB86B”
→ “A & B”
get every event of calendar “Joanne” whose summary contains {“A & B”}
→ {event id “BED81B14-89E6-4416-B2E8-F6A06C819275” of calendar id “00A69C2B-3B9E-4A45-8A08-1D53B09D0E2C”}
get uid of event id “BED81B14-89E6-4416-B2E8-F6A06C819275” of calendar id “00A69C2B-3B9E-4A45-8A08-1D53B09D0E2C”
→ “BED81B14-89E6-4416-B2E8-F6A06C819275”
get start date of event id “BED81B14-89E6-4416-B2E8-F6A06C819275” of calendar id “00A69C2B-3B9E-4A45-8A08-1D53B09D0E2C”
→ date “Thursday, 11 November 2010 14:00:00”
get summary of event id “BED81B14-89E6-4416-B2E8-F6A06C819275” of calendar id “00A69C2B-3B9E-4A45-8A08-1D53B09D0E2C”
→ “A & B”
end tell
tell current application
activate
end tell
tell application “AppleScript Editor”
choose from list {“Thursday, 11 November 2010 08:15:00 with couple A & B with Chris and Event ID is:FCB9E82B-DEDD-46DD-8AC7-01BA9C690642”, “Thursday, 11 November 2010 01:00:00 with couple A & B with Chris and Event ID is:D5ED9966-81A3-416F-9203-A14BF8B98549”, “Thursday, 11 November 2010 01:45:00 with couple A & B with Chris and Event ID is:2853225A-87D0-4E06-8C84-68F31C2FA3D5”, “Thursday, 11 November 2010 13:15:00 with couple A & B with Chris and Event ID is:4D25E0DF-6EC8-4D6E-80D8-8DC42DFB3CB8”, “Thursday, 11 November 2010 14:00:00 with couple A & B with Joanne and Event ID is:BED81B14-89E6-4416-B2E8-F6A06C819275”} with prompt “Select Events to Delete:” with multiple selections allowed without empty selection allowed
→ {“Thursday, 11 November 2010 13:15:00 with couple A & B with Chris and Event ID is:4D25E0DF-6EC8-4D6E-80D8-8DC42DFB3CB8”, “Thursday, 11 November 2010 14:00:00 with couple A & B with Joanne and Event ID is:BED81B14-89E6-4416-B2E8-F6A06C819275”}
Result:
error “Can’t get event whose uid = "4D25E0DF-6EC8-4D6E-80D8-8DC42DFB3CB8".” number -1728

Hi,

the whose filter expects a list


delete (every event whose uid is eventID)

or an range specifier like


delete (1st event whose uid is eventID)
delete (last event whose uid is eventID)
delete (some event whose uid is eventID)

That is great, Stefan!! It works perfectly. I am only not sure about one thing. Why in my script editor replies window am I getting a Result: 3???

tell application “AppleScript Editor”
choose from list {“Thursday, 11 November 2010 08:15:00 with couple A & B with Chris and Event ID is:FCB9E82B-DEDD-46DD-8AC7-01BA9C690642”, “Thursday, 11 November 2010 01:00:00 with couple A & B with Chris and Event ID is:D5ED9966-81A3-416F-9203-A14BF8B98549”, “Thursday, 11 November 2010 01:45:00 with couple A & B with Chris and Event ID is:2853225A-87D0-4E06-8C84-68F31C2FA3D5”} with prompt “Select Events to Delete:” with multiple selections allowed without empty selection allowed
→ {“Thursday, 11 November 2010 01:00:00 with couple A & B with Chris and Event ID is:D5ED9966-81A3-416F-9203-A14BF8B98549”, “Thursday, 11 November 2010 01:45:00 with couple A & B with Chris and Event ID is:2853225A-87D0-4E06-8C84-68F31C2FA3D5”}
end tell
tell application “iCal”
exists some event of calendar “Chris” whose uid = “D5ED9966-81A3-416F-9203-A14BF8B98549”
→ true
delete every event of calendar “Chris” whose uid = “D5ED9966-81A3-416F-9203-A14BF8B98549”
exists some event of calendar “Chris” whose uid = “2853225A-87D0-4E06-8C84-68F31C2FA3D5”
→ true
delete every event of calendar “Chris” whose uid = “2853225A-87D0-4E06-8C84-68F31C2FA3D5”
end tell
Result:
3

the result window displays always the result of the last executed line,
in your script the result of

set n to n + 1

PS: this is double checked


 if exists (1st event whose uid is eventID) then
                   delete (1st event whose uid is eventID)
end if

the whose filter checks the existence anyway, this is sufficient

delete (1st event whose uid is eventID)

Thank you, Stefan,

So, I assume that the 3 Result is irrelevant to the process??

Second, do I not need the ‘exists’ in order to successfully run the [b}if[/b] and else if part of the script??


tell calendar "Chris"
				if exists (some event whose uid is eventID) then
					delete (every event whose uid is eventID)
				else
					tell calendar "Joanne" of application "iCal"
						delete (every event whose uid is eventID)
					end tell
				end if

Are you suggesting that I will not run an error if I do this?


tell calendar "Chris"
				delete (1st event whose uid is eventID)
				else
					tell calendar "Joanne" of application "iCal"
						delete (1st event whose uid is eventID)
					end tell

Thanks, DDHawk

ok, in this case the exists line makes sense.
As the whose filter is quite expensive, this might be better


tell application "iCal"
	try
		tell calendar "Chris"
			set eventToDelete to every event whose uid is eventID
			delete eventToDelete
		end tell
	on error
		tell calendar "Joanne"
			delete (every event whose uid is eventID)
		end tell
	end try
end tell

Yes - I see…

Adjusted as follows:


try
				tell calendar "Chris"
					set eventToDelete to every event whose uid is eventID
					delete eventToDelete
				end tell
			on error
				tell calendar "Joanne"
					delete eventToDelete
				end tell
			end try


That worked as expected - very clear - thank you for such great advice!!

No, it can’t, the variable eventToDelete won’t be set if the eventID is not in calendar Chris.
Actually the whose filter is not needed at all because an event can be referenced by its uid


tell application "iCal"
	try
		tell calendar "Chris" to delete event id eventID
	on error
		try
			tell calendar "Joanne" to delete event id eventID
		end try
	end try
end tell

the second try block is needed if the uid doesn’t exist at all

Hey Stefan,

This is the whole piece of Applescript. Previously, I have selected an event/events in list myChoice with a defined ‘without empty selection allowed’, so technically there must be a uid to find in one of these calendars.


tell application "AppleScript Editor"
		activate
		set myChoice to choose from list choiceCount with prompt "Select Events to Delete:" with multiple selections allowed without empty selection allowed
	end tell


tell application "iCal"
		set n to 1
		set y to count of every item in myChoice
		repeat until n is y + 1
			set charCount to count of characters in item n in myChoice
			set eventName to item n in myChoice
			set eventIDChar to characters (charCount - 35) thru (charCount) of eventName
			set eventID to eventIDChar as string
			set eventToDelete to every event whose uid is eventID
			try
				tell calendar "Chris"
					delete eventToDelete
				end tell
			on error
				try
					tell calendar "Joanne"
						delete eventToDelete
					end tell
				end try
			end try
			set n to n + 1
		end repeat
	end tell

However, I am listening to your experience and adding the extra try block. However, I have now rewritten the script taking the eventToDelete clause out of the try block. Does this satisfy all script writing code rules???

regards,

DDHwk

the eventToDelete definition must be within the first try block, because there could occur the error.
If the uid exists in any case the second try block is not needed.

here is a simpler version of your script, the Script Editor tell block is not needed too,
activate iCal and put the choose from list line in the iCal tell block


tell application "iCal"
	activate
	set myChoice to choose from list choiceCount with prompt "Select Events to Delete:" with multiple selections allowed without empty selection allowed
	repeat with oneEvent in myChoice
		set eventID to text -35 thru -1 of oneEvent
		try
			tell calendar "Chris" to delete event id eventID
		on error
			tell calendar "Joanne" to delete event id eventID
		end try
	end repeat
end tell