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: Fri, 06 Jan 2017 00:49:39 +0000
In my message of Thu, 5 Jan 2017 22:13:04 +0000, I wrote:
>It's actually possible to get the phone numbers in ASObjC without using
>the Contacts application at all:
[snip]
>In Mavericks and Yosemite, you have to use the AddressBook framework,
>which I think involves nested repeats.
Something like this:
use AppleScript version "2.4" -- Mac OS 10.10 (Yosemite) or later.
use framework "Foundation"
use framework "AddressBook" -- The documentation recommends "Contacts" for 10.11 or later.
set groupName to "Family and Friends"
set addressBook to current application's class "ABAddressBook"'s addressBook()
-- Get all the groups and filter the array for the one with the specified name.
set allGroups to addressBook's groups()
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 groupMembers to namedGroup's members()
-- Parse their phones (if they have any) for the numbers.
set phoneNumbers to current application's class "NSMutableArray"'s new()
repeat with thisPerson in groupMembers
set thesePhones to (thisPerson's valueForProperty:(current application's kABPhoneProperty))
if (thesePhones is not missing value) then
-- thesePhones is an ABMultivalue. Cycle through its 'values'.
repeat with i from 0 to (thesePhones's |count|()) - 1
tell phoneNumbers to addObject:(thesePhones's valueAtIndex:(i))
end repeat
end if
end repeat
set phoneNumbers to phoneNumbers 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
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