Use JavaScript to Automate stuff on macOS

  1. Filter out only the mobile numbers, which can be identified by their first two digits (71, 72, 77, 76 etc.)
  2. Remove the preceding zero since that is no longer valid with the country code. This is mostly a Sri Lankan thing. Eg: 077####### -> +9477#######.
  3. Remove the braces that iPhone contacts adds automatically to make sure there is not mishap.
var contacts = Application("Contacts");
var people = contacts.selection();
console.log("Processing: " + people.length + " Contacts")people.forEach( person => {
let phones = person.phones();
phones.forEach( phone =>
let num = phone.value();
let n = num.replace("(","").replace(")","");
if( n[0] === " " ) n = n.substr(1)
if( n[0] === "0" ) n = n.substr(1)
if( n.startsWith("77") ||
n.startsWith("72") ||
n.startsWith("76") ||
n.startsWith("71")) {

phone.value = "+94" + n;
console.log(person.firstName() + " " + phone.value())
  1. Open the Scrip Editor on your Mac and paste it in new script window.
  2. Make sure to switch the script language from AppleScript to JavaScript.
  3. Make any changes as you see fit for your requirement.
  4. Open your Contacts app and select all the contacts you want to your script to search and change. Try it on a few first (2–3) to ensure any changes you made work as expected.
  5. Switch back to the Script Editor.
  6. Hit the play button to run the script. Switch to the Messages tab on the bottom section to see the progress.



Coder, Creator of things, Entrepreneur

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store