Reputation: 8395
Let's say that I've just created an ABPerson
record and managed to save it in the user's address book. How do I programmatically open the default application which handles the address book (which most likely is Contacts but in some cases it might be Outlook or some other app) and show the new address book record I've just added?
Thanks in advance.
Upvotes: 2
Views: 572
Reputation: 616
Here is my take using the Contacts app, and in Swift, written as an extension for CNContact
. I expect most people are using Contacts in preference to AddressBook nowadays.
(CNContact's identifier
is the same as ABPerson's uniqueId
.)
func showInContacts() {
let path =
"/Users/someusername/Library/Application Support/AddressBook/Sources/05A62A31-9C1F-423F-A9F4-011E56EAAF29/Metadata/0A1F4FC2-7E01-4A40-92DE-840F8C84DE58:ABPerson.abcdp
var url = URL(fileURLWithPath: path)
url.deleteLastPathComponent()
url.appendPathComponent(self.identifier)// self is a CNContact
url.appendPathExtension("abcdp")
NSWorkspace.shared.open(url)
}
Contacts are in separate files buried at the end of a long chain of sub-folders in user's Library/Application Support. The file names are simply the contact's identifier plus an extension. You can save some typing by dragging one of them to your Xcode editor, surrounding with quotes, and maybe removing the last path component. As my app isn't for distribution that is enough for me; otherwise you will have to do some doctoring: the user's name will be in the second path component. I don't know the significance of the long ID number following 'Sources', whether it is user or system specific, but it is the only item in that subfolder, so you should be able to build a viable path programatically.
Upvotes: 0
Reputation: 8395
The addressbook
URL scheme is able to show the person record or edit it:
ABPerson * aPerson = <#assume this exists#>;
// Open the Contacts app, showing the person record.
NSString * urlString = [NSString stringWithFormat:@"addressbook://%@", [aPerson uniqueId]];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:urlString]];
More information is in Address Book Programming Guide.
Upvotes: 1