Use JavaScript to Automate stuff on macOS

Hiraash Thawfeek
3 min readOct 26, 2019

When you travel out of your resident country there are things that you miss quite often. One of those things that most people end up missing is the caller-id when you receive calls from your resident country. This happens mostly because people tend to save phone numbers without the country code for local numbers. This bites you back big-time when you are traveling. I recently moved to Singapore from Sri Lanka and it has been a blast, except when I get calls or WhatsApp messages.

So I wanted to convert all my Sri Lankan contact numbers that did not have a country code to have one. Usually I would use Automator on my mac to get this sort of little tasks done. But this time it was too much of a stretch for Automator to include all my nitty-gritty requirements to get the job done. Enter JXA (JavaScript for Automation), something that Apple included in Yosemite as an alternate to AppleScript.

I wanted to be very careful in doing this because I did’t want my few thousand contacts to get f#cked up over some bad code written in a hurry. So I chose to

  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.

And the script looks as follows.

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

To get it running:

  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.

Script Editor and Automator very useful tools available in macOS that lets you work with your OS and Apps to automate stuff. You can get stuff done relatively easy. However figuring out the scripting APIs can be a bit tricky. The Script Editor does provide a listing of all the available API’s through the File > Open Directory. But the documentation is not very descriptive. It’s still a good place to start. On JXA there is very little help available online. These are the few useful resource I found so far. Happy Automation!!

Apple Guide —

JXA Cookbook —

Error Codes —