Note: I am not asking how to create a rule which runs an Applescript. I want to go the other way around: run a script that will apply existing rules to all the messages in the inbox. That is, simulate

  1. Select Inbox
  2. Select All messages
  3. Menu Message/Apply Inbox Rules

I have ActOn rules that purge certain old messages, and I'd like a script I can set to run, say, nightly.

EDIT: My Google search turns up only people doing this by hand or with Keyboard Maestro. Rather discourgingly, I don't see any verb exposed in the Apple Mail script dictionary to run the rules, only to make or edit them. (See screenshots.)ScreenshotScreenshot

Is this really so difficult?


1 Answer 1


You don't really need the dictionary for this, as all the things you need to do are key-commandable. The only manual step is to deselect all afterwards [I can't, or am not willing to, test this script in action. I have some fairly complex rules plus some manual mail-shifting which a global Rules run will upset.]

You don't really need much more than

tell application "Mail" to activate
tell application "System Events"
    keystroke "1" using command down --select inbox
    keystroke "a" using command down --select all
    keystroke "l" using {command down, option down} --run Rules
end tell

perhaps with some delays added to give it time to catch up.
I haven't run this to test it, but it's simple enough syntactically & does compile correctly.

After that all you need is a way to trigger it on time. You can do that by either keeping it constantly running & doing its own time check, or launching it from Calendar.

See Applescript run code at specific time and Softron - HOW TO: Trigger an AppleScript at specific date and time

Little trick to deselect everything in a mailbox when All are selected.
Cmd/click one mail, which will deselect it.
Click the same mail again, will select it.
Cmd/click the same mail again, will deselect.

  • 1
    Wow. I'll try this. Should have thought about System Events; I don't do much scripting. I plan to install as a cron job, or, to be more modern, a Launch Agent, set to weekly. Feb 18, 2023 at 19:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .