Weird NSReceiver Error 4 Error in iCal Script

I’m trying to write a script that will automatically move any unfinished to-do items to today. I wrote the following script, which would dump out with NSreceiver error 4 after updating exactly half of the overdue to-do items in a calendar (rounding up, so 3 of 5, 2 of 3, 1 of 1, etc). Run again, it did half again, and so on until a calendar was done, then it does the same thing with the next calendar:

This script, on the other hand, is really slow, and it mostly works. It leaves one or two of the 15 or 16 items undone the first time (in both of the test calendars, but at least it doesn’t dump out.

I should note that the test to-do items are all identical dummies with nothing more than a title and a due date in 2 dummy calendars.

Ack. Help?

Hi,

The first script fails because you are checking the number of items that fit the criteria every time the loop goes around. This number reduces with every loop, and halfway through you’ll end up with the number of loops exceeding the number of items left, hence the error. (I’m sure somebody else will be able to explain this better.)

I also simplified some of the date maths. Todos only have a due date and don’t hold the time of day.

Try this:

set thisDate to date (date string of (current date))
tell application "iCal"
	activate
	repeat with thisCalendar in calendars
		-- Set an array variable to references to the todos.
		set myTodos to (todos of thisCalendar whose due date is greater than thisDate)
		-- Repeat through items in the array variable (the number of items in the array won't be reduced).
		repeat with thisTodo in myTodos
			if (not (completion date of thisTodo exists)) then set due date of thisTodo to thisDate
		end repeat
	end repeat
end tell

Best wishes

John M

OK, that worked really well (though I did need to change the due date greater than thisDate to less than).

A read of the event log leads me to I think I understand what you’re saying re the looping:

– the “repeat with” loop determines the number of items on which it needs to operate at the outset of the loop, so if there are 6 todos meeting those critiera, it knows it will do 6 loops, but but if one changes those items inside the loop, they’re no longer included in the items on which the loop will operate

– then I reduced the total number of items on which it could operate each time that I reset the due date, so when it gets to item 4 of 6, there is no item 4 on which to perform the operation.

– ergo, assigning those items to an array first makes that problem go away.

That sounds to me like a classic newbie mistake. Thanks for being kind to me, and thanks for the solution.

Jeremy