Hi,
I like to access the Mac address book to make some auto completing textfield. For this I need either a quick search to the address book or complete access to all entries (I'm only interested im e-mail addresses).
Anyone already having some code for this?
Best,
Kukulkan
Anyone accessed the Mac address book?
Re: Anyone accessed the Mac address book?
Does this help ?
Code: Select all
ImportC "/System/Library/Frameworks/AddressBook.framework/AddressBook" : EndImport
Global NewList vCard.s()
Procedure GetCards()
Protected enum = CocoaMessage(0, CocoaMessage(0, CocoaMessage(0, 0, "ABAddressBook sharedAddressBook"), "people"), "objectEnumerator")
Protected obj = CocoaMessage(0, enum, "nextObject")
ClearList(vCard())
While obj
AddElement(vCard())
vCard() = PeekS(CocoaMessage(0, CocoaMessage(0, obj, "vCardRepresentation"), "bytes"), -1, #PB_UTF8)
obj = CocoaMessage(0, enum, "nextObject")
Wend
EndProcedure
If OpenWindow(0, 0, 0, 600, 400, "AddressBook example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
EditorGadget(0, 10, 10, 580, 380)
GetCards(); Get all address book entries as vCards
ForEach vCard()
AddGadgetItem(0, -1, vCard())
Next
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Anyone accessed the Mac address book?
Second example (start typing email address in input field)
Edit:
macOS documentation says not to use the AddressBook framework in macOS 10.11 and later.
The Contacts framework has to be used instead (see CNContactStore).
Unfortunately I haven't been able to get this working.
Code: Select all
#kABPrefixMatchCaseInsensitive = 10
ImportC "-framework AddressBook" : EndImport
Global AddressBook = CocoaMessage(0, 0, "ABAddressBook sharedAddressBook")
Procedure.s FindEmailAddress(PartialAddress.s)
Protected results.s, find, records, recordCount, emails, emailCount, i, j
find = CocoaMessage(0, 0, "ABPerson searchElementForProperty:$", @"Email", "label:", #nil, "key:", #nil, "value:$", @PartialAddress, "comparison:", #kABPrefixMatchCaseInsensitive)
records = CocoaMessage(0, AddressBook, "recordsMatchingSearchElement:", find)
recordCount = CocoaMessage(0, records, "count")
For i = 1 To recordCount
emails = CocoaMessage(0, CocoaMessage(0, records, "objectAtIndex:", i - 1), "valueForProperty:$", @"Email")
emailCount = CocoaMessage(0, emails, "count")
For j = 1 To emailCount
results + PeekS(CocoaMessage(0, CocoaMessage(0, emails, "valueAtIndex:", j - 1), "UTF8String"), -1, #PB_UTF8) + #LF$
Next
Next
ProcedureReturn results
EndProcedure
If OpenWindow(0, 0, 0, 600, 400, "AddressBook example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(0, 10, 10, 580, 20, "")
EditorGadget(1, 10, 40, 580, 350)
Repeat
SetGadgetText(1, FindEmailAddress(GetGadgetText(0)))
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
macOS documentation says not to use the AddressBook framework in macOS 10.11 and later.
The Contacts framework has to be used instead (see CNContactStore).
Unfortunately I haven't been able to get this working.
Last edited by wilbert on Mon May 15, 2023 1:49 pm, edited 2 times in total.
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Anyone accessed the Mac address book?
Wow, thats great! Thank you!
Sadly I can only test this on Monday first.
Sadly I can only test this on Monday first.