Dialing the Phone from OS X Address Book with AppleScript, XML-RPC, PHP and Asterisk

This may qualify as the single most complicated way to dial the phone ever created. However the end result — click on number in the Mac OS X Address Book and have it dialed for me — is quite neat.

OS X Address Book

Here’s what happens when I select the “Dial” mouseover for a telephone number: an AppleScript is called that sends an XML-RPC request to a PHP-based XML-RPC server that then, in turn, places the call by connecting, over TCP/IP, with the Asterisk Manager API, to my Asterisk PBX.

The end result is that the phone on my desk rings, I pick up, and my call is connected with the number I selected.

Here are the pieces.

The AppleScript Address Book Plug-in

Using this burger locator script as a starting point, I created an AppleScript called AddressBook2Asterisk [hint: on a Mac, simply click on that link to open the script in Script Editor] that looks like this:

using terms from application “Address Book”
  
  on action property
    return “phone”
  end action property
  
  on action title for p with e
    return “Dial”
  end action title
  
  on should enable action for p with e
    if value of e is missing value then
      return false
    else
      return true
    end if
  end should enable action
  
  on perform action for p with e
    set telephone to value of e
    set resultList to DialOut(“peter”, “secret”, telephone)
    return true
  end perform action
  
end using terms from

on DialOut(username, password, telephone)
  tell application 
    “http://www.serverurl.com/directory/asterisk-dial-xmlrpc.php”
    return call xmlrpc {method name:”asterisk.dial”, 
    parameters:{username, password, telephone}}
  end tell
end DialOut

The XML-RPC Server

This AppleScript calls an XML-RPC server called asterisk-dial-xmlrpc.php, written in PHP, using the Useful Inc. XML-RPC in PHP code running on my webserver.

The XML-RPC server accepts three parameters from the XML-RPC client called from AppleScript: username, password and telephone number. The username and password are the same as in /etc/asterisk/manager.conf. This affords some measure of security, as it means that without the username and password, arbitrary calls to Timbuktu can’t be made by others (note, however, that the password is sent cleartext, so you should take precautions if your client and your server aren’t local to each other and behind a firewall).

Dialing the Telephone

Assuming the username and password are correct, the PHP script then, using a snippet of code found here, connects directly with the Asterisk Manager API via TCP/IP on port 5038, and originates the call.

And that’s it: select “Dial” and the phone rings and the call connects.

Why XML-RPC?

You may wonder why I complicate things by using XML-RPC to communicate from AppleScript to Asterisk rather than simply connecting directly to the Asterisk Manager API from AppleScript. Simple answer is “I couldn’t figure out how to do that.” More subtle answer is “creating the XML-RPC server means I can dial the phone from anything in the world that can talk XML-RPC.”

I’m not a master, or even a journeyman, at AppleScript, XML-RPC or Asterisk, so I welcome commments on improving the code.