Apple Mail - Send Selected Email(s) to a Particular Mailbox

Hi

I'd like to send emails to a particular folder. I have several gmail addresses and use them via apple mail. All of them have a particular folder called @toreview. I'd like to press a button, say, command-e, while I'm in the mail app and the email will be moved from the inbox to the @toreview folder.

Thanks.

Use a Hot Key trigger (generally I’d recommend you use Control rather the Command to avoid conflicting with menu shortcuts (which a hot key will override, but its often not ideal)).

Then use a Select Menu Item action to select from the Message ➤ Move to menu (use the popup menu on the right side of the action to select the menu.

If I’m reading your message correctly, you have something like three Gmail addresses. Each Gmail address has a toreview folder. If that’s correct, and messages may be filed to any of

Account1 > toreview
Account2 > toreview
Account3 > toreview

then you will probably want to create three macros, one for each toreview folder. If that’s the case, you’d just follow Peter’s (@peternlewis ) instructions but create three macros, each with its own Hot Key trigger.

(I do my filing using AppleScript: A prompt is presented, I select the destination folder for the message, the message is then moved as specified. I can provide sample code if you’d like, but it sounds like Peter’s approach will be sufficient for your needs.)

This could easily be done in AppleScript.
This is NOT the exact solution, but should be a good starting point:

tell application "Mail"
  set theMessage to selection --- Get the selected messages
  set accountName to name of account of mailbox of theMessage --- To find an archive mailbox.
  --- This gets a mailbox that contains "Archive".  Uses "last" so you could have
  --- "Archive 2014-02" and "Archive 2014-03" and it'd use "Archive 2014-03".
  set theArchiveMailbox to last item of (mailboxes of account accountName whose name contains "Archive")
  set theMessageId to message id of theMessage --- So we can find it again once it's moved.
  move theMessage to theArchiveMailbox --- Moves the message to the archive
end tell

Would you please provide that sample AS?

You want to move what to where?

Currently using ⌘1-9 with a palette to move to the major buckets.

Have since gone to sub-mailboxes that I can not extend filing to with Apple's limited options.

Once moved to major mailbox (MCA in this example) with current system, would then like to move to some sub-mailboxes (MCA Budget &...; MCA Fencing, MCA Insurance, etc.)

Would also be nice to extend the list of Major mailboxes beyond Apple's 1-9 Favorites scheme.

Instead of using Mailbox>Move to Favorite Mailbox why not use Message>Move to> ? That will allow you to move the messages to any Mailbox you choose.

1 Like

Because I needed you to remind me of that option!

Not my thread to mark as solutioned and you solutioned it for me!

Screen Shot 2022-03-20 at 4.43.50 PM

In post #8

Thank you!

1 Like

This is very useful, but there's a problem.

The menu mutates if/when you change what mailbox folders are revealed in Mail.app, therefore you cannot reliably assign keyboard shortcuts to items using KM's Select or Show a Menu Item action.

It may or may not be possible to assign keyboard shortcut using the macOS Keyboard System Prefs by using only the mailbox name and not the menu path – but I have not tested it and don't know if it can stand up to the menu mutation.

The task can be done via AppleScript:

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/03/21 21:05
# dMod: 2022/03/21 21:05 
# Appl: Mail
# Task: Move Selected Messages to a Designated Mailbox
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Mail, @Move, @Selected, @Messages, @Designated, @Mailbox
--------------------------------------------------------

tell application "Mail"
   
   set destinationMailbox to mailbox "Email Lists/TEST!" of application "Mail"
   
   set selectedMessageList to selection
   
   repeat with theMessage in selectedMessageList
      move theMessage to destinationMailbox
   end repeat
   
end tell

--------------------------------------------------------

Here's how to get a usable reference to a selected mailbox in Mail:

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/03/21 21:08
# dMod: 2022/03/21 21:08 
# Appl: Mail
# Task: Get a reference to the Selected Mailbox.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Mail, @Get, @Reference, @Selected, @Mailbox
--------------------------------------------------------
(*

NOTES:

   • Smart Mailboxes are NOT supported.
   • Select only 1 mailbox in the mailboxes panel of a mail-viewer window.
   • Use the Apple 'Script Editor.app' or Script Debugger to acquire the reference.
      - Script Debugger & SD (Lite/Free) ⇢ https://latenightsw.com
         - The lite/free version is part and parcel of the main app.
            - The mail app reverts to lite after a set demo period.
               - SD and & SD Lite eat the Apple Script Editor for lunch.

*)
--------------------------------------------------------

tell application "Mail"
   tell (some message viewer whose index is 1)
      set theMailbox to item 1 of (get selected mailboxes)
   end tell
end tell

--------------------------------------------------------

Since you can get the content of a message with Mail it's possible to write a script that can automatically file a message to different mailboxes based on a wide variety of criteria.

I have used Mail Act-On from SmallCubed Software in combination with MailHub for this sort of job.

Unfortunately Mail Act-On is no longer offered as a standalone product, and the while MailSuite is pretty expensive at $80.00 initial cost and $45.00/year for updates. It's not a subscription though. Once you buy the software you own it up to that point. I assume you can stop and restart paying for upgrades at any point, but I would verify with the devs to be certain.

Also unfortunately the developer of MailHub have been AWOL for a couple of years now, and I think it's likely he died or is otherwise incapacitated.

When I upgrade to Apple Silicon I'll lose the use of MailHub (to my great sorrow), but I'll be able to get by with Mail Act-On and AppleScript + Keyboard Maestro.

-Chris

Hi @ccstone - Yes you're right and I had actually forgotten that. But interestingly the reason I'd forgotten is that I've been using Keyboard Maestro to file emails into Folders reliably for the last couple of years.

EDIT - Just tried your AppleScript approach and it works brilliantly so, I will now use that. For one of my actual Mailboxes, the Action looks like this:

Click to Show Image of AppleScript

And I found I could run your second AppleScript to get the Mailbox Reference from within a Keyboard Maestro AppleScript Action set to "display results in a window". The displayed result was the exact reference to the Mailbox.

image

I'm still using Mojave, so I can't test this.

On Mojave and previous versions of Mojave Mail.app mutates the Move-To menu based upon whether or not a mailbox with nested items is open or closed.

When the mailbox is closed in the sidebar this one works:

image

When the mailbox is open in the sidebar this one works:

image

That may no longer be the case with Big Sur ⇢ Monterey though.

Apple has been playing some games with menu structures in some apps...

-Chris

1 Like

You're absolutely right. And it still does that in 12.3 as I've just tested.

So, the only reason I was getting consistent results is that I wasn't opening and closing the folders in the Mail sidebar...

I will switch to your AppleScript... and I'll edit my post down a bit in case it leads others down the same path... :grinning:

1 Like

It's sad and pathetic that Apple hasn't added a pop-up filter-type window for filing/moving or copying messages to a desired location.

Other mail clients have had that feature for years if not decades.

Other conspicuous omissions are a horizontal line separator and tags.

Mail needs some serious TLC – not just a shallow splash of fresh paint...

1 Like

Here's @Zabobon's suggestion in action:

Good enough for manually sorting into folders/sub-folder.

Only a problem for a keyboard-focused approach. My fluid folder list would be difficult to keep up with if I were to map to keyboard shortcuts plus manually setting up each mailbox.

I love your suggestion of some automatic sorting that Act-on/MailHub provides however, even if that was available, I found even using Hazel to file mail into Finder folders after dragging them to the desktop where Hazel could act on them was more of a job than I kept up with.

Just one, more visual than keyboard approach. I suspect many if not most will want the keyboard-focused approach you are offering.

As always, thank you for sharing a script and demonstrating what's possible!

2 Likes

The smoothest way to do this would be to use a macro to open the menu and then type-select.

Menu ⇢ Message ⇢ Move-To.kmmacros (4.9 KB)
Keyboard Maestro Export

You could even automate this a bit more by adding an Insert-Text-By-Typing action to type the first few letters of the mailbox you want to reach in a hurry, although this might be interfered with if/when the menu mutates due to changes in the Mailbox sidebar of the Mail-Viewer window.

It's also possible to get names of mailboxes with AppleScript and then use the result to file things.

This script demonstrates only the task of getting names of sub-mailboxes from a given mailbox.

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/03/22 21:20
# dMod: 2022/03/22 21:20 
# Appl: Mail
# Task: Get Names of Sub-Mailboxes of a Given Mailbox
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Mail, @Names, @Sub-Mailboxes, @Mailbox
--------------------------------------------------------
property LF : linefeed
--------------------------------------------------------

set AppleScript's text item delimiters to LF
tell application "Mail"
   tell account "iCloud"
      set mailboxNameList to (name of mailboxes of mailbox "MCA") as Unicode text
   end tell
end tell

--------------------------------------------------------

From here you could use an AppleScript dialog or a Keyboard Maestro Prompt-With-List action to make your selection and then follow up by filing the selected email(s) in the chosen mailbox.

Yes, that's where I got with a move cursor step as moving from clicking a palette item collapses the menu the show menu action reveals.

I'll play with the AS to see if it's quicker, smoother. TY!

Thanks for all the examples here. On Monterery, I found that using the account name didn't work (error -1728) but using the account ID did. I created macros that moved to a named folder in a specific account ID which worked fine, until I tried to use them on a different synced Mac - with the same accounts. When I discovered that the same accounts have different account IDs on different Macs. Much trial and error followed.

The attached applescript KM macro first discovered the account ID for the currently selected message, then moves it to a named folder "SpamMissed" in that account. This means, for example that you can select multiple messages in multiple accounts, hit the hotkey, and have them all neatly filed within their respective accounts.

tell application "Mail"
	set selectedMessageList to selection
	
	# For each selected message
	repeat with theMessage in selectedMessageList
		# This obtains the accountID of the currently selected message
		set aMessage to item 1 of (get selection)
		tell aMessage
			set |mailBox| to its mailbox
		end tell
		set accountID to id of account of |mailBox|
		set destinationMailbox to mailbox "SpamMissed" of account id accountID of application "Mail"
		move theMessage to destinationMailbox
	end repeat
end tell

Mail (Spam).kmmacros (2.2 KB)

Reviving an old thread since this function is still absent from macOS and mail plugins will be unavailable once Sonoma is out. Some people may currently be using (e.g. Mail ActOn) for this purpose. Searching other posts, a workable solution is to use 'Show Palette of Macros' and 'Select or Show Menu' actions. It has performed well for single and multiple messages. I have attached a screenshot of the basic macro with a palette and what a basic palette looks like when triggered.