How can I automatically apply rules to my inbox on Apple Mail?

I’m new to AppleScript altogether. Based on my best effort research, I’ve incorporated 2 pieces of code I found into one.

  1. Apply rules to selected e-mail
  2. Open Apple’s Mail app and select all messages in a specified mailbox

Unfortunately, the final code does not work.

tell application "Mail"
   activate
   set selected mailboxes of message viewer 3 to {mailbox "foldernamehere"}
   set selected messages of message viewer 3 to every message of mailbox "foldernamehere"
   click menu item "Apply Rules" of menu "Message" of menu bar 1
end tell

[format]Syntax Error
Expected end of line, etc. but found class name[/format]

By the way, I have no idea what does the numeral beside “message viewer” in the AppleScript means, but I intended AppleScript to specifically select the inbox of the relevant account because there are many accounts in my list, and folder(s) of some accounts may be identically named.

Please let me know what went wrong and how to fix the code. Thank you!

The title of your post and what you explain us further are - different things. I will assume, you want apply some new rules created in the Preferences of Mail.app to old messages in your INBOX. Because new incoming messages are processed by Mail.app itself, automatically:


tell application "Mail"
	activate -- required  for GUI scripting (that is, for keystroke)
	set theMessages to messages of mailbox "INBOX" of account "Gmail"
	set frontMessageViewer to make new message viewer at end of message viewers
	delay 1 -- required to wait for the Mail.app response
	set selected mailboxes of frontMessageViewer to {mailbox "INBOX" of account "Gmail"}
	set selected messages of frontMessageViewer to theMessages
end tell

delay 1 -- (or, more seconds) -- wait while the Mail.app selects all inbox messages

tell application "System Events" to tell process "Mail"
	keystroke "l" using {command down, option down} -- Apply rules
end tell

By the way, message viewer index is index of certain viewer among multiple viewers you can make (using make command, see my code).

Dunno if it will make the script do what you want but at least it should allow it to compile.

UI scripting is a function of ‘system events’ rather than the exclusive domain of the active app.

Try wrapping the offending line in a special tell statement, like so:

	tell application "System Events" to tell process "Mail"
		click menu item "Apply Rules" of menu "Message" of menu bar 1
	end tell

Thanks for the suggestion! Unfortunately, it did not work for me. I got this error: [format]Mail got an error: Can’t get message viewer 1. Invalid index.[/format]

I lack the capability to comprehend the error and fix it. However, it’s fine though! Because KniazidisR’s code helped me a lot.

Thank you! But I want to point out that rules on emails in the mailbox were not successfully applied, presumably because the emails in the mailbox were not successfully selected by the script. I managed to get the script work by adding this line:

	tell application "System Events" to tell process "Mail" to keystroke "a" using command down

Full code:

tell application "Mail"
	activate -- required for GUI scripting (that is, for keystroke)
	set theMessages to messages of mailbox "INBOX" of account "Gmail"
	set frontMessageViewer to make new message viewer at end of message viewers
	delay 1 -- required to wait for the Mail.app response
	set selected mailboxes of frontMessageViewer to {mailbox "INBOX" of account "Gmail"}
	set selected messages of frontMessageViewer to theMessages
	tell application "System Events" to tell process "Mail" to keystroke "a" using command down
end tell

delay 1 -- (or, more seconds) -- wait while the Mail.app selects all inbox messages

tell application "System Events" to tell process "Mail"
	keystroke "l" using {command down, option down} -- Apply rules
end tell

In the past 4 days, I’ve been trying to enhance the script to improve code flexibility so that it can support both top-level mailbox and sub-mailbox and learn to incorporate user interaction to mitigate the risk of user interference during its execution since it’s GUI scripting. Interfering with GUI script execution could result in unintended consequence. The risk is very real if this script is scheduled to automatically run periodically. Imagine doing something half way, and then the script suddenly pops up and start to use those keyboard shortcuts on other application. Speaks of scheduling the script to automatically run it periodically, I had to learn how to accomplish that on macOS and ended up comfortably with using LaunchControl. :smiley:

Here is my final code: https://bit.ly/3MtZxmy :slight_smile:

I even managed to create a progress bar just for aesthetic purpose! (though the progress bar by default is buried by the Mail app while it’s executing)

I’m elated to have successfully programmed something useful and practical as a non-programmer! (though with the help from MacScripter forum)

I’m glad that you found a solution. For future reference, here is a snippet that should work to select that menu item. Two messages were selected in a standard 3-pane window. Not sure why that particular error was triggered. Regards.