-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAssignPhoneticName.m
53 lines (46 loc) · 1.66 KB
/
AssignPhoneticName.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// AssignPhoneticName.m
#import <AddressBook/AddressBook.h>
#import <CoreFoundation/CoreFoundation.h>
@interface NSString (PhoneticAdditions)
- (NSString*)phoneticString;
@end
@implementation NSString (PhoneticAdditions)
- (NSString*)phoneticString {
NSMutableString* string = [self mutableCopy];
CFStringTransform((CFMutableStringRef)string,
NULL,
kCFStringTransformMandarinLatin,
NO);
NSString* result = nil;
if (![string isEqualToString:self]) {
result = [[string stringByReplacingOccurrencesOfString:@" " withString:@""]
capitalizedString];
}
[string release];
return result;
}
@end
const char* emptyIfNil(NSString* string) {
return string ? [string UTF8String] : "";
}
int main(int argc, char* argv[]) {
@autoreleasepool {
ABAddressBook* ab = [ABAddressBook sharedAddressBook];
for (ABPerson* person in [ab people]) {
NSString* lastName = [person valueForProperty:kABLastNameProperty];
NSString* phoneticLastName = [lastName phoneticString];
NSString* firstName = [person valueForProperty:kABFirstNameProperty];
NSString* phoneticFirstName = [firstName phoneticString];
if (phoneticLastName)
[person setValue:phoneticLastName forProperty:kABLastNamePhoneticProperty];
if (phoneticFirstName)
[person setValue:phoneticFirstName forProperty:kABFirstNamePhoneticProperty];
if (phoneticLastName || phoneticFirstName)
printf("Update %s%s (%s %s)\n",
emptyIfNil(lastName), emptyIfNil(firstName),
emptyIfNil(phoneticLastName), emptyIfNil(phoneticFirstName));
}
[ab save];
}
return 0;
}