3

I have the following AppleScript triggered by a Mail.app rule:

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        repeat with msg in theMessages
            set theText to subject of msg & return & content of msg & date sent of msg
            display dialog (theText)
        end repeat
    end perform mail action with messages
end using terms from

If I select a message, right click and choose 'Apply Rules' it works properly. However, if the script is triggered by an incoming message, it seems to have a random message in theMessages.

Here is the rule:enter image description here

How do I get the right message?

I'm using High Sierra with Mail 11.2.

ghenne
  • 1,903
  • 2
  • 19
  • 29

2 Answers2

0

As your script will iterate over your mail, I expect that your messages are not sorted by date ... So when you run you script it will take the first element (and not the most recent)


Could you run Mail, then sort your email by date (with the most recent at top position) then quit and re-run Mail (to double-check that configuration was saved)

Then verify if your script works.


If you don't want to set the filter manually, according to this, you may add at the following script to the beginning :

tell application "System Events" to click menu item "Date" of menu "Sort By" of menu item "Sort By" of menu "View" of menu bar item "View" of menu bar 1 of process "Mail"

to sort email by date before running your script in order to get the right message.

You may also take a look here, here and here to verify and double check that the rules are properly setup.

A. STEFANI
  • 6,707
  • 1
  • 23
  • 48
  • This solution does not work. Even after sorting, it still picks a random message. Remember, the script is being triggered by a mail rule on an incoming transaction. We're expecting to see the message selected by the rule, but getting a different message, – ghenne Jan 24 '18 at 11:46
  • Could you make a print screen of the rule, because it appear that the rule condition is not properly setup ? – A. STEFANI Jan 24 '18 at 11:51
  • Image added to original post. – ghenne Jan 24 '18 at 12:51
  • Write it in one line like it's supposed to be!: `tell application "System Events" to click menu item "Date" of menu "Sort By" of menu item "Sort By" of menu "View" of menu bar item "View" of menu bar 1 of process "Mail"` – user3439894 Jan 24 '18 at 13:45
  • Indentation may help sometimes, but I change according to your advice. @user3439894, have you any idea why his filter is not working ? – A. STEFANI Jan 24 '18 at 13:50
  • Yes, indentation and judicious use of white space is very helpful when coding, however a single System Event to click a menu item in the manner you posted is IMO a poor way to code a single event such as that. If you want it not to be on one line, you can break it down as in this example https://paste.ee/p/kfKkN , either way is better then how originally posted. That said, to answer the question in your comment... Since I'm not in a position to test the OP's code in Mail, I really can't effectively troubleshoot the issue. Although, for a +100 bonus I really wish I could test it. – user3439894 Jan 24 '18 at 14:12
  • To clarify what I have done, I sorted the mail list manually in the app. I did not try sorting using the script, since it would be redundant. – ghenne Jan 24 '18 at 14:17
  • Can you try to add the condition rules : "Status | is Unread" and remove the action "stop evaluating rules", and add an action to "move email to a folder", to avoid your filter to treat indefinitely the same email and then stop. – A. STEFANI Jan 24 '18 at 14:37
  • Mail.app does not have the option to check mail status as unread. Your other suggestions made no difference. – ghenne Jan 24 '18 at 21:10
0

Apparently, handling incoming messages with rules is an asynchronous process. When on perform mail action is called, the message is not yet completely updated. Only partial data is available immediately.

A possible workaround would be to add a delay 1 into the script. This give Mail a second to finish updating the message. Here is what the script then looks like:

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        repeat with msg in theMessages
            -- delay a bit of time for msg to load
            delay 1

            set theText to subject of msg & return & content of msg & date sent of msg

            — do other processing

        end repeat
    end perform mail action with messages
end using terms from
ghenne
  • 1,903
  • 2
  • 19
  • 29