Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Add mobile carrier and SMS email to Address Book entries Phones
Since the AT&T rates are so high for data, I have the smallest/cheapest plan available for texts and minutes, so I often get close to my limit at the end of the month. I therefore like to send my SMSs via the unlimited data email (###@vtext.com), and only make/answer calls to those that are in network when I'm running out of minutes.

Instead of going through each Address Book entry and adding emails to them manually after looking up their carrier through a service (such as this one at whitepages.com), I adopted ViViDBoarder's excellent script to add the SMS emails, updating the code to work better with the current version of whitepages.com, and adding a little suffix representing the carrier to the mobile phone label so it says mobile att.

I wanted something I could see easily on the phone when it rings, so I shortened the carrier names. This works on the iPhone, and should work on other phones you might sync with your Address Book if they show you the phone number label when a call comes in. An alternative is to have it set the actual person's name or suffix to the carrier label instead. You just have to make sure the person's name isn't so long that the suffix falls off the end of the screen.

Copy this into Script Editor, select the contacts in Address Book you want to update, run it, sit back, and let it go! Sync your phone with your computer and you should be good to go, saving money despite the carriers not telling you who is in network and therefore a free call!

-- 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
This script plodded through my 2500 contacts in about 20 minutes, so it isn't super fast. However, it does the job and you should only need to do this once for your entire Address Book and on new entries. (Hmmm ... what if I set up a Finder Folder Actions script to act on ~/Library/Address Book files ... next week, maybe!).

[robg adds: I haven't tested this one.]
    •    
  • Currently 1.80 / 5
  You rated: 2 / 5 (15 votes cast)
 
[10,063 views]  

Add mobile carrier and SMS email to Address Book entries | 4 comments | Create New Account
Click here to return to the 'Add mobile carrier and SMS email to Address Book entries' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Add mobile carrier and SMS email to Address Book entries
Authored by: rycardo on Jul 20, '09 02:00:57PM
Excellent script!

After running this script, if you would like to be able to add the SMS email
address to new and/or individual contacts. I modified a script I have to do this as an Address Book Plug-In.

Copy the code below and Paste in to Script Editor and save to either:
/Library/Address Book Plug-Ins or
/Users/your user name/Library/Address Book Plug-Ins.

You'll need to Quit and re-Open Address Book, but then when you click on the mobile label of a phone number, you will see "Create SMS Email Address".

There a few properties at the top of the script which should make it easy to modify for other carriers, countries, or web sites.

Rycardo

NB It's a long script, I'm not sure how to make it display in a small box with scroll bars.

-- This is the text that shows in the menu when you click on 'mobile' next to the phone number
property menuTitle : "Create SMS/MMS Email Address"

-- These are used in the curl shell to get the information
property shellCurl : "curl -A Mozilla/4.0 -G "
property shellURL : " http://www.whitepages.com/carrier_lookup?"
property shellDataBegin : "-d carrier=att -d number_0="
property shellDataEnd : " -d response=1"

-- These are used to parse out the web result
property textBefore1 : "class=\"carrier_result"
property textBefore2 : "("
property textAfter : ")"

-- These are the TelCos, the name that appears in the web result, and the domain used for email.
property mobileTelCos : {{"Verizon", "@vtext.com"}, {"AT&T/Cingular", "@txt.att.net"}, {"T-Mobile", "@tmomail.net"}, {"Cellular One", "@mobile.celloneusa.com"}, {"Alltel Wireless", "@message.alltel.com"}, {"Sprint PCS", "@messaging.sprintpcs.com"}}

using terms from application "Address Book"
	on action property
		return "phone"
	end action property
	
	on action title for the_person with the_field
		return menuTitle
	end action title
	
	on should enable action for the_person with the_field
		return true
	end should enable action
	
	on perform action for the_person with the_field
		set theValue to ((value of the_field) as string)
		set theLabel to ((label of the_field) as string)
		set theName to ((name of the_person) as string)
		
		if theLabel is equal to "mobile" then
			set mobileNumber to my getmobilenumber(theValue)
			set mobileTelCo to my lookupMobileTelCo(mobileNumber)
			set emailAddress to my getSMSEmail(mobileTelCo, mobileNumber)
			set theResult to my addEmailAddress(the_person, emailAddress, mobileTelCo)
			
			save addressbook
			return true
		else
			display alert "You are attempting to look up the email address for " & theName & "'s " & theLabel & " phone " & theValue & "." & return & return & "This script only works for mobile phones."
			return false
		end if
		
	end perform action
end using terms from

on getmobilenumber(phonenumber)
	tell application "Address Book"
		set onlyNumbers to "0123456789"
		set theNumber to ("" as string)
		repeat with i from 1 to (length of phonenumber)
			set theChar to ((text i thru i of phonenumber) as string)
			if onlyNumbers contains theChar then
				set theNumber to theNumber & (theChar as string)
			end if
		end repeat
	end tell
	return theNumber
end getmobilenumber

on addEmailAddress(thePerson, emailAddress, emailLabel)
	tell application "Address Book"
		try
			tell thePerson
				-- is there a dupe or no email found?
				set dupe to false
				
				-- if no email address found
				if emailAddress 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 emailAddress 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 with properties {label:"mobile", value:emailAddress}
				else
					display alert ((name of thePerson) as string) & " already has " & emailAddress & " as a contact Email."
				end if
				
			end tell
			
		on error eM number eN
			display dialog eM with title eN
		end try
	end tell
end addEmailAddress

on lookupMobileTelCo(mobileNumber)
	try
		tell application "Address Book"
			set theShell to shellCurl & shellDataBegin & mobileNumber & shellDataEnd & shellURL
			
			set theResult to do shell script theShell
			
			set theMark to (the offset of textBefore1 in theResult)
			if theMark > 0 then
				set theResult to ((text theMark thru (the length of theResult) of theResult) as string)
				
				set theMark to (the offset of textBefore2 in theResult)
				if theMark > 0 then
					set theResult to ((text (theMark + 1) thru (the length of theResult) of theResult) as string)
					set theMark to (the offset of textAfter in theResult)
					
					if theMark > 0 then
						set theResult to ((text 1 thru (theMark - 1) of theResult) as string)
					else
						set theResult to ""
					end if
				else
					set theResult to ""
				end if
			else
				set theResult to ""
			end if
		end tell
	on error eM number eN
		display dialog eM with title eN
		set theResult to ""
	end try
	return theResult
end lookupMobileTelCo

on getSMSEmail(mobileTelCo, mobileNumber)
	try
		set theEmail to ""
		
		repeat with telCo in mobileTelCos
			if mobileTelCo is equal to (item 1 of telCo) then
				set theEmail to ((mobileNumber & (item 2 of telCo)) as string)
				exit repeat
			end if
		end repeat
		
	on error eM number eN
		display dialog eM with title eN
		set theEmail to ""
	end try
	
	return theEmail
end getSMSEmail


[ Reply to This | # ]
Add mobile carrier and SMS email to Address Book entries
Authored by: ViViDboarder on Aug 04, '09 12:03:10PM

Cool!!!

Haha, I saw the title and was going to post a link to my script! Glad to see that people are finding it as useful as I did myself.



[ Reply to This | # ]
Add mobile carrier and SMS email to Address Book entries
Authored by: Jason Belec on Aug 10, '09 03:46:38PM

This is rather cool. Anyone got it to function for Canadian's? Denmark?



[ Reply to This | # ]
Add mobile carrier and SMS email to Address Book entries
Authored by: jsuar on Aug 21, '09 05:13:42AM

nice script! this was the only solution i could find after a good amount of searching...any solution for windows users?

also, i manage all my contacts via google contacts. i might look at the gc api, possibly write a similar script.

any thoughts?

JS



[ Reply to This | # ]