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"

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

I hope this helps!

1 Like

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!

Bill

1 Like

This is a life saver @Tjluoma. I was just making a pot about this when this appeared in my search. Would you be kind to share an apple script that moves an email to another folder? Listened to the streamdeck episode on macpoweruses and want to be able to move stuff around easily.
Thank you in advance:)

J

2 Likes

I don’t know the answer to that one but @RosemaryOrchard or @MacSparky might if no one else chimes in.

AppleScript is a language that I only know via Google+Copy/Paste.

(Despite using 3 different mail apps, I’m not currently using Mail.app on the Mac.)

1 Like

“Would you be kind to share an apple script that moves an email to another folder?”

maybe you mean “move an email to another mailbox?”

I’m using AppleScript to make this Alfred Workflow work for me.

It works by listing all folders from the selected message account, then it uses the Alfred interface to allow me to search and select the desired folder, and then will move the message to the desired folder. Maybe it can help you as well.

1 Like