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)
[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
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
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