How to use sharedAddressBook ?

Hi, I continue to discover ASOC.

I wonder why the following lines return an error (unrecognized function sharedAddressBook. (error -10000)) :

on applicationWillFinishLaunching_(aNotification)
		set monCarnetDeContacts to current application's ABAddressBook's sharedAddressBook()
	end applicationWillFinishLaunching_

What should I do ?

Thanks,
Nicolas

Have you added ABAddressBook.framework to your project?

Thanks for your help !

After adding the framework to the project, there is no error for this line.

However, trying to progress, the next line is an error (unrecognized selector sent to instance) :

set thePerson to monCarnetDeContacts's initWithAddressBook_(monCarnetDeContacts)

Regards,
Nicolas

You left out an “alloc()'s” before the init…

Thanks again Shane,

I found in your book the need to “alloc()”… and managed to use it with init().

set monCarnetDeContacts to current application's ABAddressBook's sharedAddressBook()
       
       tell current application's ABPerson to set thePerson to alloc()'s init()

To create a new person, I understand it has to be an instance of ABPerson.

After that, I struggle to set a first name (or to get the first name, even if equal to missing value by default). Each of the following lines returns an error :

tell thePerson to set leNom to valueForProperty_("kABFirstNameProperty")
thePerson's setValue_forProperty_("test","kABFirstNameProperty")

Is it because the value needs to be a NSSTring ?
I managed in another script to use defaults and preferences, using objectForKey.

Regards,
Nicolas

Edit : I found, after searching again the forums, that replacing “kABFirstNameProperty” by “First” would do the trick. What I don’t understand is how I’am supposed to know this, as I spent some time finding the “kABFirstNameProperty” in the documentation for ABRecord ?

Beats me. I suspect there may be something wrong with the framework’s bridge file. I came across it by running:

current application's ABPerson's |properties|()

--> {AIMInstant, BirthdayComponents, ABDateComponents, MaidenName, Birthday, ICQInstant, FirstPhonetic, MiddlePhonetic, calendarURIs, Suffix, com.apple.carddavvcf, LastPhonetic, UID, MSNInstant, JobTitle, HomePage, InstantMessage, YahooInstant, com.apple.collectionpath, First, Middle, JabberInstant, com.apple.etag, Modification, Nickname, ABRelatedNames, Title, Address, Phone, Organization, ABDepartment, com.apple.synced, Creation, Note, Email, Last, SocialProfile, ABPersonFlags, RemoteLocation, com.apple.vcardhash, com.apple.uuid, ABDate, com.apple.addlInfo, URLs}

Thanks :slight_smile:

Hi,

I continue to progress, and I am able to create a person, enter a value for the first name and last name in a window, set the properties of the person and retrieve them ! I struggled to understand that the window delivered textField that needed to be converted to NSString, and managed to do that.

But…

I don’t see how to use the function ABAddressBookGetPersonCount.
It seems that it asks for a ABAddressBookRef and I only have a ABAddressBook as an argument ?

set monCarnetDeContacts to current application's ABAddressBook's sharedAddressBook()
set leNombreDePersonnes to ABAddressBookGetPersonCount_(monCarnetDeContacts)

What am I missing ? (error : “unrecognized selector sent to object”)

Thanks,
Nicolas

ABAddressBookGetPersonCount() is a C function which is only available in iOS

the MacOS / ASOC equivalent is


current application's ABAddressBook's sharedAddressBook()'s people()'s |count|()

:rolleyes: of course !

Many thanks