Need help with scripting Mail

I have a long list of mail rules. All of them, normally, are enabled. About the fouth rule down unconditionally stops evaluation rules. Its called the “Stop Here” rule. This keeps all the messages except spam in my inbox. I read the messages as they come in.

My morning routine is to disable that rule, select all the messages in the In Box, and “Apply Rules” which sorts the messages into various mail boxes, etc. I then move the messages that remain to my “2006” mail box. I do the same with the “Sent” mail box. I then go back and re-enable the Stop Here rule.

Clearly something that needs to be scripted. But I don’t have a clue where to start. The problem is two fold:

First, I guess there are now two approachs to scripting: one via the GUI interface using “System Events” and the other via regular Applescript objects, etc. It seems like the “regular” method would be better for enabling and disabling the “Stop Here” rule… except I don’t see an action to enable or disable it. I see a property that tells me if it is enabled or disabled. But no way to change it.

Second, with the System Events thing, trying to figure out what something is called and how to get System Events to tickle the target application in the way that I want. That appears to me to be black magic.

Can anyone help me figure out either of these two stumbling blocks?


Hi pedz,

UI scripting is not nesessary, the solution is easy:

tell application "Mail" to set enabled of rule "Stop Here" to false -- (disable) or true (enable) 


How do you do apply the rules to a list of messages. I figured out:

	tell application "Mail"
		set inboxMessages to messages of inbox
	end tell

will get me the list of messages. Now I’m trying to figure how how to run the rules on all of them. “perform mail action with messages” looks promising but the dictionary I’m looking at doesn’t really say what it does exactly.

“perform mail action with messages” is a handler to be called by a script menu or a rule.
It’s similar to the open handler.

Rules are globally defined and an element of application class, not of message.
You can’t assign a rule explicitly to a single message

here is a diagram of the script hierarchy of Mail

I’m not trying to assign a rule.

I want to do the equivalent of selecting all the messages in the Inbox and then picking “Apply Rules” from the Message menu.

hm, the only alternative I know is:

tell application "Mail" to activate

tell application "System Events"
	tell process "Mail"
		keystroke "1" using command down
		delay 0.5
		keystroke "a" using command down
		delay 0.5
		keystroke "l" using {option down, command down}
	end tell
end tell


Thank you so very much. I never thought about using keys. I’ve only seen examples where the buttons, etc are picked.

Thanks again!

Small catch.

In the normal mail window, if the bottom panel is picked, then the select all command selects all of that message – not all of the messages in the Inbox.

I’m trying to figure out how to pick the top panel.

Wow. Thank you so much.

Is there a list of documents that I can study? I get so frustrated with Applescript. I have the language guide but I have not read it. Its too thick. And I try searching it but can never find things that help. I also try googling but that rarely helps. And then the GUI stuff seems even more magical.

What do you guys recommend?

I finally got my script to work. I posted it to my blog here:

Thank you all for helping.