Search code examples
macosapplescriptmacos-mail-app

How to open the folder/mailbox where is located a message I selected in a search result in the Mail App on macOS?


In the Mail app of macOS, I work with lots of smart folders (a particular mailbox that shows the results of pre-defined search criteria). Thus, these smart mailboxes show messages from different accounts and folders.

Usually, I need to jump to the actual mailbox/folder where is located a message selected in the list of results. I also have many folder.

One of the improvements (annoyances) in the new Mail app was that I could not find a way to do that. In past versions of macOS (at least up to Mavericks) this was easy. I could do the same as I still do in many other applications. See the image.

enter image description here

The previous trick is not working anymore in the message windows of the Mail App.

Is there any way to jump or open the mailbox/folder where is located a message I select in a search result or smart mailbox?


Solution

  • Solution with Automator + AppleScript

    The solution I found was to create an Automator Service and optionally associate it with a shortcut.

    1. Open Automator.
    2. New Document.
    3. Select Service for the type of document.
    4. At the top of the window, set the input type for this service:
      Service receives selected choose no input
      in choose Mail.app
    5. In the Actions library (left pane) find the action Run AppleScript.
    6. Drag and drop it in the workflow area.
    7. Copy the code at the end of this answer and paste it into the action Run AppleScript.
    8. Save your service (e.g. "Jump to Folder").

    Test the service

    When testing the service, Automator could remain open and also the Mail App.

    1. Open Mail App.
    2. Do a search and select one message, preferably a message located in a custom folder.
    3. In the menu bar go to Mail > Services. You should see your new service.
    4. Select the service.

    The selected and active mailbox/folder should be the mailbox of the previously selected message.

    Optional. Assign a shortcut to your service:

    1. Open System Preferences.
    2. Go to Keyboard > Shortcuts
    3. In the left pane select Services
    4. At the end of the right pane under General you should find your service
    5. Assign a shortcut to it (e.g. CMD-OPTION-J)

    The Code

    set theDialogTitle to "Go to Folder Script"
    
    tell application "Mail"
    
        -- Get the selected messages and the count of them
        set theMessageList to selected messages of message viewer 1
        set theCount to length of theMessageList
    
        -- Error if no messages
        if theCount is 0 then
            display dialog ¬
                "No message selected." with title theDialogTitle buttons {"OK"} with icon caution
            return
        end if
    
        -- Error if more than one message
        if theCount is greater than 1 then
            display dialog ¬
                "Must select only one message." with title theDialogTitle buttons {"OK"} with icon caution
            return
        end if
    
        -- Get the message
        set theMessage to item 1 of theMessageList
    
        -- Get the mailbox object
        set theMailbox to mailbox of theMessage
    
        -- Select the mailbox
        set selected mailboxes of message viewer 1 to theMailbox
    
    end tell