-- RUNS ON SELECTED ENTRIES
-- this script adds email addresses for selected AddressBook cards
-- for the SMS gateways associated with the cellphone number provided on the card
-- This script checks for and does not act on duplicates
-- May not work 100% if number was brought from another carrier
-- beginning of bash script command
set command to "curl -AMozilla/4.0 \"http://www.whitepages.com/carrier_lookup?carrier=att&response=1&name_0=&number_0="
-- used for parsing the webpage
set astid to AppleScript's text item delimiters
set justBefore to "fieldset>"
tell application "Address Book"
-- makes a list of all selected entries
set mylist to selection
-- iterates for each person
repeat with this_person in mylist
-- makes list of each phone number for this_person
set phonesList to every phone of this_person
-- iterates for each phone number
repeat with phonenumber in phonesList
-- if the phonenumber is a mobile number extract the number
if label of phonenumber is "mobile" then
-- gets the mobile number
set theNumber to value of phonenumber
-- makes mobile number URI compatable
--set theNumber to do shell script "perl -e 'use URI::Escape; print uri_escape(\"" & theNumber & "\")';"
-- removes spaces - and () and . from phone number
set illegalCharacters to {" ", "-", "(", ")", "."} -- Whatever you want to eliminate.
set replaceWith to ""
repeat with thisChar in illegalCharacters
set AppleScript's text item delimiters to {thisChar}
set theNumber to text items of theNumber
set AppleScript's text item delimiters to {replaceWith}
set theNumber to theNumber as Unicode text
end repeat
theNumber
-- adds the number and ending " to the command
set theCommand to command & theNumber & "\""
-- quotes the command to ensure & is executed fine
set quotedCommand to quoted form of theCommand
-- switches to bash and gets the source of the page as T
set T to (do shell script "/bin/bash -c " & quotedCommand)
-- begin parsing
set AppleScript's text item delimiters to justBefore
set lastPart to text item 4 of T
if lastPart contains "No Results Found" or lastPart contains "Invalid Mobile Number" then exit repeat
set AppleScript's text item delimiters to "("
set X to text item 2 of lastPart
set AppleScript's text item delimiters to ")"
set carrier to text item 1 of X
log carrier
-- reset the delimiter
set AppleScript's text item delimiters to astid
-- creates the email address for the phone number
set theEmail to ""
set theSuffix to ""
if carrier is "Verizon" then
set theEmail to theNumber & "@vtext.com"
set theSuffix to "vzn"
end if
if carrier is "AT&T/Cingular" or carrier is "AT&T/Cingular" then
set theEmail to theNumber & "@txt.att.net"
set theSuffix to "att"
end if
if carrier is "T-Mobile" then
set theEmail to theNumber & "@tmomail.net"
set theSuffix to "tmo"
end if
if carrier is "Cellular One" then
set theEmail to theNumber & "@mobile.celloneusa.com"
set theSuffix to "cell1"
end if
if carrier is "Alltel Wireless" then
set theEmail to theNumber & "@message.alltel.com"
set theSuffix to "altl"
end if
if carrier is "Sprint PCS" then
set theEmail to theNumber & "@messaging.sprintpcs.com"
set theSuffix to "sprnt"
end if
(*
set oldSuffix to (suffix of this_person)
if (oldSuffix is "") or (oldSuffix is "missing value") then
set suffix of this_person to theSuffix
else
set suffix of this_person to theSuffix & ", " & oldSuffix
end if
*)
-- add name of carrier to phone number label so we can see it when being called
if theSuffix is not "" then set label of phonenumber to label of phonenumber & " " & theSuffix
-- is there a dupe or no email found?
set dupe to false
-- if no email address found
if theEmail is "" then
set dupe to true
end if
-- checks all email addresses of the person for duplicates
set emailList to every email of this_person
repeat with existingMail in emailList
if value of existingMail is equal to theEmail then
set dupe to true
end if
end repeat
-- if there are no duplicates, add the new email address
if dupe is false then
make new email at end of emails of this_person with properties {label:"mobile", value:theEmail}
end if
end if
end repeat
end repeat
save addressbook
end tell