jjgod / apn
- Source
- Commits
- Network (0)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
ebd81aa
commit ebd81aaacaf2ffabe0ae7703deca63cc372b608a
tree 96eafac1beee9953cf4c548647581a625e71a94d
parent 4bb1ea348d90ef261a8ab07284a50db5216c179a
tree 96eafac1beee9953cf4c548647581a625e71a94d
parent 4bb1ea348d90ef261a8ab07284a50db5216c179a
apn / AssignPhoneticName.py
| 8b06a350 » | jjgod | 2008-07-24 | 1 | #!/usr/bin/python | |
| 2 | |||||
| 3 | import sys, pinyin | ||||
| 4 | from Foundation import * | ||||
| 5 | from ScriptingBridge import * | ||||
| 6 | |||||
| 7 | reset = False | ||||
| 8 | |||||
| 9 | def is_cjk_char(x): | ||||
| 10 | # Punct & Radicals | ||||
| 11 | if x >= 0x2e80 and x <= 0x33ff: | ||||
| 12 | return 1 | ||||
| 13 | |||||
| 14 | # Fullwidth Latin Characters | ||||
| 15 | if x >= 0xff00 and x <= 0xffef: | ||||
| 16 | return 1 | ||||
| 17 | |||||
| 18 | # CJK Unified Ideographs & | ||||
| 19 | # CJK Unified Ideographs Extension A | ||||
| 20 | if x >= 0x4e00 and x <= 0x9fbb: | ||||
| 21 | return 1 | ||||
| 22 | # CJK Compatibility Ideographs | ||||
| 23 | if x >= 0xf900 and x <= 0xfad9: | ||||
| 24 | return 1 | ||||
| 25 | |||||
| 26 | # CJK Unified Ideographs Extension B | ||||
| 27 | if x >= 0x20000 and x <= 0x2a6d6: | ||||
| 28 | return 1 | ||||
| 29 | |||||
| 30 | # CJK Compatibility Supplement | ||||
| 31 | if x >= 0x2f8000 and x <= 0x2fa1d: | ||||
| 32 | return 1 | ||||
| 33 | |||||
| 34 | return 0 | ||||
| 35 | |||||
| 36 | def contain_cjk_char(line): | ||||
| 37 | # print "@: %s" % (line.encode("utf-8")) | ||||
| 38 | |||||
| 39 | for ch in line: | ||||
| 40 | if is_cjk_char(ord(ch)): | ||||
| 41 | return 1 | ||||
| 42 | |||||
| 43 | return 0 | ||||
| 44 | |||||
| 45 | def assign_pinyin(getName, getPhoneticName, setPhoneticName): | ||||
| 46 | name = getName() | ||||
| 47 | |||||
| 48 | if name and contain_cjk_char(name): | ||||
| 49 | pname = getPhoneticName() | ||||
| 50 | if not reset and pname: | ||||
| 51 | return None | ||||
| 52 | |||||
| 53 | name_py = pinyin.hanzi2pinyin(name).capitalize() | ||||
| 1a0c62cf » | jjgod | 2008-07-24 | 54 | setPhoneticName(name_py) | |
| 8b06a350 » | jjgod | 2008-07-24 | 55 | ||
| 56 | return (name, name_py) | ||||
| 57 | |||||
| 58 | return None | ||||
| 59 | |||||
| 60 | ab = SBApplication.applicationWithBundleIdentifier_("com.apple.AddressBook") | ||||
| 61 | |||||
| 62 | if len(sys.argv) > 1 and sys.argv[0] == '-r': | ||||
| 63 | reset = True | ||||
| 64 | |||||
| 65 | for person in ab.people(): | ||||
| 66 | fname_pair = assign_pinyin(person.firstName, | ||||
| 67 | person.phoneticFirstName, | ||||
| 68 | person.setPhoneticFirstName_) | ||||
| 69 | lname_pair = assign_pinyin(person.lastName, | ||||
| 70 | person.phoneticLastName, | ||||
| 71 | person.setPhoneticLastName_) | ||||
| 72 | |||||
| 73 | if fname_pair or lname_pair: | ||||
| 74 | print "%s%s (%s%s%s)" % ((fname_pair and fname_pair[0]) or "", | ||||
| 75 | (lname_pair and lname_pair[0]) or "", | ||||
| 76 | (lname_pair and fname_pair and " ") or "", | ||||
| 77 | (fname_pair and fname_pair[1]) or "", | ||||
| 78 | (lname_pair and lname_pair[1]) or "") | ||||
| 79 | |||||
| 80 | print "Done." | ||||
| 81 | |||||
