Phone Number in Contacts

Struggling… can’t get a phone number out of a contact:


tell application "Contacts"
	set selPerson to selection
	
	repeat with thisperson in selPerson
		set PersonFName to first name of thisperson -- this line works
		
		display dialog PersonFName
		
		set PersonPhone to first phone of thisperson -- this line does not work
		
		display dialog PersonPhone
		
	end repeat
end tell

Anybody with insight, I’d sure appreciate it. Thanks.

Model: 15" MBP Retina
AppleScript: 2.4
Browser: Safari 601.3.9
Operating System: Mac OS X (10.10)

Check the Dictionary:

[format]phone‚n [inh. contact info] : Phone number for a person.
elements
contained by people.
inherited from contact info
properties
label (text or missing value) : Label is the label associated with value like “work”, “home”, etc.
value (text, date, or missing value) : Value.
id (text, r/o) : unique identifier for this entry, this is persistent, and stays with the record.[/format]

You need value to get it:


tell application "Contacts"
	set selPerson to selection
	
	repeat with thisperson in selPerson
		set PersonFName to first name of thisperson -- this line works
		
		display dialog PersonFName
		
		set PersonPhone to value of first phone of thisperson -- this line does not work
		
		display dialog PersonPhone
		
	end repeat
end tell

Thanks tons for looking at this Craig - I’m guess I’m getting hung up on the syntax -

but when I try

get properties of thisperson

“phone” is nowhere to be found. I run this:


tell application "Contacts"
	set selPerson to selection
	
	repeat with thisperson in selPerson
		
		get properties of thisperson
		
	end repeat
end tell

I get this:


tell application "Contacts"
	get selection
	get properties of person id "97F4F494-2949-4B33-9A73-69CC6B84C24A:ABPerson"
end tell
Result:
{last name:"Sample", suffix:missing value, title:missing value, company:false, first name:"Joe", image:missing value, nickname:missing value, organization:"Sample Co Name", maiden name:missing value, name:"Joe Sample", home page:missing value, job title:missing value, modification date:date "Thursday, January 7, 2016 at 8:36:33 AM", phonetic middle name:missing value, department:missing value, class:person, middle name:missing value, birth date:missing value, phonetic last name:missing value, id:"97F4F494-2949-4B33-9A73-69CC6B84C24A:ABPerson", vcard:"BEGIN:VCARD
VERSION:3.0
PRODID:-//Apple Inc.//Mac OS X 10.10.5//EN
N:Sample;Joe;;;
FN:Joe Sample
ORG:Sample Co Name;
item1.TEL;type=pref:8884445555
item1.X-ABLabel:office
ADR;type=HOME;type=pref:;;123 Any Street;AnyTown;NY;10003;
CATEGORIES:card
UID:97f4f494-2949-4b33-9a73-69cc6b84c24a
X-ABUID:97F4F494-2949-4B33-9A73-69CC6B84C24A:ABPerson
END:VCARD
", note:missing value, selected:true, creation date:date "Thursday, January 7, 2016 at 8:35:59 AM", phonetic first name:missing value}

The only place the phone appears in the result is in the Vcard. Is this a bug with applescript in the Contacts.app?

Hi. It’s not a bug, as the feature works correctly and is properly documented in the dictionary. Your last question shows that you want a property, but phone is an element. Craig’s edit should work, as written.

Thanks Marc. Craig’s edit doesn’t work for me as written:

set PersonPhone to value of first phone of thisperson

I’ve tried every example of different ways to say it that I can find.


tell application "Contacts"
	set selPerson to selection
	
	repeat with thisperson in selPerson
		
-- None of these work for me:

		set thePhoneValue to the value of phone of thisperson
		--set thePhoneValue to the value of phone 1 of thisperson
		--set thePhoneValue to the value of first phone of thisperson
		--set thePhoneValue to (value of first phone of thisperson whose label = "office")
		--set thePhoneValue to the value of item 1 of phone of thisperson
		
		display dialog thePhoneValue
		
	end repeat
end tell

Would you give me a hint as to what I’m missing here?

The script that I posted initially works perfectly on both of my machines (2014 Mac Pro, 2012 Mac Mini), both of which are running the latest versions of OS X (10.11.2). This should not make a difference, but we may as well thoroughly investigate it.

I do not believe the code has changed much over the years for Contacts/Address Book, but I will look through my older scripts if you are working with older versions.

See if this works, and then examine the results for clues:

tell application "Contacts"
	get properties of every phone of first person
end tell

My results:

{{label:“mobile”, value:“13176031427”, class:phone, id:“65439182-E867-4102-B533-DE6D81483771”}}

Thanks again, it must be a problem with my particular environment.

Here’s what I get running that:

 tell application "Contacts"
	get properties of every phone of person 1
		--> error number -10000

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

I’m on a MBP 15" Retina Late 2013, 2.6 GHz Intel Core i7 running Yosemite 10.10.5

Applescript 2.4, Script Editor Version 2.7 (176)

Contacts.app Version 9.0 (1579)

I can’t upgrade to El Capitan just yet, until Adobe gets it’s stuff running reliably

Understood. My wife’s Mac Air is still running Yosemite. I will play with her machine when I get home tonight. This is maddening.

My wife’s Macbook Air is running Yosemite 10.10, and all these scripts work perfectly.

Unlucky for you. Have you found any other oddities with your environment? You may need to do a re-installation of the OS.

I ran :

tell application "Contacts" -- 8.0
   get properties of every phone of person 1
end tell

under 10.9.5 with no problem.

I compared the dictionaries of Contacts 8.0 and 9.0.
My old eyes saw no difference between their descriptions of person and phone.

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) vendredi 8 janvier 2016 17:40:51

Just to chime in, I’m on 10.10.3 and Contacts Version 9.0 (1579), and

tell application "Contacts" -- 8.0
get properties of every phone of person 1
end tell

works fine for me, too. Sounds like an environment-specific bug.

Thanks to all, I agree that this problem is particular to this machine.

Now to figure that part out…