9 Replies Latest reply: May 17, 2013 7:48 AM by adayzdone
wesleyh84 Level 1 Level 1 (10 points)

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
  • 1. Re: Why doesn't this simple applescript work any more in ML mail.app (as a rule)
    Pierre L. Level 4 Level 4 (3,860 points)

    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 messages theMessages for rule theRule

            tell application "Mail"

                repeat with theMessage in theMessages

                    display dialog "inside"

                    set theTest to (exists id of theMessage)

                    display dialog theTest

                end repeat

            end tell

        end perform mail action with messages

    end using terms from

  • 2. Re: Why doesn't this simple applescript work any more in ML mail.app (as a rule)
    wesleyh84 Level 1 Level 1 (10 points)

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

  • 3. Re: Why doesn't this simple applescript work any more in ML mail.app (as a rule)
    wesleyh84 Level 1 Level 1 (10 points)

    Hmm, it doesn't seem to have anything to do with the ID, it happens with other fields also (like subject). Is there anything wrong with my on perform?      

     

                                               set theSubject to subject of theMessage

  • 4. Re: Why doesn't this simple applescript work any more in ML mail.app (as a rule)
    twtwtw Level 5 Level 5 (4,690 points)

    well, I noticed that if another display dialog is added just before the repeat loop, things seem to work correctly.  I'm still working on the 'why' of it though.

  • 5. Re: Why doesn't this simple applescript work any more in ML mail.app (as a rule)
    twtwtw Level 5 Level 5 (4,690 points)

    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 messages theMessages for rule theRule

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

  • 6. Re: Why doesn't this simple applescript work any more in ML mail.app (as a rule)
    wesleyh84 Level 1 Level 1 (10 points)

    That's weird, this workaround doesn't work for me. I still get the same error message.

  • 7. Re: Why doesn't this simple applescript work any more in ML mail.app (as a rule)
    twtwtw Level 5 Level 5 (4,690 points)

    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


  • 8. Re: Why doesn't this simple applescript work any more in ML mail.app (as a rule)
    wesleyh84 Level 1 Level 1 (10 points)

    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?