Created
April 2, 2021 22:57
-
-
Save dinhoabreu/e46ea30b26e70bfc8498b3174e880a80 to your computer and use it in GitHub Desktop.
macos - contacts - update phone values to international format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rcguilherme maybe you enjoy.