Apple Mail Aikido 1.0

Moving Apple Mail Messages Using AppleScript, Automator, and Keyboard Shortcuts

For Inbox-zero practitioners, having a quick way to file email is critical. Far and away the fastest method is with keyboard shortcuts.

There used to be several plugins on the market that integrated shortcuts into the Apple Mail preferences pane including Mail Act-On by SquareCubed. The release of macOS Sonoma (macOS 14) removed support for plugins, killing these enhancements. 

As a response to this, we’ve concocted a workaround that we call “Mail Aikido”. 

 

How Does It Work?

Mail Aikido uses Automator, AppleScript, and Keyboard Shortcuts to turn your selected keyboard shortcuts into actions that move selected messages into mail folders. 

Step 1: You create “Quick Actions” with AppleScript using Automator that moves selected mail messages into folders. You create one “quick action” for every folder that you’d like to quickly move email into.

Step 2: You then create a custom keyboard shortcut for each folder that you’d like to target and assign it to the Apple Mail app.

Step 3: Upon first use, you will be prompted to grant Automator and Mail permissions to perform these actions via the System Settings.

Step 4: You will be able to quickly move messages into folders using keyboard shortcuts, greatly speeding your email workflow.

 

Setting Up Mail Aikido

This might look complicated but it is easier than it looks. Just repeat the Automator Scripts and Keyboard Shortcuts instructions below for each folder you would like to target with a keyboard shortcut:

Automator Scripts

  1. Open Automator
  2. Choose “Quick Action
  3. Choose the following settings:
    Workflow receives: No input
    In: Mail.app
    Image: Mail
    Color: Black
  4. In the search field, search for “Applescript”
  5. Drag “Run Applescript” into the composing window on the right
  6. Copy and paste AppleScript 1 from below into the text field
  7. Change the “mail account name” and “target folder” to match your email account and the folder you would like to move messages into
  8. Drag a second instance of “Run AppleScript” below your first script
  9. Copy and paste AppleScript 2 from below into the text field
  10. File > Save. Choose an obvious and unique name like action-newsletters

Two AppleScripts loaded in sequence in Automator

FYI: These are saved in ~/Library/Services/ as .workflow files. 

FYI: You should now see this in Apple’s Mail menu: Mail > Services

After saving quick actions in Automator they will show in the Mail Services menu

 

Keyboard Shortcuts

  1. Open System Settings > Keyboard > Keyboard Shortcuts …
  2. Choose App Shortcuts
  3. Click the “+” button and…
    Application: Mail.app
    Menu title: type the name of your action eg: action-newsletters
    Keyboard shortcut: type your desired shortcut eg: ctrl-n
    • Click “Done”

Addin an App-specific keyboard shortcut in System Settings

How app-specific shortcuts display in System Settings

 

FYI: In Apple Mail, you should now see the keyboard shortcut next to your action in the menu Mail > Services. This means you can use it.

After creating keyboard shortcuts in System Settings they will show in the Apple Mail Services menu

 

Permissions

When you first attempt to use your new shortcut you’ll be prompted to grant permission for Automator and Apple Mail to perform actions. Go to System Settings and set them. They will be:

Privacy & Security > Accessibility > Mail and Automator

Grant Automator and Apple Mail permission to control your computer

 

That’s too many words. Can you just show me how to do this?

Sure! Here is a YouTube video. It covers all the steps, but the AppleScripts shown are an earlier iteration: 

 

AppleScript 1: Move Messages into selected folders

(* Mail Aikido 1.0 Script 1 of 2 *)
(* Move messages into selected folders *)

(* Step 1: Change "mail account name in quotes" to match the human-readable name for the mail account that you want to copy mail into *)

(* Example myaccount name might be "GMail" or "Work" *)

(* Step 2: Change "target folder in quotes" to match the path and folder you would like to copy mail into *)

(* If your target folder is on the top level of your account, you only need to enter the folder name eg: "Newsletters" *)

(* If your target folder is nested, use forward slashes to traverse the hierarchy eg: "Receipts/2024" *)

 tell application "Mail"

    set myaccount to "mail account name in quotes" 
    set theMessages to selection

    repeat with eachMessage in theMessages
        copy eachMessage to mailbox "target folder" of account myaccount
        delete eachMessage
    end repeat

end tell

 

Applescript 2: Select the first message in the current mailbox

