Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, Swift, BASIC, and whatever else I happen to feel like hacking at.

Find all parent mailboxes in macOS Mail

Jerry Stratton, September 6, 2023

AppleScript Editor: Enable Script Menu: Enable the Script Menu in AppleScript Editor’s preferences.; AppleScript

Make sure Script Menu is enabled in Script Editor.

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")
Mail Scripts Folder: Open Mail’s Scripts Folder to place scripts inside for use in macOS’s Mail app.; AppleScript; macOS Mail

Use the script menu to reveal Mail’s Scripts Folder.

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
    • end repeat
  • 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.

  1. <- HyperCard Viewer