Find all parent mailboxes in macOS Mail
I am an information packrat; I never throw out emails, and have messages dating back to the last century. I manage this using hierarchical mailboxes to keep the archives from cluttering up my Mail window.
I have Rules set up to send various messages directly to the mailbox where they should be stored, and then Smart Mailboxes to display unread messages of various classifications: Unread Mail, Unread Mass Mails, Unread Spammy, and Unread Junk, each less critical than the previous.
In the past, it’s been easy enough to see where any message is stored. The macOS Mail app has acted like any other application on the Mac: documents (i.e., messages) could display the full path to the document by option-clicking the document’s title (i.e., the message’s subject) in the window’s title bar.
As far as I can tell, this is no longer possible. Mail has been moving away from a document-based model toward a flat, search-based model, and no longer makes it easy—or even possible—to find the full path to a viewed message. The immediate parent mailbox’s name is displayed in the sidebar, but the mailbox’s parent is nowhere to be found. So that I can see easily enough that the order I’m waiting on is in the eBay mailbox. But not that the eBay mailbox is in the Purchases mailbox of the Finances mailbox. The latter two are completely hidden.
Obviously, however, the Mail app itself knows where these messages are, and that information can be displayed using AppleScript.
[toggle code]
-
tell application "Mail"
- set mailboxPaths to ""
-
repeat with currentMessage in (get selection)
- set mailboxPath to ""
- set messageMailbox to the mailbox of currentMessage
-
repeat while (exists name of messageMailbox)
- set mailboxPath to the name of messageMailbox & ":" & mailboxPath
- set messageMailbox to the container of messageMailbox
- end repeat
- set mailboxPaths to mailboxPaths & mailboxPath & return
- end repeat
- end tell
- display dialog mailboxPaths buttons ("OK")
If you run this script in Script Editor while one or more messages are selected, it will pop up a window to display the full path to each selected message’s parent mailbox.
There’s a minor trick involved in knowing where to stop traveling up the hierarchy of parent mailboxes. At the top level, the “container” of the mailbox will not be a mailbox but, rather, an account. Accounts don’t have containers. If an item doesn’t have a container, attempting to access its container is an error. This is true even when using the “exists” terminology. It’s a programming catch-22: if the container doesn’t exist, asking if it exists is an error.
So, instead, my script asks if the item’s name exists. The top-level container has an empty name, so asking if it exists isn’t an error.
Since the container of a message is its mailbox, we can also display that mailbox in a new viewer window.
[toggle code]
-
tell application "Mail"
-
repeat with currentMessage in (get selection)
-
tell (make new message viewer)
- set selected mailboxes to the mailbox of currentMessage
- end tell
-
tell (make new message viewer)
- end repeat
-
repeat with currentMessage in (get selection)
- end tell
This goes through each selected message, creates a new viewer window, and opens the mailbox of the current message in that viewer.
The easiest way to use these scripts is to make sure you have “Script Menu” enabled in Script Editor’s preferences. Once you have the Script Menu enabled, it will appear in the menu bar in the upper right; from there, you can Open Mail Scripts Folder and put your scripts into the Mail Scripts Folder. Once they’re in Mail’s scripts folder, they will appear under the script menu when you’re using Mail.
More AppleScript
- JXA and AppleScript compared via HyperCard
- How does JXA compare to the AppleScript solution for screenshotting every card in HyperCardPreview?
- Using version control with AppleScripts
- AppleScripts aren’t stored as text, which makes it impossible to track changes in AppleScript files using version control software such as Mercurial or Git.
- Save clipboard text to the current folder
- Use the Finder toolbar to save text on the current clipboard directly to a file in the folder that Finder window is displaying.
- Adding parenthetical asides to photograph titles on macOS
- Use Applescript to append a parenthetical to the titles of all selected photographs in Photos on macOS.
- AppleScript, variables, and dropped filenames in Automator
- Automator is a simple workflow system for Mac OS X. By its nature it is very procedural: one task follows another; workflows don’t loop and they don’t store variables for later. However, this is possible in Automator and while it adds complexities it can also solve problems such as wanting to save dropped filenames for later use.
- 17 more pages with the topic AppleScript, and other related pages