Re: Creating a list of numbers from a Contacts group
Re: Creating a list of numbers from a Contacts group
- Subject: Re: Creating a list of numbers from a Contacts group
- From: Nigel Garvey <email@hidden>
- Date: Thu, 05 Jan 2017 22:13:04 +0000
Yvan KOENIG wrote on Thu, 05 Jan 2017 21:40:47 +0100:
>ASObjC offers this kind of feature.
It's actually possible to get the phone numbers in ASObjC without using
the Contacts application at all:
use AppleScript version "2.5" -- Mac OS 10.11 (El Capitan) or later.
use framework "Foundation"
use framework "Contacts"
set groupName to "Family and Friends"
set contactStore to current application's class "CNContactStore"'s new()
-- There's no CNGroup predicate for matching groups by name, so get them all.
set allGroups to contactStore's groupsMatchingPredicate:(missing value) |error|:(missing value)
-- Get the named group from the resulting array.
set predicateForNamedGroups to current application's class "NSPredicate"'s predicateWithFormat:("name = %@") argumentArray:({groupName})
set namedGroup to (allGroups's filteredArrayUsingPredicate:(predicateForNamedGroups))'s firstObject()
-- Get the contacts in that group.
set predicateForContactsInNamedGroup to (current application's class "CNContact"'s predicateForContactsInGroupWithIdentifier:(namedGroup's identifier()))
set groupMembers to (contactStore's unifiedContactsMatchingPredicate:(predicateForContactsInNamedGroup) keysToFetch:({current application's CNContactPhoneNumbersKey}) |error|:(missing value))
-- Get their phone numbers (an array of arrays).
set phoneNumbers to groupMembers's valueForKeyPath:("phoneNumbers.value.stringValue")
-- Lose any empty arrays and coerce to list.
set phoneNumbers to (phoneNumbers's arrayByRemovingObject:({})) as list
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
set phoneNumbers to phoneNumbers as text
set AppleScript's text item delimiters to astid
phoneNumbers
In Mavericks and Yosemite, you have to use the AddressBook framework,
which I think involves nested repeats.
NG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden