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

Why is Mail passing the wrong message list to AppleScript?

For a number of years, I've been using an AppleScript to fire off a Growl notification when I receive a message in Mail from certain senders. Although it's very similar to a script provided at Growl.info for the very same thing, I'm finding that it's not working properly since I upgraded to Mountain Lion. The Mail rule that runs the AppleScript is very simple: a list of (any) conditions with a single Run AppleScript action.


AFAIK, this rule should only pass to the AppleScript those messages that match the conditions (some "Subject contains", some "From contains"). However, I'm getting Growl notifications of messages that arrive at the same time as messages that should match the rule. For example, I get two messages in, one of which has a sender of "johnsmith@example.com" and another from "marysue@domain.net", and I get notified of the John Smith message when it's Mary Sue who is in the Mail rule. (I don't get notified of the Mary Sue message.)


Here's the AppleScript:


using terms from application "Mail"

on perform mail action with messagestheMessagesfor ruletheRule

tell application "Mail"

repeat with eachMessage in theMessages

set theSubject to subject of eachMessage

set theSender to sender of eachMessage

set theMessage to "You have received a message from " & theSender & "."


tell application "Growl"

set theName to "New Message"

set theAppName to "Custom Mail Notifications"

set the allNotificationsList to {theName}

set the enabledNotificationsList to {theName}

registeras applicationtheAppName ¬

all notificationsallNotificationsList ¬

default notificationsenabledNotificationsList ¬

icon of application "Mail.app"

notifywith nametheName ¬

titletheSubject ¬

descriptiontheMessage ¬

application nametheAppName ¬

with sticky

end tell

end repeat

end tell

end perform mail action with messages

end using terms from



I don't believe the problem is in the script, since theMessages shouldn't include the John Smith message, but I don't have any other explantion. Could someone else hazard a guess at what is going on here? Thanks!

iMac (20-inch Early 2008), OS X Mountain Lion (10.8.2), 6GB, 500GB internal, 4TB external

Posted on Feb 18, 2013 2:06 PM

Reply
7 replies

Apr 10, 2013 12:54 PM in response to Don Morris

Okay, now this is just boggling. After including a line that logs the message subject and the count of theMessages to the System Log, I can see that Mail is often only passing one message at a time to the script and the message is not one that should have triggered the rule. Not only that, but sometimes theSubject and theSender are also not from the same message! For example:


User uploaded file


4/10/2013 11:56:34.966 AM GrowlMessage[28627]: Message is cheriewri has registered at $7 S... 1

(The '1' at the end of the log message is the count of theMessages.)


In the Growl notification above, the sender is from one message, the subject (the title of the notification) is from another message received at the same time, and the actual message that triggered the rule is yet another message. Message 3 triggered the rule, the log shows message 2 running through the script, and the notification shows the sender from message 1.


What the heck is going on!?

Sep 2, 2013 7:01 PM in response to Don Morris

Did you ever track this one down? I'm trying to use a simple AppleScript to file my messages as part of a rule so I could add a delay before the message is moved. It seems that the rule processing happens so fast that it occasionally corrupts messages in my primary IMAP account (and I don't get notifications either), sometimes splicing in the header from a completely different message, or treating attachments as raw text. Rebuilding the mailbox fixes it, but then I have to reapply all my other rules for coloring and such, and disable all my filing rules to make sure manually filed messages don't get moved out, etc.


So anyway, I set up the following simple script, with different ones set to move messages to specific folders based on the parameters of the mail rulle. Unfortunately, when I turned on the computer this morning and four messages that came in overnight were downloaded, they were all moved to the wrong folders. Selecting the messages and applying the rules manually did fix them, but this sounds very similar to your problem. Could it be something in the repeat parameter that is causing it to get different "theMessages" as it executes? I tried removing the repeats from the script and it then only kind of works. Sometimes it does what it's supposed to but other times it doesn't move any messages.


using terms from application "Mail"

on perform mail action with messagestheMessagesfor ruletheRule

tell application "Mail"

delay 3

repeat with eachMessage in theMessages

move eachMessage to mailbox "Other" of account "name@domain.com"

end repeat

end tell

end perform mail action with messages

end using terms from

Sep 7, 2013 5:03 AM in response to Don Morris

It's been a long time since I scripted growl, but at a guess I'd say you're tripping over a race condition. Mail passes email references to rule scripts lazily as they arrive, growl is designed to operate asynchronously, and so there's plenty of room for confusion. To fix this, try breaking references before you send data to growl; in other words, make sure you're sending growl simple text strings rather than references to mail message object properties. you can do that with the get command:


set theSubject to (get subject of eachMessage)

set theSender to (get sender of eachMessage)


or maybe:


set theSubject to (get subject of eachMessage) as rich text

set theSender to (get sender of eachMessage) as rich text

You might also get rid of the register command. You only need to do that once, and while it normally doesn't hurt to do it every time, in this case it might be slowing things down.

Why is Mail passing the wrong message list to AppleScript?

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