Mail and Parsing Emails!

Alright… I’ve got a dilemma. I need to be able to have an applescript that be attached to a rule in Mail.
When the script is run, it parses the content of the email (which is already formatted ready for parsing from a PHP server-side form) and adds it to an Excel template.
I have the parsing subroutine down right (it works with just text for input) but I need to be able to have it automated within Mail.

Any suggestions? I have a feeling I’m making a stupid mistake.

using terms from application "Mail"
	on perform mail action with messages theMessages
		tell application "Mail"
			set theMessages to selection
			set theContent to the content of theMessages as string
		end tell
		parseMessage(theContent)
	end perform mail action with messages
end using terms from
on parseMessage(theContent)
	set AppleScript's text item delimiters to {"  "}
	set certFrom to text item 2 of theContent as string
	set Occassion to text item 4 of theContent as string
	set annivYears to text item 6 of theContent as string
	set dateofAniv to text item 8 of theContent as string
	set nameS to the text item 10 of theContent as string
	set recepNumber to the text item 12 of theContent as string
	set recepAddress to the text item 14 of theContent as string
	set requestAddress to the text item 16 of theContent as string
	set requestName to the text item 18 of theContent as string
	set requestNumber to the text item 20 of theContent as string
	tell application "Finder"
		open "blahblahblahblah:Request Form.xls"
	end tell
	tell application "Microsoft Excel"
		set certF to value of (range "A4")
		set value of (range "A4") to (certF & "             " & certFrom)
		set occaS to value of (range "A6")
		set value of (range "A6") to (occaS & "             " & Occassion)
		set value of (range "B17") to recepAddress
		set value of (range "B20") to recepNumber
		set value of (range "A15") to nameS
		set value of (range "B8") to annivYears
		set value of (range "D8") to dateofAniv
		set value of (range "B22") to requestName
		set value of (range "B23") to requestAddress
		set value of (range "B25") to requestNumber
	end tell
	
end parseMessage

Hi,

selection in Mail.app results always a list, so either you select the first item of the list

 set theContent to the content of item 1 of theMessages as string

or you need a repeat block to proceed all messages

Thanks, but it seems to still not be doing anything, even if I hit “Apply Rules” on the message I want. (yes, the rules are set correctly, I also set them to move the message to a different mailbox)

for rules better use this syntax

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			-- do something
		end repeat
	end perform mail action with messages
end using terms from

Ah, thank you so much! After adding that and moving things around, this works:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				set theContent to the content of item 1 of theMessages as string
			end repeat
		end tell
		parseMessage(theContent)
	end perform mail action with messages
end using terms from

you’re welcome.
If you always want to parse only one message, you can totally omit the repeat block