Moving a bunch of iCal events to another Calendar

Hi All,

Need some help here. Getting close to going insane!

There are 24 events to move from the ‘lunch’ calendar to the ‘submitted’ calender, and strangely it bombs out at the 13th on the first run, then at the 7th on the second run, and so on. ie. It can only go half way! before erroring!

Can anyone shed some light on this. If I haven’t explained something, just let me know.

Thanks in advance.

Jase

the bit of problematic code is below:


	
tell application "iCal"
	
	set thisCalendar to first calendar whose title is "Lunch"
	set the returned_events to every event of thisCalendar
	
	set Submitted_Cal_Name to "Submitted" as Unicode text
	set Submitted_Calendar to (first calendar whose title is Submitted_Cal_Name)
	
end tell

repeat with i from 1 to the count of returned_events
	
	tell application "iCal"
		
		set this_event to item i of returned_events
		
		display dialog "i = " & i & return & "This Event Count = " & (count of this_event) & return & "Total Event Count = " & (count of returned_events) & return & "Class = " & (class of this_event) giving up after 1
		
		set the event_properties to properties of this_event
		
		move this_event to end of Submitted_Calendar
		
		
	end tell
end repeat
	

Hi Jase,

It is failing because you reduce the count of ‘returned_events’ by one every time you move it to the other calendar, but the repeat only stores the original count. Therefore when the repeat loop gets halfway through, the item it is trying to reference doesn’t exist anymore.

You can get around this by starting at ‘count of returned_events’ and going to 1 by steps of -1.

Try this version of your script:

tell application "iCal"
	set the returned_events to every event of calendar "Lunch"
	set Submitted_Calendar to calendar "Submitted"
	
	repeat with i from (count of returned_events) to 1 by -1
		set this_event to item i of returned_events
		display dialog "i = " & i & return & "This Event Count = " & (count of this_event) & return & "Total Event Count = " & (count of returned_events) & return & "Class = " & (class of this_event) giving up after 1
		move this_event to end of Submitted_Calendar
	end repeat
end tell

Best wishes

John M

Hi,

two notes about the repeat constructions:
1.) There are two major forms, repeat with i from x to y and repeat with i in aList.
The first one uses a accessible index variable, which is usefull e.g. to specify the position of an element of a list,
the second uses references to process the elements of a list directly. If you don’t need the index variable of the first form
don’t use it. It’s slower than processing a list directly.
In your case it’s better

repeat with i in every event of this calendar
...
end repeat

2.) But this doesn’t solve the problem, to get lost renaming files of or moving files from the list, because
in every loop the list will be generated again but not the (internal) index variable i. Furthermore it wastes a lot of time when
the list doesn’t change. To avoid this use the word get:

repeat with i in (get every event of this calendar)
...
end repeat

or with your variable

set the returned_events to (get every event of thisCalendar)
repeat with i in returned_events
...
end repeat

this generates the list only once holds it temporarily in memory,
and the loop doesn’t run out of bounds

Learn something every day :D. I didn’t know the “Get” trick for repeat with i in L, Stefan; Thanks.