Skip to Content

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.

Comments

Hello, Can you comment how to modify the makefiles to get an Asterisk running in Panther. Thanks!!
I was able to do it from the tarball by changing the makefiles a little bit and the code a little bit. it runs under panther 10.3.4 anyway. here are the diffs: –- diff -r asterisk-0.9.1 asterisk-0.9.1.macosx # diff of the original directory and the modified one diff -r asterisk-0.9.1/Makefile asterisk-0.9.1.macosx/Makefile 163c163 < ifeq ($(shell uname -r),7.0.0) – > ifeq ($(shell uname -r),7.4.0) diff -r asterisk-0.9.1/aesopt.h asterisk-0.9.1.macosx/aesopt.h 157,158c157,158 < # include < # include – > # include > /*# include */ diff -r asterisk-0.9.1/asterisk.c asterisk-0.9.1.macosx/asterisk.c 1556c1556 < #if defined(__FreeBSD__) – > /*#if defined(__FreeBSD__)*/ 1672c1672 < #endif – > /*#endif*/ diff -r asterisk-0.9.1/md5.c asterisk-0.9.1.macosx/md5.c 8c8 < # include – > # include
the diff I pasted in the previous message got munged somehow by html stuff (?). namely, the “#include” lines that are followed by angle brackets had the angle brackets and the filename inside erased. Here is the same diff with brackets replaced by ‘[’ and ‘]’: diff -r asterisk-0.9.1 asterisk-0.9.1.macosx # diff of the original directory and the modified one diff -r asterisk-0.9.1/Makefile asterisk-0.9.1.macosx/Makefile 163c163 < ifeq ($(shell uname -r),7.0.0) – > ifeq ($(shell uname -r),7.4.0) diff -r asterisk-0.9.1/aesopt.h asterisk-0.9.1.macosx/aesopt.h 157,158c157,158 < # include [endian.h] < # include [byteswap.h] – > # include [machine/endian.h] > /*# include [byteswap.h]*/ diff -r asterisk-0.9.1/asterisk.c asterisk-0.9.1.macosx/asterisk.c 1556c1556 < #if defined(__FreeBSD__) – > /*#if defined(__FreeBSD__)*/ 1672c1672 < #endif – > /*#endif*/ diff -r asterisk-0.9.1/md5.c asterisk-0.9.1.macosx/md5.c 8c8 < # include [endian.h] – > # include [machine/endian.h]
You can use AppleScript to talk directly to the Astman interface on port 5038 through the TCP/IP Scripting Addition (aka TCP/IP OSAX) … http://www.osaxen.com/tcpip_scripting_addition.html however, this OSAX is not free. rgds benjk
Please show me the installation step by step. Thanks.
Why not do it all in Applescript for free! :) Cheers, —Zed –– using terms from application “Address Book” on action property return “phone” end action property on action title for p with e return “Dial with VOIPv2” 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(telephone) return true end perform action end using terms from on DialOut(telephone) set mynumber to 2000 set callerid to telephone set username to “zed” set passwd to “nottelling” set remotehost to “pbxint” set expectscript to “set timeout 20; spawn telnet ” & remotehost & ” 5038; expect "Asterisk Call Manager/1.0"; send "Action: login username: ” & username & ” secret: ” & passwd & ” "; expect "Message: Authentication accepted"; send "Action: originate Exten: ” & telephone & ” Context: inside Channel: SIP/” & mynumber & ” Priority: 1 Callerid: ” & callerid & ” "; send "Action: logoff "; ” set results to do shell script “usr/bin/expect -c ‘” & expectscript & “’” — if (results is not equal to “”) then display dialog results end DialOut
Hi, it works very well, thank you very much. Just a question: when I call an AddressBook contact, I see in the phone “from Address Book (Unknown)” … Could I change it with something like “Andrea (xxx)”? How could I do this? Thanks for your support Regards Andrea
I know this can be modified to show the name, I have to do someting like return “name” but I need some help on the applescript any volunteers send brandon at dacrib dot net.
Your script has $timeout but none listed … I suppose it must work great somewhere, but if anyone is having problems, put a hardcoded value in or assign it to the variable: $socket = fsockopen($ASTERISK_IP,$ASTERISK_PORT, $errno, $errstr, 30);
If you still want to use this - XML-RPC exists at http://downloads.sourceforge.net/phpxmlrpc/xmlrpc-2.2.tar.gz?modtime=1172790041&big_mirror=0
Hi, we used your script for years with our old pbx, now we change to freepbx … and … we have a problem: call are generated, internal sip phone rings, but when we answer we have: == Starting SIP/120-084d9428 at DefaultOutgoingRule,3357886692,1 failed so falling back to exten ‘s’ == Starting SIP/120-084d9428 at DefaultOutgoingRule,s,1 still failed so falling back to context ‘default’ — Executing [s@default:1] Playback(“SIP/120-084d9428”, “vm-goodbye”) in new stack — Playing ‘vm-goodbye’ (language ‘en’) — Executing [s@default:2] Macro(“SIP/120-084d9428”, “hangupcall”) in new stack — Executing [s@macro-hangupcall:1] GotoIf(“SIP/120-084d9428”, “1?skiprg”) in new stack — Goto (macro-hangupcall,s,4) — Executing [s@macro-hangupcall:4] GotoIf(“SIP/120-084d9428”, “1?skipblkvm”) in new stack — Goto (macro-hangupcall,s,7) — Executing [s@macro-hangupcall:7] GotoIf(“SIP/120-084d9428”, “1?theend”) in new stack — Goto (macro-hangupcall,s,9) — Executing [s@macro-hangupcall:9] Hangup(“SIP/120-084d9428”, “”) in new stack == Spawn extension (macro-hangupcall, s, 9) exited non-zero on ‘SIP/120-084d9428’ in macro ‘hangupcall’ == Spawn extension (default, s, 2) exited non-zero on ‘SIP/120-084d9428’ good bye? why? tks
for Asterisk now the following might help especialy if you are using the web interface $ASTERISK_CONTEXT = “home”; change to $ASTERISK_CONTEXT = “from-internal”;
The include files are missing. Where can I download them??? The links are broken! //–––––––––––––––––— // We’re using the Useful Inc. XML-RPC server, which // you can get from: // // http://xmlrpc.usefulinc.com/php.html // // I’ve installed it under /www/php-scripts/xmlrpc - // you will obviously change for your location. //–––––––––––––––––— include(“/www/php-scripts/xmlrpc/xmlrpc.inc”); include(“/www/php-scripts/xmlrpc/xmlrpcs.inc”);