1 2 Previous Next 25 Replies Latest reply: Apr 30, 2013 9:11 AM by John Galt
John Galt Level 8 Level 8 (36,415 points)

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!
  • 1. Re: AppleScript works for everything except Exchange server emails
    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?

  • 2. Re: AppleScript works for everything except Exchange server emails
    John Galt Level 8 Level 8 (36,415 points)

    Excellent idea!

     

    Here is my revised 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

                                            say "script entry"

                                            set theSender to (extract name from sender of eachMessage)

                                            set theSubject to (extract name from subject of eachMessage)

                                            say "Mail from " & theSender & "Regarding " & theSubject using "Alex"

                                            say "script exit"

                                  end repeat

                        end tell

              end perform mail action with messages

    end using terms from

     


     

    This caught Mail red-handed with another failure to speak the sender's name from the Exchange account: when the Exchange email arrived, I heard "script entry" and nothing more.

     

    Therefore it would appear that the script executes, but something is different about extracting the sender from an Exchange account vs. others.

     

    Since I never heard "script exit" I conclude something in the above causes the entire script to abort... yet manually "applying rules" works just fine after the email is received. Is there a Console entry or other log that may be enlightening?

     

    Other emails announce the name of the sender as well as the subject as you suggested.

     

    Comparing the raw source of an Exchange email to ones that work obviously reveals many differences, but it still has a line that begins with "From:" just like everything else. It's obviously in a different place.

     

    So what is different about extract name from for an Exchange email than others? Is there another way to parse the source of an Exchange email?

  • 3. Re: AppleScript works for everything except Exchange server emails
    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!


  • 4. Re: AppleScript works for everything except Exchange server emails
    John Galt Level 8 Level 8 (36,415 points)

    I'll try that.

     

     

    Austin Kinsella1 wrote:

     

    I'm not sure that

    set theSubject to (extract name from subject of eachMessage)

    will work.

     

    It works for everything else, in the same manner that extract name from sender of eachMessage works for extracting the sender's name, but I will try

     

    set theSender to ( sender of eachMessage) as text

     

    for both sender and subject. Your suggestion is certainly simpler.

     

    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.

     

    I suspected that. I will comment them out. I deleted them and they were superfluous as you said.

     

    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!

     

    I'll let you know!

     

    What should I expect from (count of theSender) ?

     

     

  • 5. Re: AppleScript works for everything except Exchange server emails
    John Galt Level 8 Level 8 (36,415 points)

    set theSender to ( sender of eachMessage) as text

     

    This is not ideal since it speaks (for example) "Apple Support Communities Updates discussions updates at apple dot com"

     

    instead of simply "Apple Support Communities Updates" which is what I prefer, by using

     

    set theSender to (extract name from sender of eachMessage)

     

    .


    extract name from appears to extract just the name - without the email address part.

     


     

    Odd observation: upon compiling the script AS changed text to rich text:

     

    set theSender to (sender of eachMessage) as rich text


    .

     

    Message was edited by: John Galt

  • 6. Re: AppleScript works for everything except Exchange server emails
    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!

  • 7. Re: AppleScript works for everything except Exchange server emails
    John Galt Level 8 Level 8 (36,415 points)

    say (count of theSender)

     

    ... always returns a non-zero result. I am not sure of its significance. The number ranged from eighteen to twenty-four to thirty-three. It may be the total number of emails from that sender in my Inbox.

     

    Austin Kinsella1 wrote:

     

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

     

    That is a good point too, but I think it's a red herring in this case.

     

    There seem to be two varieties of "From" in an email raw source: those that include a name as well as an email address, and those that include only an email address. For example, today's email from Apple regarding their one day shopping event contains

     

    From: Apple <News@InsideApple.Apple.com>

     

    ... and the only part that is extracted and spoken with my script is "Apple".

     

    If the sender's name is not included as in the form above, the entry might resemble this:

     

    From: Dilbert@MegaCorp.com

     

    ... and the name will be spoken as "Dilbert at MegaCorp dot com".

     

    So I think the use of

     

    extract name from sender of eachMessage

     

    is still OK.


    Remember that the script executes fine if I manually "apply rules" to the message after it is received. Something else is going on with the way Exchange is doing things.

     

    Anyway, you gave me some more ideas to try... thanks!

     

     

  • 8. Re: AppleScript works for everything except Exchange server emails
    John Galt Level 8 Level 8 (36,415 points)

    Here is my revised, simply awful, script:

     

    using terms from application "Mail"

              on perform mail action with messages theMessages

                        tell application "Mail"

                                  repeat with eachMessage in theMessages

                                            say "script entry"

                                            set AppleScript's text item delimiters to {""}

                                            set theSender to sender of eachMessage as string

                                            set brkpos to offset of "<" in theSender

                                            if (brkpos is greater than 1) then

                                                      say "break position is greater than one "

                                                      set theSender to (text items 1 through (brkpos - 1)) of theSender as string

                                                      if theSender contains "\"" then

                                                                say "the sender contains a quote "

                                                                set AppleScript's text item delimiters to {"\""}

                                                                set theSender to text item 2 of theSender as string

                                                      end if

                                            else

                                                      say "break position is less than or equal to one "

                                                      if (brkpos is equal to 1) then

                                                                say "break position is equal to one "

                                                                set theSender to (text items 2 through -1) of theSender as string

                                                      end if

                                                      set AppleScript's text item delimiters to {"@"}

                                                      set theSender to text item 1 of theSender as string

                                            end if

                                            say "end break position test  "

                                            say "Mail from " & theSender

                                            say "script exit"

                                  end repeat

                        end tell

              end perform mail action with messages

    end using terms from


     

     

    The result of all this is the same... a tortured way to extract the sender's name, which works but the results are exactly the same as the other one.


    When an Exchange email arrives, the above script speaks "script entry" and that is the last I hear from it. All other emails speak the sender's name just fine. As before, manually "applying rules" after the email is received speaks the sender's name as I would expect.

     

    So it is getting lost prior to "end break position test" but I have no idea why.

  • 9. Re: AppleScript works for everything except Exchange server emails
    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

  • 10. Re: AppleScript works for everything except Exchange server emails
    John Galt Level 8 Level 8 (36,415 points)

    Getting closer... the error handler caught something, but several emails arrived at once, and all I could decipher from the error handler was "can't make... application(?) ...  (indecipherable) ... into type string."

     

    I do not receive emails from the Exchange-centric puzzle palace often so I will have to wait until I receive another. Clearly there was something that could not be resolved into a string. The fact the script can resolve it after the email is received remains a mystery for now.

     

     

    set OldDelim to Applescript's script item delimiters

    ...

    set Applescript's script item delimiters to OldDelim

     

    I did not do that yet.

  • 11. Re: AppleScript works for everything except Exchange server emails
    John Galt Level 8 Level 8 (36,415 points)

    John Galt wrote:

     

    Getting closer... the error handler caught something, but several emails arrived at once, and all I could decipher from the error handler was "can't make... application(?) ...  (indecipherable) ... into type string."

     

    The error is "Can't make type sndr of item n of count application into type string".

     

    where sndr is spoken as it is spelled:  "S N D R"

     

    and n = a number that began with 1 and was incremented by one up to 10. The error as it was spoken was

     

    "Can't make type sndr of item n of count application count application count application... (this was repeated several times)... into type string."

  • 12. Re: AppleScript works for everything except Exchange server emails
    John Galt Level 8 Level 8 (36,415 points)

    John Galt wrote:

     

    The error is "Can't make type sndr of item n of count application into type string".

     

    Correction - not "count application" but current application

  • 13. Re: AppleScript works for everything except Exchange server emails
    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.

  • 14. Re: AppleScript works for everything except Exchange server emails
    Frank Caggiano Level 7 Level 7 (23,865 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