AppleScript (or Other) to Simply View a Mailbox in Apple Mail

I thought this would be simple, then POOF, an entire afternoon down the tubes. I’m looking for an elegant way to have AppleScript open email (got that part nailed down) and then display the contents of a mailbox. I am able to use Keyboard Maestro and my Streamdeck quickly open mailboxes that are identified as favorites, but I’m hoping to be able to open any mailbox and/or smart mailbox. Not looking for someone to write the code, but any pointers would be appreciated.

The proper AppleScript syntax is, as usual, obtuse.

You have to tell it what account to use even if you only have one account. The account name appears to be whatever name you have tin the “Description” for the account, as shown here:

I could not figure out how to do it without first setting a variable (you’ll see theMailbox below)

That being said, this (below) works for me, at least for “real” mailboxes. I did not test “Smart” mailboxes. There were some old posts which said they were not accessible by AppleScript, but that might not be true anymore.

tell application "Mail"

	set theMailbox to mailbox "Test" of account "iCloud"
	set selected mailboxes of front message viewer to theMailbox
end tell

I hope this helps!

Thanks, TJ:

So close! It was the word “front” that I was missing. :exploding_head: I wasn’t thinking about that needing an index reference.

I think you’re right about smart mailboxes, though I’ve seen posts about getting to them through system events and keystrokes. That’s for another day.

Much appreciated!


1 Like