How to delete a mailbox hierachy?

Here is an example script showing the problem I have with mail and AppleScript

tell application "Mail"
  set mboxName to "aaa/bbb/ccc"
  make new mailbox with properties {name:mboxName}
  set mb to mailbox named mboxName
  repeat while (count of mailboxes of mb) < 1
    set p to container of mb
    delete mb
    set mb to p
  end repeat
end tell

This script first creates a mailbox Hierachs, i.e. A mailbox “aaa” containing a mailbox “bbb” containing “ccc”.

Now assume I want to delete the hierachy. So I thought, I remember “ccc”'s container, delete “ccc” and proceed as long as the current mailbox does not contain other mailboxes.

Unfortunately this fails with an error. Just “ccc” gets deleted. The others remain.

Does someone have any idea where my mistake is?

I’m surprised the first deletion worked. Delete normally refers to a message, not a mailbox. To delete aaa or bbb, I had to right-click on it and delete it in the contextual menu getting an “Are you sure” message. It might continue to work, however, if you put the delete statement in an “ignoring application responses” block - I didn’t try that; I don’t normally use Mail (I’m a long-time Eudora user).

Hi,

I don’t think this is going to be much use, but FYI.

The error I get in the event log when trying to delete the next level up is NSKeySpecifierEvaluationScriptError. According to this page

This problem may have something to do with this, as after testing I can delete blue mailboxes in this way, but not white ones.

Best wishes

John M

Thanks for the info! Yes! It won’t help me when trying to delete those folders, but now at least know why they can’t be deleted.

I think you ave to create and then delete each mailbox individually allot like i do in this script only I’m just creating them


tell application "Mail"
	-- givmail 2 seconds to load in all the new mail
	delay 2
	set theMessage to every message in inbox as list
	repeat with amessage in theMessage
		if amessage is not "" then
			
			set SenderName to extract name from sender of amessage
			set theDomain to extract address from sender of amessage
			set tid to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "@"
			set theDomain to text item 2 of theDomain
			set AppleScript's text item delimiters to "."
			set theDomain to text item 1 of theDomain
			set AppleScript's text item delimiters to tid
						
			
			if not (exists mailbox theDomain) then
				make new mailbox with properties {name:(theDomain)}
			end if
			
			if not (exists mailbox SenderName of mailbox theDomain) then
				
				make new mailbox with properties {name:(theDomain & "/" & SenderName)}
			end if
			move amessage to mailbox SenderName of mailbox theDomain
		end if
		
	end repeat
end tell

Thanks for your reply, but I can’t. What I posted was just an example. In the real script the “aaa/bbb/” part is “imported/”, meaning: I want to delete my Folders which are left over from importing mailboxes.

Hi Skeeve,

I have answered this in your thread on the same subject at fischer-bayern.de - here the solution I posted there:

tell application "Mail"
	set mboxName to "aaa/bbb/ccc"
	make new mailbox with properties {name:mboxName}
end tell

tell application "Mail"
	set mb to mailbox named mboxName
	try
		repeat while (count of mailboxes of mb) < 1
			set theContainer to container of mb
			set theContainerPath to my containerPath(mb) & "/" & name of mb
			try
				delete mb
			on error
				do shell script "rmdir ~/Library/Mail/Mailboxes/" & theContainerPath
				make new mailbox with properties {name:theContainerPath}
				
				delete mb
			end try
			set mb to theContainer
		end repeat
	end try
end tell


on containerPath(mb)
	tell application "Mail"
		set retVal to ""
		repeat
			try
				set retVal to (name of container of mb) & "/" & retVal
				set mb to container of mb
			on error
				try
					return text 1 thru -2 of retVal
				on error
					return ""
				end try
			end try
		end repeat
	end tell
end containerPath

D.