-- Apple Mail - List Mailboxes -- https://c-command.com/scripts/spamsieve/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 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