Apple Mail - List Mailboxes

Summary: Opens a TextEdit window showing the internal AppleScript names of all your mailboxes.
Requires: SpamSieve, Apple Mail
Install Location: ~/Library/Scripts/Applications/Mail
Last Modified: 2019-10-02

Description

Some scripts such as Apple Mail - SaneBox, Apple Mail - Remote Training, and Apple Mail - Server Junk Mailbox require you to know Mail’s internal name for a mailbox, which may be different from the one that it displays in the user interface. This script logs all your account and mailbox names to a new window in TextEdit so that you can find the name that you need to enter in the script.

Installation Instructions · Download in Compiled Format · Download in Text Format

Script

on run
    
tell application "Mail"
        
set _string to my appendAccounts(accounts, 0, "")
        
set _string to my appendOnMyMac(_string)
    
end tell
    
my displayString(_string)
end run

on appendAccounts(_accounts, _level, _string)
    
tell application "Mail"
        
set _result to _string
        
repeat with _account in _accounts
            
set _result to my appendLine(_account's name, 0, _result)
            
set _result to my appendMailboxes(my topLevelMailboxesOfAccount(_account), 1, _result)
        
end repeat
        
return _result
    
end tell
end appendAccounts

on appendOnMyMac(_string)
    
set _string to my appendLine("On My Mac", 0, _string)
    
set _mailboxes to my onMyMacMailboxes()
    
set _string to my appendMailboxes(_mailboxes, 1, _string)
    
return _string
end appendOnMyMac

on onMyMacMailboxes()
    
tell application "Mail"
        
set _result to {}
        
repeat with _mailbox in mailboxes
            
set _container to _mailbox's container
            
try
                
get container of _container -- Child mailbox
            
on error
                
copy _mailbox to end of _result -- Top-level mailbox
            
end try
        
end repeat
        
return _result
    
end tell
end onMyMacMailboxes

on topLevelMailboxesOfAccount(_account)
    
tell application "Mail"
        
set _result to {}
        
set _mailboxes to _account's mailboxes
        
repeat with _mailbox in _mailboxes
            
set _container to _mailbox's container
            
if class of _container is class of _account then -- Comparing without "class" doesn't work
                
set _result to _result & {_mailbox}
            
end if
        
end repeat
        
return _result
    
end tell
end topLevelMailboxesOfAccount

on appendMailboxes(_mailboxes, _level, _string)
    
tell application "Mail"
        
set _result to _string
        
repeat with _mailbox in _mailboxes
            
set _result to my appendLine(_mailbox's name, _level, _result)
            
set _result to my appendMailboxes(_mailbox's mailboxes, _level + 1, _result)
        
end repeat
        
return _result
    
end tell
end appendMailboxes

on appendLine(_line, _level, _string)
    
return _string & my prefixForLevel(_level) & _line & return
end appendLine

on prefixForLevel(_level)
    
set _indent to " "
    
set _result to ""
    
repeat _level times
        
set _result to _result & _indent
    
end repeat
    
return _result
end prefixForLevel

on displayString(_string)
    
do shell script "echo " & _string's quoted form & " | open -ef"
end displayString