1 2 Previous Next 25 Replies Latest reply: Apr 30, 2013 9:11 AM by John Galt Go to original post
  • 15. Re: AppleScript works for everything except Exchange server emails
    John Galt Level 8 Level 8 (36,395 points)

    Capital idea Frank!

     

     

    Screen Shot 2012-12-06 at 10.27.35 PM.png

     

    Austin - that makes sense. When the troublesome email arrives hopefully the above dialog will show something, because whatever arrives is apparently different than what the rule evaluates after the message resides in my Inbox. It would be nice to eliminate the parts between the brackets, which is the reason for converting it to a string. I suppose text should be no different. Clearly my script is objecting to making something "into type string". I have to find out what that something is.

  • 16. Re: AppleScript works for everything except Exchange server emails
    Austin Kinsella1 Level 6 Level 6 (11,505 points)

    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.

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

    I am beginning to think this is not fixable and the problem may be with Mail, more specifically the way it handles Rules, and may not be strictly limited to AppleScript or Exchange.

     

    Just wanted to keep this thread open until I have time to describe the reasons for this conclusion.

  • 18. Re: AppleScript works for everything except Exchange server emails
    twtwtw Level 5 Level 5 (4,690 points)

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

    set theSender to contents of (extract name from sender 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)

                                  say theSender

                                  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...

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

    Thanks twtwtw - I made that change and will let you know.

  • 20. Re: AppleScript works for everything except Exchange server emails
    Jim Strong Level 1 Level 1 (0 points)

    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

  • 21. Re: AppleScript works for everything except Exchange server emails
    Jim Strong Level 1 Level 1 (0 points)

    ..I should have added:- it doesn't matter where you place the "Every Message" rule, it seems to be affected by Rules later in the hierachy that move emails to other folders.

     

    Jim

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

    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.

     


  • 23. Re: AppleScript works for everything except Exchange server emails
    Jim Strong Level 1 Level 1 (0 points)

    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

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

     

     

    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 messages theMessages

                        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


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

    using terms from application "Mail"

              on perform mail action with messages theMessages

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

                                            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 dialog TheError buttons {"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

     

    Screen Shot 2013-04-30 at 12.02.25 PM.png

1 2 Previous Next