Address Book & Xcode

Hi Folks,

has anybody a clue how to bring all the contents of the address book into a
xcode AS Application?

Thanks for any hints,

Stefan

read the dictionary of AdressBook about the stucture,
gather the data, and process it to your Xcode application.

Or parse ~/Library/Application Support/AddressBook/AddressBook.data, but this is very complicated

Hi Stefan,

I am sorry, but I still need your help :frowning:


tell application "Address Book"
set phoneList to {}
set peopleCount to (count every person)
repeat with i from 1 to peopleCount
set phoneList to phoneList & (get value of every phone of person i)
end repeat
return phoneList
end tell


Is presenting all the found Phone Numbers - but what can I do to not only present the Number but also the name?

Reading the Dictionary I found out, that “name” and “first name” should be used - but when I try


set phoneList to phoneList & (get value of every phone, first name, name of person i) 

then it is not working (property error)

thanks for your help!

Stefan

try this, you get a nested list like

{{name1, {phone1, phone2}}, {name2, {phone1}}, {name3, {phone1, phone2, phone3}}}
tell application "Address Book"
    set phoneList to {}
    repeat with onePerson in (get people)
        set end of phoneList to {name of onePerson, (get value of every phone of onePerson)}
    end repeat
    return phoneList
end tell

Hi Stefan,

thats cool…

now - when I try to bring the results into a data view - I go like this:


tell Application "Address Book"
set phoneList to {}
repeat with onePerson in (get people)
set end of phoneList to text items 1 thru 2 of {name of one Person, (get value of phon of onePerson)}
end repeat
set content of theObject to phoneList

But I receive an error:

Can`t make content of <> id 64 of <> id 63 of <> id 62 of window id3 into type reference. (-1700)

Thanks for your help!

Stefan

In Mac OS X 10.4 (Tiger) you can use the AddressBook framework. However, you’ll need to understand Objective-C.
Although I haven’t used it, I think the idea of this framework is that you can read records without the Address Book application being open.
See the documentation for ABAddressBook in Xcode for more information.

Of course you do, because you have a nested list and you need a flattened one like

{{name1, phone1, phone2}, {name2, phone1}, {name3, phone1, phone2, phone3}}

Try this, it takes only the persons who have at least a phone number

tell application "Address Book"
    set phoneList to {}
    repeat with onePerson in (get people whose phones is not {})
        set end of phoneList to {name of onePerson} & (get value of phones of onePerson)
    end repeat
    set content of theObject to phoneList
end tell

Note: Your text item construction is useless in this case

Hi Stefan,

thanks a lot - this error is gone - but a new one occurs:

AppleScript Error - Address Book got an error: Can*t make {{…}} - I even tried with more rows in the data view…


tell application "Address Book"
   set phoneList to {}
   repeat with onePerson in (get people whose phones is not {})
       set end of phoneList to {name of onePerson} & (get value of phones of onePerson)
   end repeat
display dialog "here" -- is showing up
display dialog phoneList -- error showing
   set content of theObject to phoneList
end tell


thanks for your help!

Stefan

display dialog expects a string (actually Unicode text), not a list
You have to coerce the list to a string

If there’s an API for getting Address Book data (which there is), you shouldn’t be parsing its persistent store file. Its implementation details may change behind your back, whereas the API isn’t likely to do so in a way that would break your application.

I know, Sir, and I actually didn’t mean business, because it might be too complicated to parse the data file :wink:

Hi Stefan,

thanks for your hint about display dialog



tell application "Address Book"
set phoneList to {}
repeat with onePerson in (get people whose phones is not {})
set end of phoneList to {name of onePerson} & (get value of phones of onePerson)
end repeat
set phoneList_dialog to phoneList as string
display dialog "here" -- is showing up
display dialog phoneList_dialog -- now correct - showing
set content of theObject to phoneList
end tell


Not display dialog is correct - but while trying to add the phoneList to the DataView I still get the error:

Can`t make content of <> id 64 of <> id 63 of <> id 62 of window id3 into type reference. (-1700)

Thanks for your help!

Stefan

Check the reference of theObject, it’s obviously wrong

Hi Stefan,

I am afraid, but the references should be ok - I tried the following in the same application - Version 2 is working - Version 1 not…


on awake from nib theObject
if name of theObject is "adressbook" then
-- beginn of not working
tell phoneList to {} 
repeat with onePerson in (get people whose phones is not {})
set end of phoneList to {name of onePerson} & {get value of phones of onePerson}
end repeat
set content of theObject to phoneList
end tell

-- end of not working

end if
end awake from nib


on awake from nib theObject
if name of theObject is "adressbook" then
-- beginn of working
set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
set num_list to paragraphs of (read theFile)
set {tid, text item delimiters} to {text item delimiters, ";"}
repeat with num in num_list
set end of myList_ab to text items 1 thru 3 of num
end repeat
set text item delimiters to tid
set content of theObject to myList_ab
-- end of working

end if
end awake from nib

thanks for your help!

Best regards,

Stefan

Is “adressbook” not a typo?

Hi Folks,

I have found the best solution for that and will show the results as soon as possible…

Thanks for your time,

Stefan

Hi Folks,

I have found a solution by adding the “Address Book” palette into IB (/Developer/Extras/Palettes/ABPalette.palette)

I have named it: people_picker

Then I have add the following files into my project:

ABHandler.h

ABHandler.m

If you want something different as the phone Number than you have to replace everything what is “phone” by “email”…

This two files have to be imported into your application!

The next step is to import the Address Book Framework!

The Applescript code to select a number is the following (I have named the Button “copy_number_sms”


if name of theObject is "copy_number_sms" then
		-- hier wird die aktuell gewählte nummer fürs senden gesetzt!
		
		try
			set object_name to name of theObject as Unicode text
			set the_id to my get_id()
			if the_id = false then error "nothing selected"
			if object_name = "open" then
				open location "addressbook://" & the_id
			else if object_name = "copy_number_sms" then
				display dialog "da"
				set the_record to call method "getABDictFromID:" of class "ABHandler" with parameter the_id
				set the_info to {the_record's phone}
				set contents of text field "sms_nummer" of drawer "Outbox" of window "main" to (the_info as Unicode text)
			end if
		on error the_error
			log "Error: " & the_error
		end try
end if


Hi Folks,

has someone a clue how I can get the Label “mobile” out of the property abphoneproperty?

thanks for your help,

Stefan

Try using ABMultiValue’s method - (int)indexForIdentifier:(NSString *)identifier, passing kABPhoneMobileLabel.

of course it could’t work:
You forgot to target AddressBook

on awake from nib theObject
if name of theObject is “adressbook” then
– beginn of not working
tell application “Address Book”
tell phoneList to {}
repeat with onePerson in (get people whose phones is not {})
set end of phoneList to {name of onePerson} & {get value of phones of onePerson}
end repeat
end tell
set content of theObject to phoneList
end tell