Home > Software design >  Select from Contacts a List of People Based on Email Label Using AppleScript
Select from Contacts a List of People Based on Email Label Using AppleScript

Time:06-04

Using AppleScript, how do I get a list of all people in the Contacts application who have at least one email address with a particular label?

I tried:

set peopleToCheck to every person where ((label of its email contains strContactEmailLabelHome))

Unfortunately, it returns an empty set. This can’t be correct because

set peopleToCheck to every person where ((label of its first email contains strContactEmailLabelHome))

Returns a set of one person.

CodePudding user response:

property text item delimiters : linefeed

tell application id "com.apple.AddressBook"
    -- Get the label of every email of every person
    get label of people's emails as text
    set labels to the result's text items
    
    -- Remove duplicate items from the list of email labels
    tell (a reference to the labels)
        repeat length times
            if string 1 = "" then set string 1 to 0
            if string 1 is not in rest of strings then set end to string 1
            set string 1 to 0
        end repeat
        set contents to strings
    end tell
    
    labels --> List of unique labels currently in use by at least one contact
    
    -- Choose a label to match contacts against
    set [specificLabel] to choose from list of labels ¬
        with title ["Search Contacts by Email Label"] ¬
        with prompt ["Select one of your email labels:"] ¬
        OK button name ["Search"] cancel button name ["Close"] ¬
        without multiple selections allowed and empty selection allowed
    
    -- Matches
    set P to a reference to (every person ¬
        whose emails's label contains ¬
        the specificLabel)
    
    
    -- Select the matching contacts in the application
    set selection to P
    
    return name of P
end tell

CodePudding user response:

set customLabel to "customLabel"

tell application "Contacts"
    return people whose label of emails contains customLabel
end tell

Or, using variable for the list:

set customLabel to "customLabel"

tell application "Contacts"
    set theList to people whose label of emails contains customLabel
end tell

return theList
  • Related