I have a database that is using the script below to update one person’s info in the Contacts.app, or add it if there’s no such person. It loads names and addresses from the database into the script, then runs it. I’m sure it’s not elegantly constructed but it does work with two minor exceptions.
If Contacts is not already open, it fails without the Activate. No big deal, but it would be nice if it could run without even opening Contacts, as scripts can do for Calendar. Why is Contacts different?
Even when everything runs properly and the script is done, it returns “missing value”. I can’t figure out what value is missing. When debugging, I’ve cut it back to almost nothing and it still gives that result.
My biggest problem though, is in reversing the process. I want to retrieve the same data in order to update the database. Sometimes there may be a couple of phones, sometimes not. I’m getting the name and organization, but so far can’t find the format to get the phone numbers and email. If anyone can just give me a lead on how to put that together and combine it all in any kind of string, I’d be grateful for the help.
tell application "Contacts"
activate
try
set thePerson to first person whose (first name is "Joe" and last name is "Shmoe")
on error
set thePerson to make new person with properties {first name:"Joe", last name:"Shmoe"}
end try
try
set organization of thePerson to "Acme Company"
on error
tell thePerson
make new organization at end of organizations with properties {organization:"Big Co"}
end tell
end try
try
set (street of first address) of thePerson to "123 New Street"
set (city of first address) of thePerson to "San Francisco"
set (state of first address) of thePerson to "CA"
set (zip of first address) of thePerson to "94111"
on error
tell thePerson
make new address at end of addresses with properties {label:"Address", street:"321 California St.", city:"San Francisco", state:"CA", zip:"94111"}
end tell
end try
try
set (label of first email) of thePerson to "direct"
set (value of first email) of thePerson to "Joe@shmoe.com"
on error
tell thePerson
make new email at end of emails with properties {label:"persona", value:"joe@xyz.com"}
end tell
end try
try
set (label of second email) of thePerson to "gmail"
set (value of second email) of thePerson to "Joe@gmail.com"
on error
tell thePerson
make new email at end of emails with properties {label:"gmail", value:"gmail.com"}
end tell
end try
try
set (label of first url) of thePerson to "website"
set (value of first url) of thePerson to "big.com"
on error
tell thePerson
make new url at end of urls with properties {label:"website", value:"big.com"}
end tell
end try
try
set (label of first phone) of thePerson to "Work"
set (value of first phone) of thePerson to "(415) 123-2121"
on error
tell thePerson
make new phone at end of phones with properties {label:"Work", value:"(415) 123-3174"}
end tell
end try
try
set (label of first phone) of thePerson to "Work"
set (value of first phone) of thePerson to "(415) 123-4356"
on error
tell thePerson
make new phone at end of phones with properties {label:"Work", value:"(415) 123-3174"}
end tell
end try
try
set (label of second phone) of thePerson to "home"
set (value of second phone) of thePerson to "(666) 123-3221"
on error
tell thePerson
make new phone at end of phones with properties {label:"home", value:"(415) 123-3221"}
end tell
end try
try
set (label of third phone) of thePerson to "cell"
set (value of third phone) of thePerson to "(212) 123-3221"
on error
tell thePerson
make new phone at end of phones with properties {label:"cell", value:"(212) 123-3221"}
end tell
end try
save
end tell
Retreiving info:
tell application "Contacts"
activate
set theContact to first person whose (first name is "Joe" and last name is "Shmoe")
set selection to theContact
set theName to name of theContact
set theOrg to organization of theContact
set theLabel1 to (label of first phone) of theContact
set thePhone1 to (value of first phone) of theContact
set theEmail to (value of first email) of theContact
-- these all get nothing
end tell
return theName & "|" & theOrg
Model: MacPro 2009
AppleScript: 2.8.1
Browser: Safari 601.3.9
Operating System: Mac OS X (10.10)