Neil Ticktin
Neil Ticktin

Reputation: 11

Mail.app AppleScript: Duplicating (copying) all messages to another account

I'm trying to create an AppleScript for Yosemite Mail.app that makes copies (not archives) of messages.

Let's say I have 3 accounts:

I want to select all the messages in the Master inbox -- and copy (aka duplicate) those messages to the inboxes of two other accounts, Target 1 and Target 2. In the end, there will be three inboxes, all with the same set of messages -- again copies (not archives).

I've tried things like:

set mailboxMaster to "Master"
set mailboxTargets to {"Target 1", "Target 2"}

repeat with curMailboxTarget in mailboxTargets
tell application "Mail"
    duplicate every message in mailbox "Master" to mailbox curMailboxTarget
end tell
end repeat

but I get "Mail got an error: Can’t set mailbox"

Ideas?

Upvotes: 1

Views: 876

Answers (1)

jweaks
jweaks

Reputation: 3792

Something like this will work, Neil. wink wink

It will copy messages from the master account/mailbox, and into each account/mailbox pair in the target lists.

property masterAccount : "MySourceAccountName"
property mailboxMaster : "INBOX"

property targetAccounts : {"IMAPAccountName", "ExchangeName"}
property mailboxTargets : {"INBOX", "Inbox"}

-- set the source mailbox and account
tell application "Mail"
    set sourceBox to mailbox mailboxMaster of account masterAccount
    set theCount to count of messages of sourceBox
    set theCount to 3 -- to run a test with a limited number

end tell
if theCount < 0 then error "No messages in the source mailbox."

-- set progress indicator
set progress total steps to theCount

-- iterate for each account name in targetAccounts
repeat with a from 1 to (count targetAccounts)
    set acctName to item a of targetAccounts
    set boxName to item a of mailboxTargets

    -- set destination mailbox for this target account
    tell application "Mail" to set destinationBox to mailbox boxName of account acctName

    -- process each message
    repeat with n from 1 to theCount
        -- iterate the progress indicator
        set progress description to "Copying Message " & n & " of " & theCount

        -- duplicate the message
        tell application "Mail" to duplicate (message n of sourceBox) to destinationBox

        -- terminate the progress indicator
        set progress completed steps to n
    end repeat
end repeat

Upvotes: 1

Related Questions