Skip to content

Instantly share code, notes, and snippets.

@khsing
Created December 11, 2013 08:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khsing/7906935 to your computer and use it in GitHub Desktop.
Save khsing/7906935 to your computer and use it in GitHub Desktop.
被QQ通讯录搞花了通讯录的标签,用该脚本修复一下
#!/usr/bin/python
# coding:utf-8
import sys
from AddressBook import *
from pprint import pprint
MAPPING = {
u'工作传真': kABPhoneWorkFAXLabel,
u'住宅传真': kABPhoneHomeFAXLabel,
u'iPhone': kABPhoneiPhoneLabel,
u'移动': kABPhoneMobileLabel,
u'住宅': kABPhoneHomeLabel,
u'工作': kABPhoneWorkLabel,
u'主要': kABPhoneMainLabel,
u'其他': kABOtherLabel,
u'电话': kABPhoneMainLabel,
u'mobile': kABPhoneMobileLabel,
u'iPhone': kABPhoneiPhoneLabel,
}
def phone_lable_mapping(l):
if l in MAPPING:
return MAPPING[l]
else:
return kABOtherLabel
def fixPhoneLabels(n):
labels = set()# pprint(dir(n))
phones = n.valueForProperty_(kABPhoneProperty)
if phones:
# print type(phones)
phoneNumberMultiValue = ABMultiValueCreateMutableCopy(phones)
# print type(phoneNumberMultiValue)
# phones.attributeKeys()
for i in range(0,phoneNumberMultiValue.count()):
key = phoneNumberMultiValue.labelAtIndex_(i)
# print key
if '-' in key:
an,t = key.split('-')
labels.add(t)
ABMultiValueReplaceLabel(phoneNumberMultiValue, phone_lable_mapping(t), i)
n.setValue_forProperty_(phoneNumberMultiValue,kABPhoneProperty)
return labels
ab = ABAddressBook.sharedAddressBook()
l = set()
for people in ab.people():
l.update(fixPhoneLabels(people))
# print '\n'.join(l)
ab.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment