Formatting content of an email to a script date

Hello Again,

I posted this project a week or so ago and no one responded. Hopefully this time you all will be more interested becasue I am SO CLOSE to finishing this script (and even closer to giving up altogether).

my overal goal is to have a free .Mac account equivilent: a platform independat ability to add events, contacts, and bookmarks and also to view them on the internet. I am doing well on the other parts but this little scrpt is killing me.

So here it is. I am trying to make it so that I send an email to myself with subject line “add event” and this tirggers a rule which will read the email and use the contents of the body to set the date, content, subject, etc of a new iCal event.

All of this is working WONDERFULLY except one little detail: dates. I can’t figure out how to get the content of the email formated into a date. Below is the script with it set to add the event to calendar “Work”.

It is canabalized from “Mail2iCal1.1.4.scpt” that I found somewhere.

Anyway please please help me figure this out becuase i have tried everything and i ust cant get the stupid date formating to work.


property pCalName : ""
property pDontAsk : false

on perform_mail_action(info)
	set mailCal to my checkForDefault()
	set allMessages to |SelectedMessages| of info
	my generateItems(allMessages, mailCal)
end perform_mail_action

on run
	tell application "Mail"
		set mailCal to my checkForDefault()
		set allMessages to selected messages of message viewer 1
		my generateItems(allMessages, mailCal)
	end tell
end run

on getIndex(theItem, theList)
	repeat with i from 1 to count of theList
		if theItem is item i of theList then
			return i
		end if
	end repeat
	return -1
end getIndex

on checkForDefault()
	tell application "iCal"
		set allCals to title of calendars
		set pCalName to "Work"
		set calIndex to (my getIndex(pCalName, allCals))
		set mailCal to calendar calIndex
	end tell
	return mailCal
end checkForDefault

on generateItems(allMessages, mailCal)
	tell application "Mail"
		repeat with curMessage in allMessages
			set theSender to sender of curMessage
			set theSubject to subject of curMessage
			set mailContents to content of curMessage
			set {test1, test2} to paragraphs of mailContents
			set theBody to test2

--here is the trouble spot.  I just have it set to current date right now so you
--can see that this script does work.  just how to get a paragraph from the 
--contents to be turned into a date?

			set theDate to current date
			tell application "iCal"
				set newEvent to (make new event at the end of events in mailCal)
				tell newEvent
					set start date to theDate
					set end date to ((theDate + minutes) as date)
					set summary to (theSender & ", " & theSubject)
					set description to theBody
				end tell
			end tell
		end repeat
	end tell
end generateItems

How is the date formatted in the message body?

If you have a string like “01/04/2004 14:50:24” you can turn it into a date reference with:

set dateString to "01/04/2004 14:50:24"
date dateString
-- date "Sunday, January 4, 2004 2:50:24 PM"

The time string works with “2:50:24 PM” too.

i figure it out. the script isnt completley finished yet inthat it always writes to the work calendar but it takes the date from an email and makes a new event.

the email:


property pCalName : ""
property pDontAsk : false

using terms from application "Mail"
	on perform mail action with messages allMessages for rule theRule
		set mailCal to my checkForDefault()
		tell application "Mail"
			repeat with eachMessage in allMessages
				my generateItems(eachMessage, mailCal)
			end repeat
		end tell
	end perform mail action with messages
end using terms from

on getIndex(theItem, theList)
	repeat with i from 1 to count of theList
		if theItem is item i of theList then
			return i
		end if
	end repeat
	return -1
end getIndex

on checkForDefault()
	tell application "iCal"
		set allCals to title of calendars
		set pCalName to "Work"
		set calIndex to (my getIndex(pCalName, allCals))
		set mailCal to calendar calIndex
	end tell
	return mailCal
end checkForDefault

on generateItems(allMessages, mailCal)
	tell application "Mail"
		repeat with curMessage in allMessages
			set theSender to sender of curMessage
			set theSubject to subject of curMessage
			set mailContents to content of curMessage
			set {test1, test2, test3, test4, test5, test6} to paragraphs of mailContents
			set theBody to test2
			set theMonth to my integerToMonth((test1 as integer))
			set dateString to current date
			set month of dateString to theMonth
			set day of dateString to (test2 as integer)
			set year of dateString to (test3 as integer)
			set time of dateString to (test4 as integer)
			tell application "iCal"
				set newEvent to (make new event at the end of events in mailCal)
				tell newEvent
					set start date to dateString
					set end date to ((dateString + minutes) as date)
					set summary to test5
					set description to test6
				end tell
			end tell
		end repeat
	end tell
end generateItems

on integerToMonth(theInteger) --(used by recordToDate)
	try
		if theInteger is less than 1 then error
		{January, February, March, April, May, June, July, August, September, October, November, December}'s item theInteger
	on error
		error "Error: not an integer between 1 and 12."
	end try
end integerToMonth