Applescript for getting email text

Hello, I am new to this forum and I have a basic question that may be pretty evident for some of you.

I have written an Automator work flow that triggers every time I get an email from a particular sender, subject and keyword in the message text.
I need an Applescript that stores the message text (of the email that triggered the Automator work flow) into a text variable for further processing.

I did a Google search and apparently Applescripts that use email rules don’t work with Leopard anymore. Is that true?

Any guidance or link to the solution will be greatly appreciated.

Thanks a lot for your time!

Model: MacBook Pro
AppleScript: 2.0.1
Browser: Safari 525.28.3
Operating System: Mac OS X (10.5)

Never mind, I figure it out. This is the script for what it is worth.

tell application “Mail” to get content of first message of inbox
set theMessageText to result

Can anybody see any “danger” in doing it this simple?

Thanks again :slight_smile:

Model: MacBook Pro
AppleScript: 2.0.1
Browser: Safari 525.28.3
Operating System: Mac OS X (10.5)

No, you can even do it in one line


tell application "Mail" to set theMessageText to content of (get first message of inbox)

Why would you refer to the message as “(get first message of inbox)”?

You said initially that you were running this from an automator workflow that triggers when you get an email and it already filters out by “keyword in the message text”. It sounds like you already get the message text, so why not just write it to a file right then-and-there since you already have it?

My worry would be with “(get first message of inbox)” that the email you actually want to target is not the first message any longer… maybe you receive new emails as you’re processing that email.

Thank you for your reply and input.
I have set up a Mail rule (on Mail preferences) that checks for a sender, subject and ‘keyword’ in the text, of a mail message that just arrived via Apple’s Push and triggers an automator app if it finds a match. The ‘keyword’ is actually a password that the sender has to type in the message. When the automator starts I had to find the username of the sender which is contained in the message. At that point I don’t have the message text. With the script I posted (or the simpler from you) I get the text of the message and can parse for the username. Hope it makes sense.
I know there could be a theoretically possible risk that the ‘first message in the mail box’ may not be the message that triggered the script. I am hoping that the response is fast enough that that will be the case and that the push Mail feature does its job properly. Do you think there is a better and fail safe way of doing it?
Thanks a lot for your time.

I think this might do what you want


set myMail to GetRecentSpecialMail("password")

tell application "Mail"
	if myMail is false then
		-- no message in inbox with keyword
		set myText to ""
	else
		set myText to content of myMail
	end if
end tell

on GetRecentSpecialMail(keyWord)
	tell application "Mail"
		set closestMatch to false
		set currentHigh to "Jan 1 1900"
		repeat with receivedMessage in messages in inbox
			set receivedDate to date received of receivedMessage
			set messageContent to (get content of receivedMessage)
			
			if (messageContent contains keyWord) ¬
				and (currentHigh < receivedDate) then
				set currentHigh to receivedDate
				set closestMatch to receivedMessage
			end if
		end repeat
		
		return closestMatch
		
	end tell
end GetRecentSpecialMail

No, this is not true. Here’s one that works fine. Notice that it gets the email message, opens a new TextEdit document and sets the email message as the text of the TextEdit document. But this is what I meant… just get the message text right in the email rule.

using terms from application "Mail"
	on perform mail action with messages TheMessages for rule theRule
		repeat with aMessage in TheMessages
			tell application "Mail"
				tell aMessage
					set emailAddress to extract address from sender
					set emailSubject to subject
					set emailDate to date received
					set emailContent to content -- this is the message of the email
				end tell
			end tell
			
			tell application "TextEdit"
				activate
				set myDoc to make new document
				set text of myDoc to emailContent
			end tell
		end repeat
	end perform mail action with messages
end using terms from

This is true: Applescripts which send messages in a mail rule don’t work with Leopard anymore

mikerickson: that’s a great idea and fail safe too. Will give it a shot. Thanks a lot!

regulus6633: Ah, now I understand what you meant, that a great idea too. Will try it also. Thanks!

StefanK: Thank you for the clarification. So sending emails is what is broken in Leopard, not reading text. Thanks.

What a great site, thank you all.

mikerickson,
Just a note to let you know that your script worked perfectly, thank you. One last thing, is there a way to just check, lets say, the last 10 messages, instead of the whole inbox? If there are a lot of messages in the inbox the process may take a long time. Thanks again.

You could have it search your smart mailbox.

Alternatly, you could put the Return line inside the loop, so it will return the “first” msg with the keyword.

I suspect that inbox is organized so that Repeat With loop starts with the most recent msg. (But, as my code shows, as a rule, I don’t trust such quirks.)

To answer your question, No, I don’t know a way (other than looping) to reliably create a list of the last 10 received messages.

I needed to get home to my Mac, but if you believe that my suspicion about Inbox being organized by date, this will return the 10 most recently received messages

items 1 thru 10 of (messages of inbox as list)

I checked your last script and it returns the emails arranged in a descending order according to the “message ID” number. I think it is logical to assume that the email with the highest message id is the latest.

Here is the returned list (I ran it for just 5 messages, not 10 and I xxx the personal info):

{message id 53803 of mailbox “INBOX” of account “xxx .Mac Account” of application “Mail”, message id 53793 of mailbox “INBOX” of account “xxx .Mac Account” of application “Mail”, message id 53792 of mailbox “INBOX” of account “xxx .Mac Account” of application “Mail”, message id 46904 of mailbox “INBOX” of account “xxx .Mac Account” of application “Mail”, message id 46896 of mailbox “INBOX” of account “xxx .Mac Account” of application “Mail”}

I sent myself a bunch of emails with and without the password and verified that putting the return line inside the loop, as you suggested, brings the latest dated email that has the password, so it works well and now it’s pretty fast (yeah!). This confirms that the emails are arranged from latest to oldest date. What I will do next is to add a limit counter in the loop, for the highly unlikely case that there is no email with the password (!!??). This will avoid the possibility of checking all the emails of the inbox which could take a very long time. Thanks for your great input solving my problem.

Another approach would be to go through the list (returning the first key-worded message) but stop when the messages are more than a day old or some other specified time interval.

on GetRecentSpecialMail(keyWord)
	tell application "Mail"
		
		repeat with receivedMessage in messages in inbox
			set receivedDate to date received of receivedMessage
			set messageContent to (get content of receivedMessage)
			if ((current date) - receivedDate) < 24 * hours then
				if (messageContent contains keyWord) then return receivedMessage
			else
				return false
			end if
		end repeat
		
		return false
		
	end tell
end GetRecentSpecialMail

That’s even better. Limit with time the correct way of doing it. It works great!
Thanks.

This is exactly what I am looking for, however, how does Automator Workflow get set up to use Microsoft Outlook for Mac.

Additionally, how does Microsoft Outlook for Mac know when to run a Workflow from Automator, what is the process for that?