Parsing Mail message to create iCal event

I receive email confirmations of my car coop reservations in Vancouver, Canada, and am trying to learn enough AppleScript to get Mail to transfer the reservation information into iCal. Here is an example of the email:

I have pulled together content from several example AppleScripts on this forum (thank you), but have not be able to get the resulting script to work. The plan is for the script to be triggered by a Rule in Mail, but right now there is no evidence that anything is happening, not even an error or any sort of event being created in iCal.

I will copy the full draft script below – am I missing a key command to get this working?

Thanks,
Sean

Here is the draft:

using terms from application "Mail"
	on perform mail action with messages these_messages for rule this_rule
		tell application "Mail"
			set the message_count to the count of these_messages
			repeat with i from 1 to the message_count
				set this_message to item i of these_messages
				set theDescription to the content of this_message
				set {TID, text item delimiters} to {text item delimiters, "You have booked the "}
				set theContent to text item 2 of theDescription
				set theContent to text item 1 of theContent
				set text item delimiters to " on "
				set theSummary to text item 1 of theContent
				set theContent to text item 2 of theContent
				set text item delimiters to ", picking up at "
				set theDate to text item 1 of theContent
				set theContent to text item 2 of theContent
				set text item delimiters to " and returning by "
				set theStartTime to text item 1 of theContent
				set theContent to text item 2 of theContent
				set text item delimiters to " ("
				set theEndTime to text item 1 of theContent
				set theContent to text item 2 of theContent
				set text item delimiters to ":"
				set {shr, smn} to text items of theStartTime
				set {ehr, emn} to text items of theEndTime
				set text item delimiters to TID
				set theStartDate to date theDate
				tell theStartDate
					set {its hours, its minutes} to {shr, smn}
				end tell
				set theEndDate to date theDate
				tell theEndDate
					set {its hours, its minutes} to {ehr, emn}
				end tell
				using terms from application "iCal"
					tell application "iCal"
						tell calendar "Travel"
							make new event at end with properties {summary:theSummary, description:theDescription, start date:theStartDate, end date:theEndDate}
						end tell
					end tell
				end using terms from
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Browser: Safari 534.53.10
Operating System: Mac OS X (10.7)

You can’t tell a calendar by name. Use:

set tCal to last calendar where its name is "Travel"

where I have three calendars named normal with only the last one active.

If you’re using iCloud, that confuses the issue. Run this:

tell application "iCal" to set allCals to calendars where its name is "Travel"

If you get more than one, use the last one (as I did). After that you can

tell tCal . etc.

Well. You can, but it won’t make much sense if you have more than one with the same name. However, iCal won’t allow me to have two calendars with the same name. Maybe it’s different in the Lion version.

My own observations on sleroy0’s script:

  1. Using the same delimiter, theContent is set to text item 2 of theDescription and then to text item 1 of itself. It doesn’t do any harm but it’s a waste of time.

  2. There’s a line break in the example e-mail text which isn’t included in the delimiter ", picking up at ", so getting text item 2 of theContent with that leads to an error.

  3. I also get an error trying to turn theDate into a date by that means. The year needs to be supplied too.

  4. There’s no need to put the iCal code in a 'using terms from application “iCal” statement and it would be better not to have it inside the ‘tell application “Mail”’ statement.

I haven’t tested the following in Mail, which I don’t use, but the text parsing and iCal code works with the information given.

using terms from application "Mail"
	on perform mail action with messages these_messages for rule this_rule
		tell application "Mail"
			set the message_count to (count these_messages)
			repeat with i from 1 to the message_count
				set this_message to item i of these_messages
				set theDescription to the content of this_message
				my parseDescription(theDescription)
			end repeat
		end tell
	end perform mail action with messages
end using terms from

on parseDescription(theDescription)
	set {TID, text item delimiters} to {text item delimiters, "You have booked the "}
	set theContent to text item 2 of theDescription
	set text item delimiters to " on "
	set theSummary to text item 1 of theContent
	set theContent to text item 2 of theContent
	-- This assume two commas in the text with the day and month between them.
	set text item delimiters to ","
	set {theDay, theMonth} to words of middle text item of theContent
	-- And this assumes two colons in the text which are the separators in the start and end times.
	set text item delimiters to ":"
	set {a, b, c} to theContent's text items
	set text item delimiters to TID
	
	set now to (current date)
	copy now to theStartDate
	set theStartDate's day to 1
	set theStartDate's month to (offset of (text 1 thru 3 of theMonth) in "JanFebMarAprMayJunJulAugSepOctNovDec") div 3 + 1
	set theStartDate's day to theDay
	set theStartDate's time to (word -1 of a) * hours + (word 1 of b) * minutes
	-- If the start date created is earlier than the current date, assume it's meant to be in the following year. This may need further refinement according to whatever you think could happen.
	if (theStartDate comes before now) then set theStartDate's year to (theStartDate's year) + 1
	
	copy theStartDate to theEndDate
	set theEndDate's time to (word -1 of b) * hours + (word 1 of c) * minutes
	
	tell application "iCal"
		tell calendar "Travel"
			make new event at end of events with properties {summary:theSummary, description:theDescription, start date:theStartDate, end date:theEndDate}
		end tell
	end tell
end parseDescription

I don’t think it’s Lion, per se, Nigel; I think it’s iCloud, and before that a preference pane cloud-type synchronizer called fruux (that works in both Snow Leopard and Lion). Each of them seems to configure a local set of files along with the remote set and these show up in AppleScript, though not in the Calendar itself, so I have three identical “Normal” calendars – the original, the fruux version (same), and the iCloud version (again the same). Without those local versions, you’d have to be connected to the Internet to see your calendars.