How do I take an iCal to-do with a set due date, and clear it?

I have a to-do that has a set due date. I’ve identified the to-do and can get and set all the other properties, but I can’t seem to clear the due date as if the checkbox to the left of the due date was unchecked (i.e. reset the due date to “missing value”). Any ideas?

Model: MacBook Pro
AppleScript: 1.10.7
Browser: Firefox 2.0.0.3
Operating System: Mac OS X (10.4)

Hi kenray,

I don’t think you can set it to missing value. Getting and setting properties are different. A workaround would be to get the properties you can set, delete the old todo, and make new one. e.g.


tell application "iCal"
	activate
	set the_cal to first calendar whose title is "Work"
	set the_todo to first todo of the_cal whose summary is "TestTodo"
	set {summary:s, priority:p} to properties of the_todo
	delete the_todo
	make new todo at end of todos of the_cal with properties ¬
		{summary:s, priority:p}
end tell

Edited: note that when setting the due date, it wants a date.

gl,

Thanks for that, but unfortunately there are some r/o properties that I can read from the “original” to-do that I can’t set in the new to-do (like UID). I may have to live with that if there’s not other way, but that means that since I’m storing the UID for synchronization elsewhere I’ll have to replace it with the new UID of the new to-do and that makes it really sticky to make sure everything in my app gets changed properly.

I’m sure there’s got to be a way to “reset” a date class?

Hi kenray,

Personally, i’d delete the task if there was no due date. It would be just a dream.

gl,

Well this is a situation where I’m creating a front end that lets people manipulate their tasks in ways iCal can’t, but also syncs with iCal so that it will in turn sync with their PDAs. So if someone sets a task to have a due date, and then later wants to remove the due date from the task (making it an undated to-do), I’m sure they would be surprised to have their task deleted instead of having the due date cleared out (reset to the equivalent of “missing value”).

For now I’m doing what you’ve suggested - delete and recreate the event, and update my front end with the new UID that is created by the new task. However I ran into a couple of things while I was doing that:

delete the_todo

didn’t work - it didn’t error out, it just didn’t delete the task. I ended up creating a DeleteToDo function that would identify it based on cycling through all the to-do’s and finding the one which matched the UID, and then deleting it using the index number of the to-do:


on DeleteToDo(pToDo)
	tell application "iCal"
		set tUID to uid of pToDo
		repeat with x from 1 to (count of todos of calendar "Test")
			if uid of todo x of calendar "Test" = tUID then
				delete todo x of calendar "Test"
				exit repeat
			end if
		end repeat
	end tell
end DeleteToDo

Which leads me to my next question: how do you get the index number of a todo item in iCal if you have it as a reference? Doing the loop I have works, but is inefficient, especially on large quantities of to-dos…

Thanks for any help you (or anyone else reading this post) can provide…

Ken

Hi Ken,

I don’t know why Apple keeps changing stuff!

When I run the following, in my version of iCal, I get a reference by index:


tell application "iCal"
	set the_cal to first calendar whose title is "Home"
	set the_todo to first todo of the_cal whose summary is "TestTodo"
end tell

result:

→ todo 5 of calendar 1 of application “iCal”

What do you get?

gl,

Well, since I didn’t know how to do this:

set the_todo to first todo of the_cal whose summary is "TestTodo"

I was going into a repeat loop looking for the matching UID that I had stored in the front end:

set tFoundIt to false
repeat with theToDo in todos of calendar "Test"
	if uid of theToDo is pUID then
		set tFoundIt to true
	end if
end repeat

And, assuming it found it, ‘theToDo’ was identified as:

→ item 3 of every todo of calendar “Test” of application “iCal”

which would not work when I tried ‘delete theToDo’. So using your code that uses “whose” really simplifies my code a lot… thanks!

But now I have another question (sorry): how can I convert the “item 3 of every todo of calendar…” to “todo 2 of calendar…” ?

Thanks for all your help,

Ken