Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript works for everything except Exchange server emails

Here is the script:


using terms from application "Mail"

set theRuleName to name of theRule

on perform mail action with messagestheMessagesfor ruletheRule

tell application "Mail"

repeat with eachMessage in theMessages

set theSender to extract name from (sender of eachMessage)

say "Mail from " & theSender using "Alex"

end repeat

end tell

end perform mail action with messages

end using terms from



It is called from a Mail rule. Here is the rule:


User uploaded file


It works for every incoming message except for those that originate from an Exchange account.

Here is the interesting part: If I manually "apply rules" by selecting the message and using Option ⌘ L then it speaks the name perfectly. It is only when the mail arrives (by checking for new mail, for instance) that the script does not appear to do anything.


More interesting: The email does get its background color set to Blue. So as far as I can tell, the Rule is being executed, but the script isn't.


Here is the Rule for everything else:


User uploaded file


(speakname5 is identical to speakname6 just without the voice "Alex" - here it is anyway)


using terms from application "Mail"

set theRuleName to name of theRule

on perform mail action with messagestheMessagesfor ruletheRule

tell application "Mail"

repeat with eachMessage in theMessages

set theSender to extract name from (sender of eachMessage)

say "Mail from " & theSender

end repeat

end tell

end perform mail action with messages

end using terms from

If I try to simply use the same Rule for everything it still does not work for the Exchange email account. That is the reason for two separate scripts - it was a troubleshooting attempt.


Why does it only work when manually "applying Rules"?


Is there anything else I can include in the speakname6 script to indicate that it is actually being called?


Here are all my Rules, so that their order is known:


User uploaded file

MacBooks  iMacs  iPods  AirPorts-OTHER, OS X Mountain Lion,  27 years Apple!

Posted on Nov 20, 2012 5:43 PM

Reply
Question marked as Best reply

Posted on Nov 21, 2012 11:20 AM

John

for debugging you could stick a beep in the script. What happens if you try speaking the subject rather than the sender?

27 replies

Dec 7, 2012 1:57 AM in response to John Galt

I didn't suggest the display dialog ... as a few years ago with a similar exercise it seemed to crash both Mail and Applescript, possibly because of the delay in the human response - your script can't continue until you press OK. Also, you can't use log, as that writes to Applescript Editor's window (I think) - you will have to open for access a disk file.

Feb 11, 2013 8:29 AM in response to John Galt

Two things to try. First, add in a 'contents of' command:

set theSender to contents of (extract name fromsender of eachMessage)

Mail doesn't send actual values via applescript; it sends pointers to internal objects, and sometimes applescript has trouble following the indirections. 'contents of' should dereference the pointer so that you get the text value.

The other possibility is that Exchange is sending headers in an odd order and the script is trying to process them before the the sender header arrives. to test that, throw in a delay loop:

repeat with i from 1 to 10

try

set theSender to contents of (extract name from sender of eachMessage)

saytheSender

exit repeat

on error

delay 1

end try

if i = 10 then

say "uh oh."

exit repeat

end if

end repeat

That will try ten times to speak the name over as many seconds.

Also, you don't have to wait for an email to arrive at the exchange server. Take your other mail accounts offline (so that you don't get distractions), and then send a test email to yourself at the exchange server. A bit like bouncing a tennis ball off a wall...

Apr 19, 2013 7:42 AM in response to John Galt

Hello John


I don't know if this is going to help, but I've been successfully been using your script for some time but increasingly I've been noticing a problem in Mountain Lion that I think I have just pinned down:-


I have a Gmail IMAP account and a rule to run your script on "Every Message". If the message remains in the Gmail IMAP Inbox, the script runs well and reads the sender's name. However, if I have a rule to file a message into a local folder, the script either does not speak a name - or it reads the name of whatever message is the top email in the Gmail IMAP Inbox.


In other words, I believe by "Every Message" Apple Mail in Mountain Lion in fact means "Every Message in your default Inbox".


Like you, the rule and script work perfectly when manually run against a selected message.


I've no idea if this will help your Exchange proiblem, but thought it might assist? :-)


Jim

Apr 21, 2013 10:29 AM in response to Jim Strong

Thank you Jim, I had not considered the implications of "every message". I chose it only because it seemed like the right one. I doubt it will change anything but I deleted that one and added two others instead: "if message is addressed to my full name" and its complement "if message is not addressed to my full name". I'll see what that does. All I want it to do is run on every message.


Right now I'm using a barely acceptable workaround, which is to explicitly check if the mail is from "x corporation", and then say "mail from x corporation". However, I occasionally receive emails from other ones that I believe are also Exchange servers, and my script results in the Mac speaking the following:


"Unrecognized parameter type. Please specify a string like John Doe at example dot com"


At least Mountain Lion is giving me a hint I can work with. I only wish I knew what the parameter was that it's trying to read, because whatever it is, it's not the right one.


Apr 21, 2013 10:51 AM in response to John Galt

I think I might have tried that instead of using "every message" but have a sneaky suspicion that Mountain Lion might also be looking in the default inbox for messages addressed to my full name etc. What I have now done is - instead of moving items from my inbox to my local folder, I copy them instead - and then later delete them from the inbox.


So far that seems to be working, but I do wish these workarounds were not necessary - or at least that Apple's release notes gave some better clues as to what the heck they had done to fix other reported problems!! :-)


Good luck with the parameter hunt!


Jim

Apr 21, 2013 2:00 PM in response to twtwtw

😟


Much testing but little progress to report twtwtw.


The result of the following is "Mega corporation rule applied... script entry... unable to extract". "Begin extraction" does not get announced, so the initial try obviously fails and it goes right to the error. However, the dialog box with title "Error" that follows does not even appear!


It's just rude.


This Rule is specifically invoked when the sender is "Megacorp". If I apply this same Rule to all other senders I get the nice announcement I desire, the dialog boxes, everything, just as expected...


To review: I can't find a way to extract the sender's name as it arrives from the Exchange server. If I apply the Rule containing this script after it is received, everything works as I intend.


There is apparently something unique about these emails. Perhaps its content is encrypted and is not de-encrypted until after it is displayed in my Inbox, and that's the reason sender cannot be extracted no matter how I try. Could that be correct?





using terms from application "Mail"

on perform mail action with messagestheMessages

tell application "Mail"

repeat with eachMessage in theMessages

say "Mega corp Mail rule applied."

delay 1

repeat with i from 1 to 10

say "Script entry."

delay 1

try

say "Begin extraction."

set theSender to sender of eachMessage as string

set theSubject to (extract name from subject of eachMessage)

say "Mail from " & theSender

display dialog "The sender is " & theSender & "The subject is " & theSubject buttons {"OK"} default button "OK" with title "Success"

set theSender to contents of (extract name from sender of eachMessage)

say "Mail from " & theSender

exit repeat

on error TheError

say "Unable to extract."

delay 1

display dialog "The sender is " & theSender & "The subject is" & theSubject buttons {"OK"} default button "OK" with title "Error"

exit repeat

end try

say "Begin delay."

if i = 10 then


display dialog "I give up." buttons {"OK"} default button "OK" with title "Timeout"

exit repeat

end if

end repeat

end repeat

say TheError using "Alex"

end tell

end perform mail action with messages

end using terms from

Apr 30, 2013 9:11 AM in response to twtwtw

using terms from application "Mail"

on perform mail action with messagestheMessages

tell application "Mail"

repeat with eachMessage in theMessages

say "Mega corp Mail rule applied."

say "Script entry."

try

say "Begin extraction."

set theSender to sender of eachMessage as string

set theSubject to (extract name fromsubject of eachMessage)

say "Mail from " & theSender

display dialog "The sender is " & theSender & "The subject is " & theSubject buttons {"OK"} default button "OK" with title "Success"

set theSender to contents of (extract name from sender of eachMessage)

say "Mail from " & theSender

on error TheError

say "Unable to extract."

display dialog "Unable to extract." buttons {"OK"} default button "OK" with title "Error"

end try

end repeat

say TheError using "Alex"


display dialogTheErrorbuttons {"OK"} default button "OK" with title "Here's why"

end tell

end perform mail action with messages

end using terms from


The above script is never able to extract the name of the sender as the email arrives. The dialog box describing the error is


User uploaded file

Aug 25, 2014 6:44 PM in response to John Galt

I've given up on this for now but if anyone has any better ideas let me know.


The script as it is:


using terms from application "Mail"

on perform mail action with messages theMessages

tell application "Mail"

repeat with eachMessage in theMessages

try

say "Mail from " & (extract name from sender of eachMessage)

on error TheError

say "Unable to extract."

say TheError using "Alex"

end try

end repeat

end tell

end perform mail action with messages

end using terms from


New ASC format renders formatted copy and paste unreadable.

AppleScript works for everything except Exchange server emails

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.