Last active
October 24, 2015 23:42
-
-
Save rjames86/fbba26662c95a558e8af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
References: | |
http://www.macdevcenter.com/pub/a/mac/2002/08/27/cocoa.html?page=2 | |
https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/AddressBook/Tasks/AccessingData.html#//apple_ref/doc/uid/20001023-BABHHIHC | |
*/ | |
ObjC.import("AddressBook"); | |
sAB = $.ABAddressBook.sharedAddressBook | |
meRecord = sAB.me | |
var propertyToObjCType = { | |
'email': $.kABEmailProperty, | |
'address': $.kABAddressProperty, | |
'phone': $.kABPhoneProperty | |
} | |
var labelToObjCType = { | |
'work': $.kABWorkLabel, | |
'home': $.kABHomeLabel, | |
'iPhone': $.kABPhoneiPhoneLabel, | |
} | |
function valueForProperty(property){ | |
return meRecord.valueForProperty(propertyToObjCType[property]) | |
} | |
function getEmailByLabel(inputLabel){ | |
emails = valueForProperty('email') | |
label = labelToObjCType[inputLabel] | |
for (var i = 0; i < emails.count; i++){ | |
if ($.CFEqual(emails.labelAtIndex(i), label)){ | |
return emails.valueAtIndex(i) | |
} | |
} | |
} | |
function getAddressByLabel(inputLabel){ | |
addresses = valueForProperty('address') | |
label = labelToObjCType[inputLabel] | |
for (var i = 0; i < addresses.count; i++){ | |
if ($.CFEqual(addresses.labelAtIndex(i), label)){ | |
return sAB.formattedAddressFromDictionary(addresses.valueAtIndex(i)).string | |
} | |
} | |
} | |
function getPhoneByLabel(inputLabel){ | |
phone = valueForProperty('phone') | |
label = labelToObjCType[inputLabel] | |
for (var i = 0; i < phone.count; i++){ | |
if ($.CFEqual(phone.labelAtIndex(i), label)){ | |
return phone.valueAtIndex(i) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment