User Chris has over 1000 labels/mailboxes in Gmail. After archiving he wants to delete the empty labels. It's not something that I can do with Mail Archiver (yet). But I can easily find empty mailboxes with AppleScript for Mail and Outlook.

A while ago I made an AppleScript to get a list of mailboxes for AppleMail. I'm going to use the script as starting point:

tell application id "com.apple.mail"
   
set theMailboxes to get mailboxes
   
set theAccounts to get accounts
   
repeat with theAccount in theAccounts
       
set AccountMailboxes to mailboxes of theAccount
       
set end of theMailboxes to AccountMailboxes
   
end repeat
   
return theMailboxes
end tell

The script first gets the local mailboxes. Then it gets the accounts. The script loops over the accounts, gets the mailboxes for each account and adds everything together. Here is the result:Result of script for getting mailboxes

Theoretically, it's quite simple to get the number of emails for a mailbox with "number of messages". But using the mailbox directly gives only the name of the mailbox and not the mailbox path. I need to get the parent of the mailbox until the parent is an account. Then we get the full mailbox path.

For a lot of emails the following script will be very slow (really!!!):

tell application id "com.apple.mail"
   
set theMailboxes to get mailboxes
   
set theAccounts to get accounts
   
repeat with theAccount in theAccounts
       
set AccountMailboxes to mailboxes of theAccount
       
set theMailboxes to theMailboxes & AccountMailboxes
   
end repeat

   
set theMailboxCount to ""
   
repeat with theMailbox in theMailboxes
       
set MailboxPath to ""
       
set MailboxPath to my getMailboxPath(theMailbox)
       
set theCount to number of messages of theMailbox
       
set theMailboxCount to theMailboxCount & MailboxPath & ", " & theCount & return
   
end repeat
   
return theMailboxCount
end tell

on getMailboxPath(theMailbox)
   
tell application id "com.apple.mail"
       
set ContainerName to name of theMailbox
       
set MailboxPath to name of theMailbox
       
set theContainer to theMailbox
       
repeat while ContainerName is not ""
           
try
               
set theContainer to container of theContainer
               
set ContainerName to name of theContainer
               
set MailboxPath to ContainerName & ":" & MailboxPath
           
on error
               
return MailboxPath
           
end try
       
end repeat
   
end tell
end getMailboxPath

Now I'm getting somewhere:

The result is now in ScriptEditor. Like the last scripts I want to see the result in Numbers/Excel. so that I can filter for the empty mailboxes. Again I add the methods to write the result to a file on the desktop. Because mailboxes can contain commas I also need to take care of quoting the commas. The resulting script is now quite a bit longer:

tell application id "com.apple.mail"
   
set theMailboxes to get mailboxes
   
set theAccounts to get accounts
   
repeat with theAccount in theAccounts
       
set AccountMailboxes to mailboxes of theAccount
       
set theMailboxes to theMailboxes & AccountMailboxes
   
end repeat

   
set theMailboxCount to ""
   
repeat with theMailbox in theMailboxes
       
set MailboxPath to ""
       
set MailboxPath to my getMailboxPath(theMailbox)
       
set theCount to number of messages of theMailbox
       
set theMailboxCount to theMailboxCount & my quote_for_csv(MailboxPath) & ", " & theCount & return
   
end repeat

   
--use file on desktop
   
set this_file to (((path to desktop folderas string) & "apple script data.csv")
   
--write data to file
   
my write_to_file(theMailboxCountthis_filetrue)

end tell

on getMailboxPath(theMailbox)
   
tell application id "com.apple.mail"
       
set ContainerName to name of theMailbox
       
set MailboxPath to name of theMailbox
       
set theContainer to theMailbox
       
repeat while ContainerName is not ""
           
try
               
set theContainer to container of theContainer
               
set ContainerName to name of theContainer
               
set MailboxPath to ContainerName & ":" & MailboxPath
           
on error
               
return MailboxPath
           
end try
       
end repeat
   
end tell
end getMailboxPath

--routine for writing data to file
on write_to_file(this_datatarget_fileappend_data)
   
try
       
set the target_file to the target_file as string
       
set the open_target_file to open for access file target_file with write permission
       
if append_data is false then set eof of the open_target_file to 0
       
write this_data to the open_target_file as «class utf8» starting at eof
       
close access the open_target_file
       
return true
   
on error
       
try
           
close access file target_file
       
end try
       
return false
   
end try
end write_to_file

--replace " with "" and add " at beginning and end so that a comma doesn't make a new field
on quote_for_csv(the_text)
   
set the_text to replace_chars(the_text, "\"", "\"\"")
   
return "\"" & the_text & "\""
end quote_for_csv

--replace characters in text
on replace_chars(this_textsearch_stringreplacement_string)
   
set AppleScript's text item delimiters to the search_string
   
set the item_list to every text item of this_text
   
set AppleScript's text item delimiters to the replacement_string
   
set this_text to the item_list as string
   
set AppleScript's text item delimiters to ""
   
return this_text
end replace_chars

Here is the final result in Numbers with my inventively named test mailboxes:

I have some test accounts with lots of mailboxes. But I need to simplify the mailboxes a bit.

Of course, I can do something similar for Outlook.