Mailbox lost in attempt to move it into another mailbox

I was trying to write a script that would move an entire mailbox (not the messages but the mailbox itself) inside another mailbox.

tell application "Mail"
	set mb to mailbox "Archived/Clients/2004/John Doe"
	move mb to mailbox "Archived/Clients"
end tell

The script fails at the ‘move’ command, with the following error message:

“Mail got an error: AppleEvent handler failed.”
[OxO,24f24f “Mail”]

However, the mailbox “Archived/Clients/2004/John Doe” just disappeared, along with all its contents!

Where did the mailbox go? Is there a way to recuperate it and the messages archived in it?
More generally, what is the right way to script what I wanted to do in the first place?

Many thank!

It’s good practice to keep a backup before running a script which was not tested before.
To be honest, I don’t apply this rule everytime but, at least, I know that I’m playing with matches and I’m not surprised if I burn some fingers.

Yvan KOENIG (VALLAURIS, France) samedi 14 juin 2014 15:35:13


I just reproduced this with a couple of test mailboxes, and as far as I can tell the source mailbox is DESTROYED. That’s a terrible bug that needs to be reported.

This works with a couple of caveats:

tell application "Mail"
	set srcMbx to mailbox "TestMove-Src"
	set srcMbxName to name of srcMbx
	set destMbx to mailbox "TestMove-Dest"
	set destMbxName to name of destMbx
	set newMailbox to destMbxName & "/" & srcMbxName & "-Temp"
	make new mailbox with properties {name:newMailbox}
	set msgList to messages of srcMbx
	repeat with i in msgList
		move i to mailbox newMailbox
	end repeat
	delete srcMbx
	set name of mailbox newMailbox to srcMbxName
end tell

I’ve only tried this with On-My-Mac mailboxes.

You need to recreate the full path of the new mailbox, because Mail is not returning it.

If I were dealing with mail that MUST not be lost I’d probably copy it instead of move it, and then I’d have a verification routine make certain every email was copied before deleting the original mailbox.

You can move messages thus:

move every message of srcMbx to destMbx

But I would be very wary of doing this with a large number of them. I’d run a lot of tests with test mail before trusting the routine to work.