MacOS – How to change Contacts.app custom labels automatically using AppleScript

applescriptcontactsmacos

Is there a way to change the current labels in Contacts.app to custom ones using AppleScript based on a specific criteria?

Here is what I mean:

I need the script to go through my contacts to change the phone numbers default labels such as mobile, home, work, etc. to specific labels based on the first 3 digits of the phone number. If the label has been changed already or the criteria does not apply, then the script should skip that phone #.

Here is an example:

  • If the phone # starts with 079, then change the label to "zain"
  • If the phone # starts with 078, then change the label to "umniah"

Is this even possible?

Best Answer

Here is an example (quick scribble) of how to get and process the labels.

tell application "Contacts"
    set theNumber to "079"

    set (label of phones of people whose value starts with theNumber) to "test"
    save


end tell

Set theNumber to your own number.

AND I WOULD BACKUP THE ADDRESSBOOK BEFORE YOU START PLAYING WITH IT.

Update.

I have change the script to chaco if the phone number starts with rather than contains.


'label' = an items label. i.e 'mobile','iphone','home page'

Phones = the phone numbers of a person/contact's entry

people = plural for more than one person/contact.

value = the value of an item in a person/contact entry - in this case the phone number

In the set command the code between the brackets ( ) is run first.

Meaning the reference to phone number's label in whole of the address book that starts with theNumber is gathered in a list.

Then the surrounding code tells each reference to change to test.