Using AppleScript to Move Mail Messages to Trash

2019-06-10 06:43发布

I've written a script that is invoked by a Mail rule. I want the script to move messages to the trash under certain conditions. It didn't work so I've commented out the test and it still didn't work. I got the code from an online tutorial. The tutorial moved the message to "Filed". The rule itself worked when I replaced the call to the script with a move to the trash. Here's what I'm testing:

using terms from application "Mail"
on perform mail action with messages these_messages for rule this_rule
    tell application "Mail"
        set the message_count to the count of these_messages
        repeat with i from 1 to the message_count
            set this_message to item i of these_messages

            --              try
            --                  set this_content to (every character of content of this_message) as Unicode text
            --                  if this_content is in {"", "?"} then error
            -- if "bowles" is not in this_content and "patton" is not in this_content then
            set mailbox of this_message to mailbox "Trash"
            -- end if
            --              on error error_message
            --                  set this_content to "NO CONTENT"
            --              end try
        end repeat
    end tell
end perform mail action with messages

end using terms from

The statement at issue is: set mailbox of this_message to mailbox "Trash"

The rest of the active code was from AppleScript Editor template.

Thanks, Curt

P.S. Are there any books out there that deal with using AppleScript with Mail?

2条回答
老娘就宠你
2楼-- · 2019-06-10 06:56

Instead of trying to "move" a message to the Trash, you should just say delete this_message.

Mail.app will then proceed according to your account settings (Mail preferences > Accounts > (your account) > Mailbox Behaviors > Trash) and move the message to its account's Trash mailbox or else delete it immediately, depending on your settings.

查看更多
beautiful°
3楼-- · 2019-06-10 07:09

I should have done a more extensive Web search before asking here. Hopefully, this answer will be useful to others. I added the first line below and modified the second one from the code above.

        set theAccount to account of mailbox of this_message
        set mailbox of this_message to mailbox "Trash" of theAccount
查看更多
登录 后发表回答