mail to ICal

New to scripting. Script ran a few times, then stopped creating ICal event. It is executed by mail rule.

Any thoughts?

Regards

tell application “Mail”
set thePrefix to “Tee Time”
set theMessages to (messages of inbox whose read status is false)
repeat with mail in theMessages
if the subject of the mail begins with thePrefix then
set theSummary to the subject of the mail
set theDescription to the content of the mail
set theStartDate to the date received of the mail

	end if
end repeat
tell application "iCal"
	set theCalendar to "Tee Time"
	tell calendar theCalendar
		make new event at end of events with properties {summary:theSummary, description:theDescription, start date:theStartDate + 3 * days}
		
	end tell
end tell

end tell

Model: IMac
AppleScript: 2.0.1
Browser: Safari 528.16
Operating System: Mac OS X (10.5)

Hi,

the script checks all messages, but creates an event only for the last message, whose subject matches thePrefix.
I recommend also to avoid nested application tell blocks


set thePrefix to "Tee Time"
tell application "Mail" to set theMessages to (messages of inbox whose read status is false)
repeat with mail in theMessages
	tell application "Mail" to set {subject:theSummary, content:theDescription, date received:theStartDate} to mail
	if theSummary begins with thePrefix then
		tell application "iCal"
			tell calendar thePrefix to make new event at end of events with properties {summary:theSummary, description:theDescription, start date:theStartDate + 3 * days}
		end tell
	end if
end repeat

Script will run manually, but not if execute from rule? Same symptoms.

from a rule you should use the rule event handler


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set thePrefix to "Tee Time"
		repeat with mail in theMessages
			tell application "Mail" to set {subject:theSummary, content:theDescription, date received:theStartDate} to mail
			if theSummary begins with thePrefix then
				tell application "iCal"
					tell calendar thePrefix to make new event at end of events with properties {summary:theSummary, description:theDescription, start date:theStartDate + 3 * days}
				end tell
			end if
		end repeat
	end perform mail action with messages
end using terms from

I am not sure how to format the following as I am still learning scripting. “rule theRule” My rule is named “TTime”. Should it read -rule TTime-?

Thanks for you help.

Regards,

Mike Van Meter

No, the variable theRule returns the name of the rule which triggered the script.
With this information you could use the same script for multiple rules

The script is not recognizing the email with Tee Time in the subject line.