Create 'calling card' numbers for iPhone contacts

Nov 27, '07 02:30:03PM

Contributed by: mkoistinen

I use a another provider for international calls from my mobile: OneTel (although, this hint should work great with others). This service allows me to dial a local number, then, when prompted, enter the international number I wish to connect to and it connects me as usual. This works fine on the iPhone, in that I can use this plan as described. But my previous mobiles allowed me to set-up 'calling cards' which, when turned on, allowed me to dial the international number directly. The phone would then intercept this and dial the local number for me, then enter my destination number. This is, to me, indispensable on a mobile phone, because without it, you have to either memorize the destination number (or write it down again) so you can enter it after the prompt -- a cumbersome task while you're on the go.

Until Apple starts to more properly support calling cards, I improvised by writing an AppleScript which will create 'onetel' versions of the numbers I am interested in. I first created a group in my Address Book called OneTel, and placed within this group all the numbers that I am interested in dialing on my iPhone -- even if they're international.

I then ran the following AppleScript...

set dialerGroup to "OneTel"
set dialerPrefix to "+442070180001pp"
set dialerSufix to "#"
set dialerLabelPrefix to "onetel "
set IDP to "00"
set CC to "44"

to searchReplace(thisText, searchTerm, replacement)
  set AppleScript's text item delimiters to searchTerm
  set thisText to thisText's text items
  set AppleScript's text item delimiters to replacement
  set thisText to "" & thisText
  set AppleScript's text item delimiters to {""}
  return thisText
end searchReplace

tell application "Address Book"
  
  -- Update all the records in the group "OneTel"
  set ThePeople to the people in group "OneTel"
  
  repeat with ThePerson in ThePeople
    
    -- First, we'll clear the old ones
    set PhoneNumbers to the phones of ThePerson
    repeat with ThePhone in PhoneNumbers
      set TheLabel to the label of ThePhone
      if (TheLabel begins with dialerLabelPrefix) then
        -- Effectively delete the old ones
        set the value of ThePhone to ""
      end if
    end repeat
    
    -- OK, make the new ones!
    set PhoneNumbers to the phones of ThePerson
    repeat with ThePhone in PhoneNumbers
      set TheLabel to the label of ThePhone
      if (TheLabel begins with dialerLabelPrefix) then
        -- We can ignore these now...
      else if (TheLabel contains "fax") then
        -- ignore fax numbers
      else
        -- OK, this looks like a good candidate
        set TheNumber to the value of ThePhone as text
        
        if TheNumber begins with (IDP & CC) or TheNumber begins with "+" & CC then
          -- Ignore numbers which are for our local country
        else
          
          if the first character of (TheNumber as text) is "+" then
            set AppleScript's text item delimiters to "+"
            set thisText to TheNumber's text items
            set AppleScript's text item delimiters to IDP
            set TheNumber to "" & thisText
            set AppleScript's text item delimiters to {""}
          end if
          
          set NewNumber to dialerPrefix & TheNumber & dialerSufix
          set NewLabel to dialerLabelPrefix & TheLabel
          
          -- Locate the right phone to update...
          set myPhone to null
          repeat with tmpPhone in PhoneNumbers
            if the label of tmpPhone is NewLabel then
              set myPhone to tmpPhone
            end if
          end repeat
          
          if myPhone is not null then
            set the value of myPhone to NewNumber
          else
            make new phone at the end of phones of ThePerson with properties {label:NewLabel, value:NewNumber}
          end if
        end if
      end if
    end repeat
  end repeat
  
end tell
The above script does the following: For every person in the group OneTel, it checks each number for the person and if it is not a fax number, but contains a number that starts with either + or the international dialling code (you set this) and a country code other than yours, it will create a new phone entry for the person. The number's label is prepended with "onetel " and it includes the full dialing sequence required for the card and phone number.

For example, if a number has the number home as +331234567890, then a number number onetel home with the digits xxxxxxxxxxpp00331234567890 will be created, where xxxxxxxxxx is the local dialing number for OneTel and each p is a two-second pause

I added this script (which I named Add OneTel Numbers) to the ~/Library » Scripts » Applications » Address Book folder, and this now shows up in my AppleScript menu when I'm in Address Book. When I've made some changes to some of the relevant cards, I run this script, then, next time I sync my iPhone, I'm all set. There's lots of room for improvement, but I felt it was good enough to share. Enjoy!

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20071112055244708