After filing messages, you might notice that your inbox reverts to a state where no messages are highlighted. To save the extra keystroke of choosing the next message to sort (every keystroke adds up!) you can add a second AppleScript to run after each mail filing action:

(* Mail Aikido 1.0 Script 2 of 2 *)
(* Select first message in current mailbox *)

tell application "System Events"
    tell process "Mail"
        select row 1 of table 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1
    end tell
end tell
 

Troubleshooting

Far and away, the two most common problems you will run into are:

  1. Spelling
  2. Permissions

If your keyboard shortcut is not throwing any errors but not moving mail where you want, check the script in Automator. We all know how to type our account and folder names but it’s easy to have a stray letter or space get in the way, or a missing quotation mark.

If your keyboard shortcut is throwing an error like…

 "The action “Run AppleScript” encountered an error: “System Events got an error: Automator Workflow Runner (WorkflowServiceRunner, [appname]) is not allowed to send keystrokes."

…you’ll check two things:

  • That System Settings > Privacy & Security > Accessibility > Mail and Automator are both set to “on”
  • If toggling the permissions above doesn’t work, you can open Automator and choose the menu Automator > Reset Warnings. Not sure why this one works, but it usually does.

 

Changing Keyboard Shortcuts

The most reliable way of updating keyboard shortcuts in System Settings is to click the “-” button to remove the shortcut, followed by the “+” to re-add it. There is some functionality to change the text of existing shortcuts, but it doesn’t always work reliably.

If you want to change your script, the easiest way is to open Automator and use File > Open Recent. You can also double-click .workflow files in ~/Library/Services

 

Credits and References:

 

Changelog:

  • 1.0 Initial public release (Available on GitHub)

 

Compatibility

Mail Aikido has been tested and confirmed to be working with:

• macOS Sonoma (macOS 14)
• macOS Ventura (macOS 13)

Given the toolkit, Mail Aikido is hypothetically compatible with every Apple OS since OS X. As users confirm compatibility it will be added to the list above.

 

Special Notes and Commentary

During development, it was noted that “moving” messages (by using “set” {directory} or “move” {directory}) worked with IMAP and Exchange but left a copy of messages in GMail directories. This led to previously moved messages reappearing in the mailbox. This is why the script employs a “copy” followed by a “delete”. 

Sometimes after deleting a message or set of messages, the mailbox will be left in a state where no message is selected. To save the click, the second script was added to highlight the first message in the mailbox. This second script does NOT scroll to the highlighted message, however–it presumes you’re working from the top of your mailbox to the bottom. 

There may be a way to calculate the “next” message to highlight based on your selection but

  1. This is above my pay grade
  2. Whatever the solution might be, it will need to consider which message to count for “next” when multiple messages are selected, with the possibility being that some of them are off-screen. 

5 Comments

  • Chris

    Just recently kicked out Outlook and moved to Apple Mail. Your script seems to nearly do what I am currently missing. But rather than creating multiple fixed folder destination I would want to have search field popping up where I can enter the desired folder destination. Any thoughts on that?

    • Haus Interactive Post author

      Hi Chris. An earlier version of this script did include the functionality you are looking for but it was removed because it added an extra step to the process. You can add this to line 17 (and move the current line 17 to line 18):

      set targetfolder to choose from list of folderlist.

      This will pull up a list of targetable mail folders.

      Alternatively, some have recommended MsgFiler which accomplishes the same thing, but with less setup and a nicer interface: https://msgfiler.com/

  • Chris

    Thanks a lot for your help and the hint with msgfiler which seems to work pretty well. I will try your solution with the added lines as well the other day. Thanks again, you made my day!

  • Jerry Levinson

    Is it possible to have Automator select all emails for a year and move them to a desktop folder? My brother has 400,000+ emails and I’d like to get them all off his email account and into desktop folders by year so he can look through them more easily than in Mail. He started getting spammed in 2015 and just ignored them.

    • Haus Interactive Post author

      This certainly sounds possible, though it is a larger topic than a blog post comment section will handle. We can say that you won’t be copying email messages to your Desktop, but you could certainly copy them to a local Apple Mail folder rather than the inbox on the email server.

      There are a number of forums where this kind of problem can be solved including Apple’s own AppleScript Forums, Mac Scripter, and Mac Rumors, to name a few. AppleScript is a bit of a deep, dark, rabbit hole but there is quite a bit you can do with it when you find an experienced wizard to guide your way.

Would you like to post a comment?

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>