Apple Event: May 7th at 7 am PT

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

Why doesn't this simple applescript work any more in ML mail.app (as a rule)

It works only when you right click on the mail message and choose "run rules", but not on incoming messages (without interaction).



The first dialog is shown both when incoming or running manually, but the second dialog (with the id) is only shown when running manually. Nothing is shown in console.log



Any ideas?



using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        tell application "Mail"
            repeat with theMessage in theMessages
                display dialog "inside"


                set theId to id of theMessage


                display dialog "the id is " & theId


            end repeat
        end tell
    end perform mail action with messages
end using terms from

Posted on Jan 26, 2013 7:58 AM

Reply
9 replies

Jan 26, 2013 9:50 AM in response to wesleyh84

Might it be that any incoming message doesn't have an ID yet ?


Try this:


using terms from application "Mail"

on perform mail action with messagestheMessagesfor ruletheRule

tell application "Mail"

repeat with theMessage in theMessages

display dialog "inside"

set theTest to (existsid of theMessage)

display dialogtheTest

end repeat

end tell

end perform mail action with messages

end using terms from

Jan 26, 2013 10:34 AM in response to Pierre L.

That's exactly it! Thx Pierre. But is there any way around this? I need to find the file on disk to copy to a different location:



set theEmail to (do shell script "mdfind -onlyin ~/Library/Mail \"kMDItemFSName = '" & theId & ".emlx'\"")


set archiveName to theId & "-" & (extract address from theMessage's sender) & ".emlx"

set saveLocation to "Users:wesley:Documents:Incoming:"


do shell script "cp '" & theEmail & "' '" & POSIX path of saveLocation & "';"

Jan 26, 2013 12:40 PM in response to Pierre L.

ok, this seems to be a weird bug with placing the repeat command right after the tell statement. add any command between the two, or reverse their order, and it works again. for instance, this works on my machine:


using terms from application "Mail"

on perform mail action with messagestheMessagesfor ruletheRule


-- repeat first, then tell

repeat with theMessage in theMessages

tell application "Mail"

display dialog "inside"

set theId to id of theMessage

display dialog "the id is " & theId

end tell

end repeat

end perform mail action with messages

end using terms from


I don't see a rhyme or reason to it yet, but a workaround is better than nothing.

Jan 26, 2013 12:51 PM in response to wesleyh84

well, the other thing to try is to dereference the variables first. e.g.:


set theId to get (id of theMessage)


but it's possible that the headers simply haven't arrived by the time the script calls for them. I suppose you could set up a repeat/delay loop to wait for them, but it would be clunky. That would look like this:


repeat

try

set theId to get (id of theMessage)


-- if we get here, id has a valid value, so...

exit repeat

end try


-- otherwise delay/repeat

delay 0.2

end repeat

display dialog "the id is " & theId

Jan 26, 2013 12:56 PM in response to twtwtw

Tried both of these without luck, second one it gets stuck in an infinite loop. it seems no matter how much time I give it it does not get the ID of the email.


Anyway, been thinking of an alternate solution in which I temporarily move the email to a special mailbox, then process "all messagees" in that mailbox (regardless of wheter or not they're new) and then move them back to the original mailbox afterwards... Very hacky.. But I don't see another way?

Why doesn't this simple applescript work any more in ML mail.app (as a rule)

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