Creating iCal events and to do to selected calendar

I’m trying to create a script that will create events and todos from Mail messages. (I know there are some out there, but they don’t do quite what I want.) I can get the info I need from Mail, but have problems creating the iCal entry. I want to prompt the user to select a calendar and then create the event or todo on the selected calendar. The problem occurs after I get the selection from the user and try to make a new event/todo on that calendar. When I run this script, I get the error: “iCal got an error: Can’t make calendar {”@Anywhere"} into type reference."

I’ve tried all sorts of shennanigans to get this to work, but I’m afraid I’m missing what is probably a simple fundamental of AppleScript. I’d greatly appreciate if someone would have mercy on me :slight_smile: and provide some advice.

Here’s the script:

tell application "Mail"
	set selectedMsg to the selection
	set countMsgs to (count selectedMsg)
	if countMsgs is not equal to 1 then
		display dialog "Choose one and only one message!" buttons {"Okay"}
		return
	end if
	--set msgToProcess to item 1 of selectedMsgs
	set taskSubject to subject of item 1 of selectedMsg
	set taskNotes to content of item 1 of selectedMsg
end tell
tell application "iCal"
	set allCalNames to name of every calendar
	set chosenCalendar to (choose from list allCalNames)
	tell calendar chosenCalendar
		make new todo with properties {summary:taskSubject, description:taskNotes}
	end tell
end tell

Thanks in advance.

  • Leon

Hi Leon,

choose from list returns a list or boolean false if the user has pressed cancel,
this does it

.
	set allCalNames to name of every calendar
	set chosenCalendar to (choose from list allCalNames) as string -- coerces the list to a string
	if chosenCalendar is "false" then return
	tell calendar chosenCalendar
		make new todo with properties {summary:taskSubject, description:taskNotes}
	end tell
end tell

You are my hero! Thanks for the quick reply. You’re suggestion fixed the calendar selection problem. I then found I had to change the way I set the properties of the new todo to avoid another error. The script works fine now. Next step is to add some more features.

Here’s the final product:

tell application "Mail"
	set selectedMsg to the selection
	set countMsgs to (count selectedMsg)
	if countMsgs is not equal to 1 then
		display dialog "Choose one and only one message!" buttons {"Okay"}
		return
	end if
	set todoSummary to subject of item 1 of selectedMsg
	set todoDescription to content of item 1 of selectedMsg
end tell
tell application "iCal"
	set allCalNames to name of every calendar
	set chosenCalendar to (choose from list allCalNames) as string
	if chosenCalendar is "false" then return
	set newtodo to (make new todo at end of todos of calendar chosenCalendar)
	tell newtodo
		set summary to todoSummary
		set description to todoDescription
	end tell
	show newtodo
end tell