1

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

update: i added a try catch block around

set theId to id of theMessage

and this is the error I get:

Can't get class mssg 1 of class mbxp "Incoming POP messages" of class mact "Telenet". Invalid index. -1719

Any idea what this means? I don't get the error when applying rules manually.

Update 2: OK i found out that incoming messages don't have an ID yet. That's a problem since I want to save the email to disk:

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 & "';"

Is there any way around this?

  • What version of OS X and Mail are you using? – John Sauer Jan 26 '13 at 17:04
  • @JohnSauer 10.8.2 and mail 6.2 (1449) Thx –  Jan 26 '13 at 18:32
  • I have some Mail Rule AppleScripts from 2 years ago in which I noted that incoming messages didn't yet have an ID. When I saw your question today, I Googled for some related documentation or other users' experience, but I couldn't find anything. So I did some testing myself today, but I'm finding that I am able to obtain `id`s and `message id`s for incoming messages. Strange... Are you interested in seeing my AppleScript that is able to obtain `id`s and `message id`s? – John Sauer Jan 26 '13 at 19:24
  • @John Sauer - yes I'd like to see that! I found this thread detailing a similar (or same) bug saying the cause is 10.8 ML - https://discussions.apple.com/thread/4186119?start=15&tstart=0 - but I don't find the solution satisfactory. Perhaps I'll set up a cron instead that search the SQLite database every 10 minutes? I'd like to see what you have first. Are you on mountain lion? –  Jan 26 '13 at 19:55
  • And another post: http://bee-software.net/blog/mail-rules-applescript-not-working-after-upgrade-to-mountain-lion/ –  Jan 26 '13 at 19:56

1 Answers1

1

As promised in my comment, here's an AppleScript that logs Mail messages' subject, id, and message id to ~/Desktop/log.txt.

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        try
            repeat with theMessage in theMessages
                LogText("subject:    " & (theMessage's subject as string))
                LogText("id:         " & (theMessage's id as string))
                LogText("message id: " & (theMessage's message id as string))
                LogText("")
            end repeat
        on error errorText number errorNumber
            tell application "Mail" to display alert ("Error: " & errorNumber) message errorText
            return
        end try
    end perform mail action with messages
end using terms from


on LogText(theText)
    tell application "Finder"
        set logPath to (path to the desktop as text) & "log.txt"
        try
            set writeText to open for access file logPath with write permission
            set currentDateAsString to do shell script "date +\"%Y-%m-%d %T\""
            write (currentDateAsString & "    " & (theText as text) & return) to writeText starting at ((get eof writeText) + 1)
            close access writeText
        on error errorText number errorNumber
            close access writeText
            error errorText number errorNumber
        end try
    end tell
end LogText

I'm also running OS X 10.8.2 and Mail 6.2. As I said, I'm surprised to say this, but triggering the above AppleScript via mail rule works just as well for incoming messages as it does when selecting the menu "Apply Rules".

Community
  • 1
  • 1
John Sauer
  • 4,411
  • 1
  • 25
  • 33
  • thank you for this, unfortunately it does not work. The error I get is : "can't get class mssg 1 of class mbxp "Incoming POP Messages" of class mact "Telenet". Invalid index. Are you running POP or imap? I think I've read somewhere that this has to do with POP only. I don't have a mailbox named "Incoming POP Messages", so that's something internal. –  Jan 26 '13 at 21:01
  • Yup, the Mail account I tested with is IMAP. I wouldn't expect that that would make a difference, but your experience suggests that it does. Sorry! – John Sauer Jan 26 '13 at 21:47
  • 1
    Thanks though for your help. I'm now doing a workaround where I move the messages to a special mailbox first, then process all messages in that mailbox instead of just the new ones. That way the message always has an ID. After processing, I move it back to the inbox so that special mailbox is always empty. –  Jan 26 '13 at 21:55