Adding an iCal event with a string

Should be a quick answer, I just seem to be missing something. The first block of code here adds the event just fine:

tell application "iCal" to activate
delay 5
tell application "iCal"
	tell calendar "Home"
		set startdate to date "Friday, December 2, 2011 2:00:00 AM"
		set enddate to date "Friday, December 2, 2011 3:00:00 PM"
		make new event at end with properties {summary:"Title", location:"Location", start date:startdate, end date:enddate}
	end tell
end tell

But this block here, I simply replace the quoted text with a string variable and it breaks, iCal runs into an error “Can’t get date” number -1728


tell application "iCal" to activate
delay 5
tell application "iCal"
	tell calendar "Home"
		set test to "Friday, December 2, 2011 2:00:00 AM"
		set startdate to date test
		set enddate to date "Friday, December 2, 2011 3:00:00 PM"
		make new event at end with properties {summary:"Title", location:"Location", start date:startdate, end date:enddate}
	end tell
end tell


Is there a fundamental concept I’m missing as to why quoted text can be converted into a date object but string variables cannot?

Hi.

It’s because ‘date test’ is inside the ‘tell’ statements for iCal. iCal doesn’t know how to do that kind of conversion (or its calendars don’t or there may be a conflict with something it does know how to do).

Assuming your computer’s set up to recognise dates in that format, you can can pass the responsibility for the conversion back to the script like this:

tell application "iCal"
	tell calendar "Home"
		set test to "Friday, December 2, 2011 2:00:00 AM"
		set startdate to my (date test)
		-- etc.
	end tell
end tell

Or better still, by taking the specifier out of the ‘tell’ statement completely:

set test to "Friday, December 2, 2011 2:00:00 AM"
set startdate to date test

tell application "iCal"
	tell calendar "Home"
		-- etc.
	end tell
end tell