Last active
September 30, 2017 17:38
-
-
Save ento/6590283 to your computer and use it in GitHub Desktop.
Print some properties of members of your Mac AddressBook.
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
# -*- coding: utf-8 -*- | |
''' | |
Print some properties of members of your Mac AddressBook. | |
https://developer.apple.com/library/mac/documentation/userexperience/Reference/AddressBook/Miscellaneous/AddressBook_Constants/Reference/reference.html#//apple_ref/doc/uid/TP40003952-CH3g-SW6 | |
$ pip install -U pyobjc-core | |
$ pip install -U pyobjc | |
''' | |
from collections import OrderedDict | |
from Foundation import NSDate, NSDateFormatter | |
from AddressBook import * | |
props = OrderedDict([ | |
('first_name', kABFirstNameProperty), | |
('creation_date', kABCreationDateProperty), | |
('modification_date', kABModificationDateProperty), | |
]) | |
def iter_groups(abref): | |
for abgroup in abref.groups(): | |
yield abgroup.uniqueId(), abgroup.name(), abgroup | |
def iter_member_properties(member, propkeys): | |
for key in propkeys: | |
yield key, member.valueForProperty_(props[key]) | |
def make_formatter(args): | |
date_formatter = NSDateFormatter.alloc().init() | |
date_formatter.setDateFormat_(args.date_format) | |
def _format(value): | |
if isinstance(value, NSDate): | |
value = date_formatter.stringFromDate_(value) | |
return value | |
return _format | |
def print_member(member, propkeys, formatter): | |
for key, value in iter_member_properties(member, propkeys): | |
print key | |
print formatter(value) | |
print '-' * 30 | |
def main(args): | |
ab = ABAddressBook.sharedAddressBook() | |
propkeys = props.keys() | |
sortprop = props[args.member_sort] | |
formatter = make_formatter(args) | |
for unique_id, name, abgroup in iter_groups(ab): | |
if args.group and name not in args.group: | |
continue | |
members = sorted(abgroup.members(), key=lambda m: m.valueForProperty_(sortprop)) | |
for member in members: | |
print_member(member, propkeys, formatter) | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-g', '--group', nargs='*') | |
parser.add_argument('--member-sort', default='first_name') | |
parser.add_argument('--date-format', default='yyyy-MM-dd HH:mm:ss') | |
args = parser.parse_args() | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment