• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Creating a list of numbers from a Contacts group
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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 13:22:39 +0000

In my message of Fri, 6 Jan 2017 10:43:05 +0000, I wrote:

>But Yvan's just informed me off-list that both of the scripts I posted
>last night are errorring when they try to get the group's contacts. From
>the error message he getting, it looks as if the group's not being
>identified on his machine. I'm looking into that at the moment.

OK. I see that Yvan's already posted the cause of that particular
problem.

For the Contacts framework script, I've decided to go with his
valueForKeyPath:("@unionOfArrays.self") idea, as it not only removes
empty arrays but flattens the containing array, from which the final
text can then be derived without having to use AppleScript's text item
delimiters. It also seems that this keypath can be tagged onto the
keypath which fetches the items to which it's applied, reducing the
amount of code still further.

The phone number array in the AddressBook script is already flat, so of
course the final text could be derived directly from that too without
using AppleScript TIDs. Here are fresh versions of both scripts.

Contacts framework:

  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, flatten the array, and derive a text result.
  set phoneNumbers to ((groupMembers's valueForKeyPath:("email@hidden"))'s componentsJoinedByString:(linefeed)) as text


AddressBook framework:

  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
  -- Derive the final text from the phone number array.
  set phoneNumbers to (phoneNumbers's componentsJoinedByString:(linefeed)) as text


An interesting thing about the AddressBook version is that it returns
the numbers in a different order each time it's run, since it gets the
contacts in a different order every time. This won't matter if the
point's just to get the numbers without caring to whom they belong.

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


References: 
 >Re: Creating a list of numbers from a Contacts group (From: Nigel Garvey <email@hidden>)
 >Re: Creating a list of numbers from a Contacts group (From: Nigel Garvey <email@hidden>)

  • Prev by Date: Re: Creating a list of numbers from a Contacts group
  • Next by Date: Re: Creating a list of numbers from a Contacts group
  • Previous by thread: Re: Creating a list of numbers from a Contacts group
  • Next by thread: Re: Creating a list of numbers from a Contacts group
  • Index(es):
    • Date
    • Thread