-
-
Save mzcu/3771916 to your computer and use it in GitHub Desktop.
(* | |
AddCountryCode.scpt | |
===================== | |
Customization of Andreas Amann's script posted on https://discussions.apple.com/message/9169756#9169756 | |
Description | |
------------ | |
Adds RS country code prefix (+381) to all phonebook entries with leading zero (RS local number format): 063123456 -> +38163123456 | |
Back up your Address book before running this script, your mileage may vary!!! | |
Usage | |
------ | |
Paste to AppleScript Editor and click "Run". | |
*) | |
tell application "Contacts" | |
repeat with eachPerson in people | |
repeat with eachNumber in phones of eachPerson | |
set theNum to (get value of eachNumber) | |
if (theNum does not start with "+" and theNum does not start with "381" and theNum starts with "0") then | |
-- log "+381" & (get text 2 thru (length of theNum) of theNum) | |
set value of eachNumber to "+381" & (get text 2 thru (length of theNum) of theNum) | |
end if | |
end repeat | |
end repeat | |
save | |
end tell |
Thank u so much
Legend — much appreciated.
Note that on macos 12.1 saving data in contacts.app is broken
Here's an updated version for USA, Canada, and other North American phone numbers. It checks the format and prepends the number with a "+1" where applicable. It also logs what was processed, which phone numbers were already correct with a country code, and which ones were skipped. The log files are saved to the desktop.
As always, make a backup first and modify the file as needed. Test first before using it to make sure it's applying to your use case correctly.
tell application "Contacts"
repeat with eachPerson in people
set personName to name of eachPerson
repeat with eachNumber in phones of eachPerson
-- Strip parenthesis and dashes
set theNum to the words of (get value of eachNumber) as text
-- Remove periods.
set theNum to do shell script "echo " & quoted form of theNum & " | tr -d '.'"
-- If the number doesn't start with a "+" or a "1" and is 10 digits long, assume it's a USA number.
-- Prepend it with "+1".
if (theNum does not start with "+" and theNum does not start with "1" and length of theNum = 10) then
set value of eachNumber to "+1" & theNum
my logProcessed("+1" & theNum & " " & personName)
-- If the number doesn't start with a "+" and starts with a "1" and is 11 digits long,
-- assume it's a USA number and prepend it with "+".
else if (theNum does not start with "+" and theNum starts with "1" and length of theNum = 11) then
set value of eachNumber to "+" & theNum
my logProcessed("+" & theNum & " " & personName)
-- If the number starts with "+" it's already correctly formatted and doesn't need changing.
-- Log it in "correct" file.
else if (theNum starts with "+") then
my logCorrect(theNum & " " & personName)
-- Skipping this phone number as it doesn't fit in the other categories.
-- Let's log it in the "skipped" file.
else
my logSkipped(theNum & " " & personName)
end if
end repeat
end repeat
save
end tell
-- Add new message to the Processed log
on logProcessed(message)
logToFile(message, "processedPhoneNumbers.txt")
end logProcessed
-- Add new message to the Correct log
on logCorrect(message)
logToFile(message, "correctPhoneNumbers.txt")
end logCorrect
-- Add new message to the Skipped log
on logSkipped(message)
logToFile(message, "skippedPhoneNumbers.txt")
end logSkipped
-- Log a message to a file (file is saved on the desktop).
on logToFile(message, fileName)
set filePath to (path to desktop as string) & fileName -- AppleScript's write command writes plain text
try
set fileRef to open for access file filePath with write permission
write message & return to fileRef as «class utf8» starting at eof
close access fileRef
on error
try
close access file filePath
end try
end try
end logToFile
@bluestarstudios Great update! I made a couple small changes to add a little more information to logging and allow for dry runs: https://gist.github.com/nquinlan/7d4b5716eb0644dac0af3b97dfc7cc54
I'm going to make some updates to handle short-codes. The script stopped with "Can’t get item 2 of every phone of item 12 of every person. Invalid index." number -1719 from item 2 of every phone of item 12 of every person."