Skip to content

Instantly share code, notes, and snippets.

@nquinlan
Forked from mzcu/AddCountryCode.scpt
Last active November 14, 2023 18:31
Show Gist options
  • Save nquinlan/7d4b5716eb0644dac0af3b97dfc7cc54 to your computer and use it in GitHub Desktop.
Save nquinlan/7d4b5716eb0644dac0af3b97dfc7cc54 to your computer and use it in GitHub Desktop.
Add country code to OS X Address book entries
(*
AddCountryCode.scpt
=====================
Customized version of @bluestarstuido's script posted on https://gist.github.com/mzcu/3771916?permalink_comment_id=4508047#gistcomment-4508047
Forked from @mzcu's script https://gist.github.com/mzcu/3771916
Originally built from Andreas Amann's script posted on https://discussions.apple.com/message/9169756#9169756
Description
------------
Adds a US/Canada country code prefix (+1) to all phonebook entries that seem to match the US Format.
Back up your Address book before running this script, your mileage may vary!!!
Usage
------
Paste to AppleScript Editor and click "Run" to see the what the script would do.
To update your address book change the following line:
set dryRun to true
To:
set dryRun to false
*)
-- By default this script does not make any changes. It's reccomended you verify that it does what you want before proceeding.
-- You can confirm by reading the files on your desktop.
-- Change true to false, on the line below to update your contacts.
set dryRun to true
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
set oldNum to theNum
-- 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
if not dryRun then
set value of eachNumber to "+1" & theNum
end if
my logProcessed("+1" & theNum & " [" & oldNum & "] " & 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
if not dryRun then
set value of eachNumber to "+" & theNum
end if
my logProcessed("+" & theNum & " [" & oldNum & "] " & 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 & " [" & oldNum & "] " & 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 & " [" & oldNum & "] " & personName)
end if
end repeat
end repeat
if not dryRun then
save
end if
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment