Dialing through the contextual menu

Continuing the topic started in yesterday’s post, this post will outline a contextual menu item that dials a phone number through the modem. You use it by selecting the number you want to dial—in an email, on a web page, whatever—control-clicking (or right-clicking) to bring up the contextual menu, and selecting the Dial Selection item.

The menu item is created through a combination of AppleScript and On My Command. The AppleScript recycles some of the code in yesterday’s post, where you can find descriptions of the functions. It uses the wonderful dialModemOSAX scripting addition by Javier Díaz Reinoso. I call it asdial.scpt and keep it in my ~/bin folder.

property areaCode : "630"
property longDistance : "1"
property myModem : "/dev/cu.usbmodem"

-- change all letters to digits, strip punctuation,
-- and standardize pauses to ~
on translateNumber(num)
  set pipeline to "echo " & quoted form of num & ¬
    " | tr 'a-z' 'A-Z'" & ¬
    " | tr 'A-Z' '22233344455566677778889999'" & ¬
    " | tr ',' '~'" & ¬
    " | sed 's/[^0-9~]//g'"
  return do shell script pipeline
end translateNumber

-- return the numbers you actually dial
on makeDialable(num)
  set tnum to translateNumber(num)
  -- strip prefix and area code from local numbers
  if length of tnum is 11 ¬
    and characters 1 thru 4 of tnum as string ¬
    is (longDistance & areaCode) then
    set tnum to characters 5 thru 11 of tnum as string
  end if
  -- handle all 10-digit numbers
  if length of tnum is 10 then
    -- strip area code from local numbers
    if characters 1 thru 3 of tnum as string is "630" then
      set tnum to characters 4 thru 10 of tnum as string
    else -- add prefix to long distance numbers
      set tnum to longDistance & tnum
    end if
  end if
  return tnum
end makeDialable

on run argv
  -- initialize the data
  set phoneNumber to makeDialable(item 1 of argv)

  -- pause iTunes if it's running
    do shell script "ps acx | grep 'iTunes$'"
    tell application "iTunes" to pause
  end try

  -- dial it
  initModem myModem with "^MAT&F1E0S7=45S0=0L2^M"
  --was "~^M~AT&F1E0S7=45S0=0L2^M"
  dial modem "ATDT" & phoneNumber
  delay 3
  hang up

end run

It can be called from the command line, with the number to dial as its argument, but it’s more handy as a contextual menu item. Fire up OMCEdit, the editor that comes with On My Command, and make a new item that looks like this:

Just hit the Save button and you’re done. I find this really handy for dialing customer support numbers that I find on web pages. No more writing the number on a scrap of paper or looking back and forth between my monitor and the phone.