Skip to content

Commit 0687f52

Browse files
author
Prabhu
committed
Added couple of more methods.
1 parent 476a25e commit 0687f52

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

README

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
addressbook
2-
------------
1+
macutils
2+
---------
33

4-
Python wrapper around Mac OSX Address Book.
4+
Package containing mac related utils to be used in python modules.
55

66
Requirements
77
-------------
88
pyobjc (Tested with 2.2)
99

1010
easy_install pyobjc
1111

12+
13+
CONTENTS
14+
=========
15+
16+
* addressbook.py
17+
18+
Python wrapper around Mac OSX Address Book.
19+
20+
Usage
21+
-----
22+
from macutils import addressbook
23+
24+
print addressbook.all()

addressbook.py

+36-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
import os
1212

1313
# List of properties to be ignored.
14-
IGNORED_PROPS = ["com.apple.ABPersonMeProperty", "ABPersonFlags", "com.apple.ABImageData",]
14+
IGNORED_PROPS = ["com.apple.ABPersonMeProperty", "ABPersonFlags",
15+
"com.apple.ABImageData",
16+
"com.apple.ABGroupMembersProperty",
17+
]
1518

1619
def getAddressBook():
1720
"""
@@ -23,23 +26,40 @@ def all():
2326
"""
2427
Returns all the people from the addressbook.
2528
"""
26-
peopleList = []
27-
allPeople = getAddressBook().people()
28-
for people in allPeople:
29-
aperson = {}
30-
for prop in people.allProperties():
29+
return _clist(getAddressBook().people())
30+
31+
def _clist(slist):
32+
"""
33+
Method to convert NSArray to python list
34+
"""
35+
retList = []
36+
for p in slist:
37+
aobj = {}
38+
for prop in p.allProperties():
3139
if prop in IGNORED_PROPS:
3240
continue
33-
tmpval = people.valueForProperty_(prop)
41+
tmpval = p.valueForProperty_(prop)
3442
if type(tmpval) == ABMultiValueCoreDataWrapper:
3543
aval = [_getVal(tmpval.valueAtIndex_(i)) for i in range(0, tmpval.count())]
3644
else:
37-
aval = _getVal(tmpval)
45+
aval = _getVal(tmpval)
3846
if aval is not None:
39-
aperson[prop.lower()] = aval
40-
peopleList.append(aperson)
41-
return peopleList
47+
aobj[prop.lower()] = aval
48+
retList.append(aobj)
49+
return retList
4250

51+
def groups():
52+
"""
53+
Return groups
54+
"""
55+
return _clist(getAddressBook().groups())
56+
57+
def me():
58+
"""
59+
Returns the current logged in user.
60+
"""
61+
return _clist([getAddressBook().me()])[0]
62+
4363
def _getVal(tmpval):
4464
"""
4565
Extract value from unicode or Date object.
@@ -52,10 +72,12 @@ def _getVal(tmpval):
5272
elif type(tmpval) == NSCFDictionary:
5373
aval = dict([(k.lower(), tmpval[k]) for k in tmpval.keys()])
5474
return aval
55-
56-
def main():
57-
all()
5875

76+
def main():
77+
#print all()
78+
#print groups()
79+
print me()
80+
5981
if __name__ == '__main__':
6082
main()
6183

0 commit comments

Comments
 (0)