Skip navigation

AppleScript works for everything except Exchange server emails

1764 Views 25 Replies Latest reply: Apr 30, 2013 9:11 AM by John Galt RSS
1 2 Previous Next
John Galt Level 7 Level 7 (33,090 points)
Currently Being Moderated
Nov 20, 2012 5:43 PM

Here is the script:

 

using terms from application "Mail"

          set theRuleName to name of theRule

          on perform mail action with messages theMessages for rule theRule

                    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:

 

Screen Shot 2012-11-20 at 8.30.21 PM.png

 

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:

 

Screen Shot 2012-11-20 at 8.28.13 PM.png

 

(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 messages theMessages for rule theRule

                    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:

 

Screen Shot 2012-11-20 at 8.41.55 PM.png

MacBooks  iMacs  iPods  AirPorts, OS X Mountain Lion,  27 years Apple!
  • Austin Kinsella1 Level 6 Level 6 (11,505 points)

    John

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

  • Austin Kinsella1 Level 6 Level 6 (11,505 points)

    I'm not sure that

    set theSubject to (extract name from subject of eachMessage)

    will work. In a similar script I have

    set theSubject to ( subject of eachMessage) as text

    So try that, and comment out set theSender ... and change the say to say theSubject

    just to verify that that works.

     

    Also, you don't need

    set theRuleName to name of theRule

    and you could take for rule theRule off the next line - you're not using either of them.

     

    It might be interesting, as a separate test, to set theSender to senter of eachMessage (so not using extract) and then maybe say (count of theSender) to see if there is anything to be said!


  • Austin Kinsella1 Level 6 Level 6 (11,505 points)

    The idea of the count of the sender is to see if there is in fact anything there  - a positive number is what you want, but if it were to be 0 then you are not getting  anything useful from set theSender ...

     

    By omitting the extract name from part you are, hopefully, verifying that there is something to extract!

  • Austin Kinsella1 Level 6 Level 6 (11,505 points)

    Time for some error handling?

     

    after say "script entry" put in a line with just try

    before end repeat put in 3 lines:

    on error TheError

    say TheError

    end try

     

    Also, as I understand it, a script will use the last set script delimiters, even if in a different script, so it is considered good practice at the start of a script to remember, and at the end of a script to restore, so you don't break some other script:

    set OldDelim to Applescript's script item delimiters

    ...

    set Applescript's script item delimiters to OldDelim

  • Austin Kinsella1 Level 6 Level 6 (11,505 points)

    Hmm. Try leaving out as as string in

    set theSender to sender of eachMessage as string

     

    sender is supposed to be text anyway, so maybe that is the problem.

    Mac OS X (10.6.8), 4GB RAM; 500GB HD
  • Frank Caggiano Level 7 Level 7 (22,820 points)

    John,

     

    Just a quick thought for troubleshooting purposes. I would get rid of the says and write the extracted data (as well as any errors) to a log file or at the very least a display dialog.

     

    I'd also have the rule printout both the raw data and what is gotten after any extracts or other manipulations.

1 2 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.