Applescript – Contacts: Why can I get addresses and emails from a contact but not phones

applescriptcontacts.app

Errors when getting phones

In Script Editor, if I run:

tell application "Contacts" to get the vcard of person 301

the result is:

"BEGIN:VCARD VERSION:3.0 PRODID:-//Apple Inc.//Mac OS X 10.13.6//EN
N:Bar;Foo;;; FN:Foo Bar
EMAIL;type=INTERNET;type=WORK;type=pref:foobar@gmail.com
TEL;type=HOME;type=VOICE;type=pref:555 555 1212
CATEGORIES:Address Book
UID:1e6e6e410a662666
X-ABUID:18B18941-E2B7-48CE-BB34-4A26CC0E23BB:ABPerson
END:VCARD
"

Note that this person has an email address and a phone number. If I run:

tell application "Contacts" to get the value of emails of person 301

the result is:

{"foobar@gmail.com"}

However if I run:

tell application "Contacts" to get the value of phones of person 301

I get

error "Contacts got an error: Can’t get value of every phone of person 301." number -1728 from value of every phone of person 301

and if I run

tell application "Contacts" to get the phones of person 301

error "Contacts got an error: AppleEvent handler failed." number -10000

What happens if the person doesn't have a phone number?

Edit: Based on one of the proposed answers to this post, I tried

tell application "Contacts"
    set thePhoneNumber to value of phones of people
end tell

which gave me something like

{missing value, {}, missing value, missing value, …}

with a lot more "missing values" and "{}s" because I have about 2000 entries in the database.

Investigating this, I found that the "missing value" appears for people who have phone numbers, and the "{}" appears for people who do not have phone numbers. If I run the same command with "emails", I get a list of email addresses for each person who has emails, and an empty list "{}" for people who don't.


This is on Mac OS 10.13.6, Contacts 11.0 (1808.8).

Have I discovered a bug or is there something wrong with my script?

Best Answer

This should work for you...

tell application "Contacts"
    launch
    delay 1
    tell its person 301
        try
            set thePhoneNumber to value of phones
        end try
    end tell
end tell

This also works for me...

tell application "Contacts"
    set thePhoneNumber to value of phones of its person 301
end tell

You can also try running this following code

tell application "Contacts"
    set thePhoneNumber to value of phones of people
end tell

Then you will go to item 301 of that result and if there is no phone number... That may be why you are getting the error.