Need help with modification of script - mail to ical

I’m really a beginner with AppleScript and have been tinkering with a script to send specific messages from mail and make them new events in ical. I have a preexisting script that takes specific mail messages via a rule and takes them and makes them into new todos in ical. This is the slightly modified script that I’m working with.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				set theSubject to subject of eachMessage
				set theContent to content of eachMessage
				try
					tell application "iCal"
						set newevent to (make new event in calendar "Work")
						tell newevent
							set summary to theSubject
							set description to theContent
						end tell
					end tell
				end try
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Here is what I’m attempting to exactly do. What I want to do is to take a given email; use a rule with the variable subject line that says: New Task Assignment; and put it in a mailbox named “Tasks”. Then I want applescript to take the subject line of the email and make it the Event Name. Then I want the body of the email to be used in two places. First, I need the script to place the whole body of the email in the notes section of the new event. Second, I need the script to take a specific line from the email body that has the due date and time of day that the task needs to be done by. The due date can be in both the “from” and “to” sections. The actual time of day that the task is due will be in the “to” section. The “from” section will be set to one hour before the actual time that the task is due ie if the task is due at 2pm the “from” section will say 1pm.

Here is what a typical email looks like:

From: shauna@hulapost.com
Subject: New Task Assignment: Please hook up the Raid in Machine Room to the G5
Date: February 26, 2008 10:08:57 PM EST
To: scott@hulapost.com

The following new task has been created on the “Hula Post” web office
site.

TASK NAME: Please hook up the Raid in Machine Room to the G5
ASSIGNED BY: Shauna
PROJECT: (none)
CATEGORY: Equipment
DUE: Friday, February 29, 2008 5:00:00 PM EST
PRIORITY: 2-Normal
STATUS: 1-Not Started
ASSIGNED TO: Chrissy, Nick, Scott, Shauna,
Terry
VISIBLE TO: Everyone

DETAILS:
Please hook up the Raid in Machine Room to the G5 Final Cut system.

If you have any questions, please check with Jonathan or Jay.

Many Thanks!
Chrissy

This task can be found at any random website.
######################################################################################################################################

Thanx in advance for your help.

Regards,
Scott

Model: MacBook Pro 2.33 3GB RAM
AppleScript: 2.1.1
Browser: Firefox 3.0b3
Operating System: Mac OS X (10.4)

No one has any thoughts on how to do this?

I’m going to try and hammer this one out. If I have good results I’ll post them.

~Mullet

Have a look at post #8 here to get some ideas. ( read the rest of the thread, in particular #5 to see what it’s doing)

Also to pick those specific lines out in from the content , break the content down in to paragraphs with the script,
This make each line an item in a list, which you can search with a if item contains repeat loop

Thanx for your prompt reply. Now I have more to work from…

~Mullet

hi, while I don’t have an answer to your question, you should check out MailTags which allows you to create ToDo, Events and more. it is well priced. coupled with Act-On from the same guys, it saved Mail for me. PS: the beta for Leopard works fine.

I’m still having problems with my script. The issue that I’m having has to do with parsing text from mail and using it in ical. The line that I want to extract is very specific and unfortunately I don’t understand the intricacies of pulling out specific text in AppleScript. I can get iCal to create new events, but they only occur on the exact date that I’m running the script. I wanted to have AppleScript take information from the email and use it to create exact due dates with the correct time of day that the task would be due. Can anyone help with the proper way to extract text from an email and use it as a date. Bare in mind I’m using Tiger here and not Leopard. Leopard has a new feature that lets you click on a time and create a new event from that. This would sort of work fine for me if I was using Leopard. Also, I wanted to automate the process rather than selecting each new email and creating an event from that. Here is the current version of my script.


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				set theSubject to subject of eachMessage
				set theContent to content of eachMessage
				set {theStart, theEnd} to paragraph 7 & text item 3 of theContent
				try
					tell application "iCal"
						set newevent to (make new event at end of events of calendar "Work")
						tell newevent
							set summary to theSubject
							set description to theContent
							set start date to theStart
							set end date to theEnd
						end tell
					end tell
				end try
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Thanx,
Scott

PS I tried MailTag and it couldn’t do what I was trying to do.

Hi,

assuming you want to extract the date of the DUE line, try this


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			tell application "Mail" to set {subject:theSubject, content:theContent} to eachMessage
			set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "DUE:"}
			set theDate to text from word 1 to word -1 of paragraph 1 of text item 2 of theContent
			set AppleScript's text item delimiters to ASTID
			try
				tell application "iCal"
					set newevent to (make new event at end of events of calendar "Work")
					tell newevent
						set summary to theSubject
						set description to theContent
						set start date to date theDate
						set end date to date theDate
					end tell
				end tell
			end try
		end repeat
	end perform mail action with messages
end using terms from

Note: it certainly works only on systems with US date format

Hi Guys’

I was working on this, so I will post what I have,

The only issue I have is I can NOT get the PM to register using the full date format, which I am sure its supposed to?

Any Ideas??

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			set theSubject to ""
			set theBody to ""
			set theSdate to ""
			set theSubject to subject of eachMessage
			set theBody to content of eachMessage
			set theSdate to (characters -1 thru word 2 of (do shell script "echo " & quoted form of theBody & "| grep DUE\\:")) as string
			my theiCal(theSdate, theSubject, theBody)
		end repeat
	end perform mail action with messages
end using terms from
on theiCal(theSdate, theSubject, theBody)
	log theSdate
	set theS to date theSdate
	tell application "iCal" to launch
	
	tell application "iCal"
		
		set theEvent to (make event at end of events of calendar "Work" with properties {start date:theS, summary:theSubject, description:theBody})
	end tell
end theiCal

Hi Mark,

a legal, but not well known way to specify text


set theSdate to (text from word 2 to word -2 of (do shell script "echo " & quoted form of theBody & "| grep DUE\\:"))

Bloody hell, that always bugged me that I had the use characters… thru … word… to get the spaces ect… between the words.

Stefan once again you are a godsend. Thanks

Pushing my luck, any idea why the PM is ignored??

Thanks again

I apologize, I’m from Europe, we don’t have this AM/PM stuff :wink:

Thanx guys,

Both attempts are still not working for me. With my last script I could at least get a new event to show up. Now nothing works when trying to get the information from mail to ical. Hrrrm. Both scripts seem to make logical sense, but Mail won’t accept them for some reason. Again, thanx for you your help gents.

Regards,
Scott

PS I’m going to try and take a combination of both scripts, combine them with mine, and see if I can iron things out.

what Do you get, Have you tested it from a selected mail.?

Thanx a million Mark! I think I was using a different version of the script when I was testing everything out. Now that I went back and retried everything the last script works like a gem! The missing link for me was learning more about text item delimiters and what not. Now my next steps are to create an alarm for an hour in advance. I’m going to give it a shot and try to modify your version of the script. Thanx again!

~Scott