Zantafio Zantafio - 1 month ago 8
AppleScript Question

AppleScript: how to move a mailbox to another mailbox in Apple Mail

Naively thought that would suffice:

tell application "Mail"
move mailbox "complaints" of account "ACME LLC" to mailbox "customers" of account "ACME LLC"
end tell


What's the correct syntax?

Follow up question, how to delete a mailbox?

tell application "Mail"
delete mailbox "complaints" of account "ACME LLC"
end tell


gives me error "AppleEvent handler failed." number -10000"

Answer

Ideally, you want to change the folder in which your mailbox is. The mailbox in which you have mailbox is the property 'container'. For instance, container of 'Sub' gives you the mailbox in which mailbox "Sub" is.

Unfortunately, this property is read only in Applescript. So you can't change it ! The work around is to create a new mail box inside your existing mail box and then move the messages between mailboxes.

Your source mailbox is named "Source" and you want to move it inside existing mailbox named "Dest" :

1) create sub mailbox with name "Source" into mailbox "Dest".

tell application "Mail" to set NewBox to make new mailbox with properties {name:"Dest/Source", class:container, unread count:0}

As you can see, Mail event handling here is a bit strange, because the path should be in the name ! fyi, I never succeed to create boxe inside boxe inside boxe. (3 levels) !

Then use the script bellow to move from Source to new "Source"

set BoxeSource to "Source"
set BoxeDest to "Dest/Source"
tell application "Mail" to set mailbox of every message of mailbox BoxeSource to mailbox BoxeDest