Moving Mail Message to Nested Mailbox

I have a script for moving selected mail messages to “On My Mac” mailboxes that worked fine in Tiger but is broken in Leopard. The script breaks when trying to move a message to a mailbox within another mailbox.

Any ideas?

Any code? :wink:

Sorry…

set excludedMailboxes to {"ToDos", "Outbox"}
tell application "Mail"
	set allMailboxes to name of mailboxes
end tell
set myMailboxes to {}
repeat with i in allMailboxes
	if i is not in excludedMailboxes then
		set end of myMailboxes to i as string
	end if
end repeat
display dialog "Choose:" buttons {"Cancel", "Copy To", "Move To"} giving up after 10 default button 3
if the button returned of the result is "Move To" then
	set moveToMailbox to (choose from list myMailboxes with prompt "Move to which mailbox?" without multiple selections allowed and empty selection allowed) as string
	tell application "Mail"
		activate
		set theMessages to selection
		if theMessages is not {} then
			move theMessages to mailbox moveToMailbox
		end if
	end tell
else
	set copyToMailbox to (choose from list myMailboxes with prompt "Copy to which mailbox?" without multiple selections allowed and empty selection allowed) as string
	tell application "Mail"
		activate
		set theMessages to selection
		if theMessages is not {} then
			duplicate theMessages to mailbox copyToMailbox
		end if
	end tell
end if

If you retrieve the name of the mailbox, you get only the last part of the “path” like a file/folder name in Finder.
To refer to a mailbox, you need the whole path


.
set theMessages to selection
		set moveToMailbox to (get 1st mailbox whose name is moveToMailbox)
		if theMessages is not {} then
.

It still doesn’t work. Moving files to nested mailboxes breaks the script.

of course you must omit “mailbox” in this line, because the variable is already the reference to the mailbox


 move theMessages to moveToMailbox

Moving to nested folders now works but using the same info (syntax modified) to copy (duplicate) to nested mailboxes is still broken.

For some reason the duplicate command works only, if the messages are duplicated one by one.
with your permission assumed I simplified the script a bit


set excludedMailboxes to {"ToDos", "Outbox"}
tell application "Mail"
	set allMailboxes to name of mailboxes
end tell
set myMailboxes to {}
repeat with i in allMailboxes
	if i is not in excludedMailboxes then
		set end of myMailboxes to i
	end if
end repeat
display dialog "Choose:" buttons {"Cancel", "Copy to", "Move to"} giving up after 10 default button 3
set B to button returned of the result
set ToMailbox to (choose from list myMailboxes with prompt (B & " which mailbox?") without empty selection allowed) as string
tell application "Mail"
	set ToMailbox to (get 1st mailbox whose name is ToMailbox)
	set theMessages to selection
	if theMessages is not {} then
		if B contains "Move" then
			move theMessages to ToMailbox
		else
			repeat with oneMessage in theMessages
				duplicate oneMessage to ToMailbox
			end repeat
		end if
	end if
end tell

Stefan,

That works great! Thank you.

One last part of my old script is broken. Can you add to the script that you wrote that if a message(s) is being moved/copied to a mailbox named “New Work” that the message(s) will be marked as unread.

Thank you so much.

Stefan. Why the double “tell”? Doesn’t this bit:

display dialog "Choose:" buttons {"Cancel", "Copy to", "Move to"} giving up after 10 default button 3
set B to button returned of the result
set ToMailbox to (choose from list myMailboxes with prompt (B & " which mailbox?") without empty selection allowed) as string

work inside of Mail?

Of course it works inside Mail, I changed only the crucial lines in the original script.

@Rob

To set the messages to read, the move branch has also to be iterated


set excludedMailboxes to {"ToDos", "Outbox"}
tell application "Mail"
	set allMailboxes to name of mailboxes
	
	set myMailboxes to {}
	repeat with i in allMailboxes
		if i is not in excludedMailboxes then
			set end of myMailboxes to i
		end if
	end repeat
	display dialog "Choose:" buttons {"Cancel", "Copy to", "Move to"} giving up after 10 default button 3
	set B to button returned of the result
	set ToMailbox to (choose from list myMailboxes with prompt (B & " which mailbox?") without empty selection allowed) as string
	set ToMailbox to (get 1st mailbox whose name is ToMailbox)
	repeat with oneMessage in (get selection)
		if ToMailbox is mailbox "New Work" then set read status of oneMessage to true
		if B contains "Move" then
			move oneMessage to ToMailbox
		else
			duplicate oneMessage to ToMailbox
		end if
	end repeat
end tell

Perfect. Thanks Stefan!