4

I have written the script below to move messages from one mailbox to another in Mail.

But when I run it, just a few email messages are moved at a time. E.g. if I have 10 messages in the mailbox, only three or four are moved. So I have to run the script multiple times to complete the operation! As far as I can tell it's pretty random how many move each time.

So does anyone know why this might be, and how I could improve this script?

Many thanks!

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Mail"

    set theWorkInbox to mailbox "Work Inbox" of account "Work"
    repeat with currentWorkMessage in every message of (mailbox "Work Holding Bay" of account "Work")
        set mailbox of currentWorkMessage to theWorkInbox
    end repeat

end tell

I'm using macOS Sierra

2 Answers 2

5

It's better if you capture a reference to every message in a variable, instead of getting the reference every time you loop.

tell application "Mail"
    set theWorkInbox to mailbox "Work Inbox" of account "Work"
    set TargetInbox to mailbox "Work Holding Bay" of account "Work"

    -- CAPTURE REFERENCE TO EVERY MESSAGE OF MAILBOX IN A VARIABLE
    set EveryMessage to every message of TargetInbox

    repeat with currentWorkMessage in EveryMessage
        set mailbox of currentWorkMessage to theWorkInbox
    end repeat

end tell
2
  • Thanks I have updated to your version, and it now seems to work perfectly.
    – Andy
    Jun 20, 2017 at 15:17
  • You're welcome! I'm glad it worked. Jun 26, 2017 at 23:20
1

Thanks Andy for this question and unlocked2412 for the great answer! I had the same trouble as Andy with reference on every loop, but after using unlocked2412's script I was still stuck with my moved messages reappearing in its original locations in GMail.

So I hope I here can add a working solution for this special case of GMail* in the MacOS Mail.app, where neither set mailbox of OR move currentWorkMessage worked, but a combined copy and delete did the trick for permanently moving:

Ceteris Paribus:

...
repeat with currentWorkMessage in EveryMessage
    duplicate currentWorkMessage to theWorkInbox
    delete currentWorkMessage
end repeat
...

*Seems like GMail handles IMAP folders a bit differently than other systems: Making Apple Mail, Gmail, IMAP and AppleScript work together

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .