Monday, June 25, 2018

#1 2009-06-06 03:48:57 pm

gtl
Member
Registered: 2009-06-06
Posts: 8

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)

Offline

 

#2 2009-06-06 05:36:42 pm

gtl
Member
Registered: 2009-06-06
Posts: 8

Re: Applescript for getting email text

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 :-)

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


Filed under: mail

Offline

 

#3 2009-06-07 01:03:14 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11498
Website

Re: Applescript for getting email text

gtl wrote:

Can anybody see any "danger" in doing it this simple?


No, you can even do it in one line

Applescript:


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


regards

Stefan


Filed under: mail

Offline

 

#4 2009-06-07 01:34:41 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: Applescript for getting email text

gtl wrote:

Can anybody see any "danger" in doing it this simple?


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.

Offline

 

#5 2009-06-07 07:37:12 pm

gtl
Member
Registered: 2009-06-06
Posts: 8

Re: Applescript for getting email text

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.

Offline

 

#6 2009-06-07 09:11:50 pm

mikerickson
Member
Registered: 2008-10-26
Posts: 437

Re: Applescript for getting email text

I think this might do what you want

Applescript:


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

Last edited by mikerickson (2009-06-07 09:27:03 pm)


Filed under: mail

Offline

 

#7 2009-06-08 05:22:45 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: Applescript for getting email text

gtl wrote:

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


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.

Applescript:

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


Filed under: mail, TextEdit

Offline

 

#8 2009-06-08 07:33:26 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11498
Website

Re: Applescript for getting email text

gtl wrote:

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


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


regards

Stefan

Offline

 

#9 2009-06-08 02:51:08 pm

gtl
Member
Registered: 2009-06-06
Posts: 8

Re: Applescript for getting email text

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.

Offline

 

#10 2009-06-09 04:27:46 pm

gtl
Member
Registered: 2009-06-06
Posts: 8

Re: Applescript for getting email text

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.

Offline

 

#11 2009-06-09 04:40:24 pm

mikerickson
Member
Registered: 2008-10-26
Posts: 437

Re: Applescript for getting email text

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.

Last edited by mikerickson (2009-06-09 04:40:54 pm)

Offline

 

#12 2009-06-09 05:19:54 pm

mikerickson
Member
Registered: 2008-10-26
Posts: 437

Re: Applescript for getting email text

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

Applescript:

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

Offline

 

#13 2009-06-09 05:40:36 pm

gtl
Member
Registered: 2009-06-06
Posts: 8

Re: Applescript for getting email text

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"}

Offline

 

#14 2009-06-09 06:01:48 pm

gtl
Member
Registered: 2009-06-06
Posts: 8

Re: Applescript for getting email text

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.

Offline

 

#15 2009-06-09 06:35:49 pm

mikerickson
Member
Registered: 2008-10-26
Posts: 437

Re: Applescript for getting email text

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.

Applescript:

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

Last edited by mikerickson (2009-06-09 06:37:48 pm)


Filed under: mail

Offline

 

#16 2009-06-09 09:12:14 pm

gtl
Member
Registered: 2009-06-06
Posts: 8

Re: Applescript for getting email text

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

Offline

 

#17 2017-12-20 10:17:27 am

Bragger
Member
Registered: 2017-12-19
Posts: 1

Re: Applescript for getting email text

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?


Filed under: microsoft

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)