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

Peter Rukavina

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

Submitted by Neal Young on

Permalink

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 <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=”“>

Submitted by neal on

Permalink

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]

Submitted by Zed on

Permalink

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

Submitted by Andrea on

Permalink

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

Submitted by Brandon on

Permalink

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.

Submitted by Bradley on

Permalink

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);

Submitted by Ste73 on

Permalink

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
— <sip 120-084d9428=”“> 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

Submitted by cmd1234 on

Permalink

for Asterisk now the following might help especialy if you are using the web interface

$ASTERISK_CONTEXT= “home”;
change to
$ASTERISK_CONTEXT= “from-internal”;

Submitted by Anonymous221 on

Permalink

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/ph…
//
// 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”);

Submitted by Nicholas on

Permalink

Anyone who comes across this site and is looking for a solution:
"... we have removed support for both compiled and AppleScript-based AddressBook plugins in Mojave 10.14. ..."

Add new comment

Plain text

  • Allowed HTML tags: <b> <i> <em> <strong> <blockquote> <code> <ul> <ol> <li>
  • Lines and paragraphs break automatically.

About This Blog

Photo of Peter RukavinaI am . I am a writer, letterpress printer, and a curious person.

To learn more about me, read my /nowlook at my bio, listen to audio I’ve posted, read presentations and speeches I’ve written, or get in touch (peter@rukavina.net is the quickest way). 

You can subscribe to an RSS feed of posts, an RSS feed of comments, or a podcast RSS feed that just contains audio posts. You can also receive a daily digests of posts by email.

Search