0

I've been using AppleScript to create new people in the Address Book/Contacts app on my Mac running macOS Monterey 12.6. I've been storing their IDs (e.g. 778C4D8D-0A22-4918-A5B6-73361FFF8779) and I now need to create a version of the AppleScript that will perform an update of the Contacts app record using their Contacts app ID as the identifier for which person to update.

Here's my AppleScript for creating a new person:

tell application "Contacts"

set thePerson to make new person with properties ¬
    {first name:"Dee", last name:"Velhopa", organization:"Paws On Error"} ¬
        
make new email at end of emails of thePerson with properties ¬
    {label:"Work", value:"[email protected]"}
make new phone at end of phones of thePerson with properties ¬
    {label:"Work", value:"+44 0161 715 7028"}
make new url at end of urls of thePerson with properties ¬
    {label:"Work", value:"http://www.pauseonerror.com/"}
make new address at end of addresses of thePerson with properties ¬
    {label:"Work", street:"MellowBank", city:"Bored End", state:"Bucks", zip:"SL8 5AJ", country:"U.K."} ¬
        
save

end tell

I haven't been able to work out how to modify this so that if I have their ID to simply do an update with the same data but not create a new person.

1 Answer 1

0

I managed to cobble together a solution with a lot of trial and error. Here's the syntax that worked for me:

tell application "Contacts"

    set thePerson to first person whose id = "C5FF98CE-4B8A-4437-AA38-630BA60AA3A4:ABPerson"


    set first name of thePerson to "Joe"
    set last name of thePerson to "Citizen"

    try
        set (label of first email) of thePerson to "Home"
        set (value of first email) of thePerson to "[email protected]"
    on error
        tell thePerson
            make new email at end of emails with properties {label:"Home", value:"[email protected]"}
        end tell
    end try

    try
        set (label of first phone) of thePerson to "Mobile"
        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:"Mobile", value:"(415) 123-3174"}
        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:"Home", street:"321 California St.", city:"San Francisco", state:"CA", zip:"94111"}
        end tell
    end try
    


    save

end tell

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .