Scripting Smart Mailboxes

Posted on January 8, 2011
Filed Under Applescript, Automation, Mail, Python | 4 Comments

As I’ve often said scripting Mail can be a chore as Apple left so much unexposed to Applescript that most IT folks really want to script. It’s annoying and probably reflects Apple’s ambivalence towards Applescript. Someone over at Stack Overflow had asked how to do it. I told them I had a half-finished script that was working for this. I ended up finding the time to finish it off finally.

I can’t promise this does everything. But it’ll at least get you in the right area. I know a lot of people want to do this and there aren’t a lot of guides on the net.

Basically what I do is open up a new smart mailbox using GUI scripting and then fill in the blanks. Now be aware I’m doing all this with Appscript and Python rather than pure Applescript. You should be able to figure out quickly what is going on though.

Smart Mailbox

#!/usr/bin/python
 
import os
from appscript import *
 
 
def makesmartmailbox():
    M = app(u'Mail')
    SE = app(u'System Events').processes[u'Mail']
 
    # Open New Smart Mailbox Sheet
    M.activate()
    SE.menu_bars[1].menu_bar_items[u'Mailbox'].menus[1].menu_items[u'New Smart Mailbox\u2026'].click()        
 
    SMB = app(u'System Events').processes[u'Mail'].windows[1].sheets[1]
 
    # Set Smart Mailbox Name
    SMB.text_fields[1].value.set(u'Test')
 
    # Set rules as any 
    SMB.pop_up_buttons[1].click()
    SMB.pop_up_buttons[1].menus[1].menu_items[1].click()
    # switch to menu_items[2] to click "all"
 
    # Select First Rule
    SMB.scroll_areas[1].pop_up_buttons[1].click()
    # Click "From" (2cd menu item)
    SMB.scroll_areas[1].pop_up_buttons[1].menus[1].menu_items[2].click()
    SMB.scroll_areas[1].text_fields[1].value.set(u'person1@somewhere.com')
 
    # Create Second Rule
    SMB.scroll_areas[1].buttons[u'add criterion'].click()
    # the new rule type is button 3
    SMB.scroll_areas[1].pop_up_buttons[3].click()   
    SMB.scroll_areas[1].pop_up_buttons[3].menus[1].menu_items[2].click()
    # the new text area is text field 2
    SMB.scroll_areas[1].text_fields[2].value.set(u'person2@somewhere.com')
 
    # include messages from sent
    SMB.checkboxes[u'Include messages from Sent'].click()
 
    # Save the rule
    SMB.buttons[u'OK'].click()
 
def test():
    makesmartmailbox()
 
if __name__ == '__main__':
    test()

The only tricky bits is finding the type of GUI object each button you want to press is it. The second trick is that for pop up buttons you have to first click the button, which creates a menu, and then select the menu item. Menu items in Applescript start at the number 1.

You’ll notice that new rules increment the popup button number by 2 as there are two popup buttons on each row. If you want to modify what rule type is picked just change the menu item number from 2 to some other number. Say 1 for “Entire Message.”

Now of course in a production script you’d put in various sorts of error checking. But this works well for me right now.

Comments

4 Responses to “Scripting Smart Mailboxes”

For people regularly wanting to script their apps using GUI scripting I recommend the application “UI Browser” which makes it really easy to find the various UI components an application offers.

Greetings

Yes, although at $55 that is pretty pricey. Apple has a free UIElementInspector that does a lot of the same thing, albeit nowhere near as elegantly. (i.e. it’s a bit of a pain to use)

Just wondering how to install these Python scripts. Sorry for the newbie type question, but just need a push in the right direction.

I’d like to try the Salesforce script for dislaying emails!

I’ll see if I can’t do a novice post tonight.

The short answer is that you can put them anywhere you like. I usually created a directory in my ~/bin directory by category. So I have, for instance ~/bin/invoice with all my python scripts for invoicing, ~/bin/address for all my python scripts for addresses, and ~/bin/itunes for all the iTunes scripts.

At the top of any Python script you have #!/usr/bin/python which means that if you run the script from the command line tells the shell to use Python to run it. However I personally execute all my scripts either from within TextMate (just hit command-R when the window is open) or from Automator where I use the “Run Shell Script” command with the text:

python “pathtomypythonscript.py”

So honestly you could put them almost anywhere.

Leave a Reply