Skip to content

Instantly share code, notes, and snippets.

@SamirTalwar
Last active October 1, 2019 10:43
Show Gist options
  • Save SamirTalwar/481da41d0f54004aeb138ddadb3a1853 to your computer and use it in GitHub Desktop.
Save SamirTalwar/481da41d0f54004aeb138ddadb3a1853 to your computer and use it in GitHub Desktop.
JavaScript to add country codes to all phone numbers without them, for the Mac Script Editor.
{
const countryCode = '+44'
const addCountryCodeToPhoneNumbers = person => {
for (const phone of person.phones()) {
if (phone.value().startsWith('0')) {
const newValue = phone.value().replace(/^0/, countryCode)
console.log(`${person.name()}: ${phone.value()} → ${newValue}`)
phone.value.set(newValue)
}
}
}
const contacts = Application("Contacts")
for (const person of contacts.people()) {
addCountryCodeToPhoneNumbers(person)
}
contacts.save()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment