Created
January 6, 2011 11:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First step towards a bayesian filter on Mail.app?