Skip to content

Instantly share code, notes, and snippets.

@dinhoabreu
Created April 2, 2021 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinhoabreu/e46ea30b26e70bfc8498b3174e880a80 to your computer and use it in GitHub Desktop.
Save dinhoabreu/e46ea30b26e70bfc8498b3174e880a80 to your computer and use it in GitHub Desktop.
macos - contacts - update phone values to international format
function normalizePhoneValues(ab, country, ddd, save) {
ab.people().forEach((person) => {
const name = person.name()
person.phones().forEach((phone) => {
const oldValue = phone.value()
if (!oldValue.startsWith('+')) {
let newValue = oldValue.replace(/\D/g, '')
if (newValue.startsWith('0')) {
newValue = newValue.substr(1)
}
if ([8, 9].includes(newValue.length)) {
newValue = ddd + newValue
}
newValue = country + newValue
if ([13, 14].includes(newValue.length)) {
if (save) {
phone.value = newValue
}
console.log(`${name}: ${oldValue} -> ${newValue}`)
}
}
})
})
}
function run(argv) {
const ab = Application('Contacts')
const country = argv.find((p) => /^\+\d+/.test(p)) || '+55'
const ddd = argv.find((p) => /^\d\d$/.test(p)) || '11'
const save = argv.indexOf('save') !== -1
normalizePhoneValues(ab, country, ddd, save)
if (save) {
console.log('saving')
ab.save()
console.log('saved')
}
}
@dinhoabreu
Copy link
Author

# dry-run to check changes
osascript -l JavaScript macos-contacts-phones-normalize.js

# save changes
osascript -l JavaScript macos-contacts-phones-normalize.js save

# change country id and local id
osascript -l JavaScript macos-contacts-phones-normalize.js +44 12

@rcguilherme maybe you enjoy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment