Skip to content

Instantly share code, notes, and snippets.

@rcarmo
Created January 6, 2011 11:29
Show Gist options
  • Save rcarmo/767786 to your computer and use it in GitHub Desktop.
Save rcarmo/767786 to your computer and use it in GitHub Desktop.
This snippet walks through your mailbox and tallies message sizes, flagged and unread status using the Python Scripting Bridge and Mail.app. It isn't speedy, but it appears to be light on system resources.
from ScriptingBridge import *
import os, sys, re
pattern = re.compile("^(INBOX|Sent*|Draft*)")
class Census:
"""Tallies mail info"""
def __init__(self, pattern):
self.app = SBApplication.applicationWithBundleIdentifier_("com.apple.mail")
self.accounts = dict([(a.name(),a) for a in self.app.accounts()])
# Jedi mind tricks
self.mailboxes = reduce(list.__add__,list([[m for m in a.mailboxes() if pattern.match(m.name())] for a in self.app.accounts()]))
self.pattern = pattern
self.stats = {'unread':0, 'flagged':0, 'total':0, 'bytes':0}
def run(self):
for m in self.mailboxes:
print m.name()
self.stats['unread'] += m.unreadCount()
for i in m.messages():
print i.subject()
if i.flaggedStatus():
self.stats['flagged'] += 1
self.stats['total'] += 1
self.stats['bytes'] += i.messageSize()
return self.stats
if __name__ == '__main__':
c = Census(pattern)
print c.run()
@alcides
Copy link

alcides commented Jan 6, 2011

First step towards a bayesian filter on Mail.app?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment