Skip to content

Instantly share code, notes, and snippets.

@citrus-lemon
Last active January 7, 2017 14:46
Show Gist options
  • Save citrus-lemon/8c676e2606ce84c32e2b31a29f885317 to your computer and use it in GitHub Desktop.
Save citrus-lemon/8c676e2606ce84c32e2b31a29f885317 to your computer and use it in GitHub Desktop.
Check Mac Contacts to add +86 before Chinese phone number and add pinyin for Chinese name
#!/usr/bin/osascript
set change to true -- if you need change the Contacts, toggle it to TRUE
(*turn Chinese characters to pinyin*)
on pinyin(chars)
(*python3 pypinyin needed*)
return do shell script "echo '"&chars&"' | /usr/local/bin/python3 -c 'from pypinyin import pinyin, lazy_pinyin;print(\"\".join([a.capitalize() for a in lazy_pinyin(input())]))'"
end
tell application "Contacts"
(*get every person of Contact*)
set persons to people
(*iter every person*)
repeat with i from 1 to number of items in persons
(*person object*)
set _person to item i of persons
(*person name*)
set _name to name of _person
(*iter every phone*)
set check_phone to true
if check_phone then
repeat with j from 1 to number of items in phones of _person
(*phone object*)
set _phone_obj to item j of phones of _person
(*phone label*)
set _phone_label to label of _phone_obj
(*phone number*)
set _phone_value to value of _phone_obj
(*select china's phone number and format*)
set _china_phone to do shell script "echo '"&_phone_value&"' | ruby -e \"b = (gets).gsub(/[- _\\n]/,'');puts b[/^(?=\\d{11}$)^1(?:3\\d|4[57]|5[^4\\D]|7[^249\\D]|8\\d)\\d{8}$/] ? '+86 ' + (b[0..2]+'-'+b[3..6]+'-'+b[7..10]) : ''\""
if _china_phone is not "" then
if change then
set value of _phone_obj to _china_phone
save
end if
log _name & " " & _phone_label & " " & _phone_value & " ==> " & _china_phone
end if
end repeat
end if
(*name pinyin added (Chinese name only)*)
set check_pinyin to true
if check_pinyin then
set _is_all_chinese to do shell script "echo '"& last name of _person & first name of _person &"' | ruby -e 'puts (gets) =~ /^[\\u4e00-\\u9fa5]+$/'"
if _is_all_chinese is not "" then
if change then
set phonetic last name of _person to my pinyin(last name of _person)
set phonetic first name of _person to my pinyin(first name of _person)
save
end if
(*chinese name no middle name*)
log _name & " ==> " & my pinyin(_name)
end if
end if
end repeat
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment