Newbe Question: Searching Address Book
Newbe Question: Searching Address Book
- Subject: Newbe Question: Searching Address Book
- From: Brad Bumgarner <email@hidden>
- Date: Mon, 1 May 2006 12:02:04 -0600
My name is Brad Bumgarner and I am new to Objective-C. I have been
using AppleScript since it was first introduced many (many) "moons"
ago. I have been using Project Builder / Xcode since OS X 10.2. I
have recently decided to step up to using Objective-C so that I may
add features to my apps that I can't do (the way I'd like to) in
AppleScript Studio. The scary thing is Objective-C is actually
starting to make sense :-)
My question: How can I search for a name in Address Book? I want to
be able to search for first names, last names and both first/last
names. I want to be able to do this much like Mail.app does. The code
below (attached to a text field via AppleScript's "on changed"
handler) works until a space character is entered. "theName" is
passed from the applescript code and contains the currently entered
text from the text field as it is being entered.
+(NSArray *)searchAdressbookForName:(NSString *)theName {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
ABAddressBook *AB = [ABAddressBook sharedAddressBook];
NSArray *firstNames = [ABPerson searchElementForProperty:
kABFirstNameProperty label: nil key: nil value: theName comparison:
kABContainsSubStringCaseInsensitive];
NSArray *lastNames = [ABPerson searchElementForProperty:
kABLastNameProperty label: nil key: nil value: theName comparison:
kABContainsSubStringCaseInsensitive];
ABSearchElement *firstAndLastNames = [ABSearchElement
searchElementForConjunction:kABSearchOr children:[NSArray
arrayWithObjects: firstNames, lastNames, nil]];
NSArray *searchResults = [AB
recordsMatchingSearchElement:firstAndLastNames];
int cnt = [searchResults count];
if (cnt > 0) {
int i;
NSMutableArray *resultNames = [[NSMutableArray alloc] init];
for (i=0;i<cnt;i++) {
NSString *firstN = [[searchResults objectAtIndex:i]
valueForProperty:kABFirstNameProperty];
NSString *lastN = [[searchResults objectAtIndex:i]
valueForProperty:kABLastNameProperty];
if ( firstN == nil ) {
[resultNames addObject:[NSString stringWithFormat:@"%@",firstN ]];
} else {
[resultNames addObject:[NSString stringWithFormat:@"%@ %@",
firstN, lastN ]];
}
}
[resultNames retain];
[pool release];
return (resultNames);
} else {
[pool release];
return nil;
}
}
@end
Thanks in advance for your help,
Brad Bumgarner, CTA
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden