Skip to content

Instantly share code, notes, and snippets.

@bricky
Created February 25, 2011 21:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bricky/844554 to your computer and use it in GitHub Desktop.
Save bricky/844554 to your computer and use it in GitHub Desktop.
AppleScript Desktop Dialer for Asterisk
set clipnumber to (the clipboard as text)
set thenumber to truncateString(clipnumber, 50)
set answer to "OK"
repeat while answer = "OK"
set question to display dialog "Number to Dial:" buttons {"Cancel", "OK"} default button 2 default answer thenumber with icon 1
set answer to button returned of question
set thenumber to text returned of question
if answer is equal to "OK" then
DialOut(thenumber)
(display dialog "Dialing: " & thenumber buttons {"OK"} giving up after 2)
end if
end repeat
on truncateString(txt, max)
set txtLength to (length of txt)
if txtLength > max then
set txt to (text 1 thru (max - 1) of txt) as string
end if
return txt
end truncateString
on cleanString(str, chars)
set tid to AppleScript's text item delimiters
repeat with c in chars
set AppleScript's text item delimiters to {c}
set chunks to text items of str
set AppleScript's text item delimiters to ""
set str to chunks as Unicode text
end repeat
set AppleScript's text item delimiters to tid
return str
end cleanString
-- find : Text (or list of text) to be found
-- replace : Text (or list of text) to replace with
-- subject : Text (or list of text) to be searched
on str_replace(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set returnList to true
-- This wouldn't make sense (you could have it raise an error instead)
if class of find is not list and class of replace is list then return subject
if class of find is not list then set find to {find}
if class of subject is not list then ¬
set {subject, returnList} to {{subject}, false}
set findCount to count find
set usingReplaceList to class of replace is list
try
repeat with i from 1 to (count subject)
set thisSubject to item i of subject
repeat with n from 1 to findCount
set text item delimiters of AppleScript to item n of find
set thisSubject to text items of thisSubject
if usingReplaceList then
try
item n of replace
on error
"" -- `replace` ran out of items
end try
else
replace
end if
set text item delimiters of AppleScript to result
set thisSubject to "" & thisSubject
end repeat
set item i of subject to thisSubject
end repeat
end try
set text item delimiters of AppleScript to prevTIDs
if not returnList then return beginning of subject
return subject
end str_replace
on DialOut(telephone)
set mynumber to "SCCP/53" -- your asterisk extension number, change to SIP or IAX as appropriate
set username to "admin" -- asterisk manager username
set passwd to "amp111" -- asterisk manager password
set remotehost to "192.168.1.101" -- asterisk server ip address
set context to "from-internal" -- context for outgoing calls
set numbertocall to my cleanString(telephone, {" ", "-", ")", "(", "."})
set numbertocall to my str_replace("+", "00", numbertocall)
set callerid to numbertocall
set expectscript to "set timeout 20;
spawn telnet " & remotehost & " 5038;
expect \"Asterisk Call Manager/1.1\";
send \"Action: login
username: " & username & "
secret: " & passwd & "
\";
expect \"Message: Authentication accepted\";
sleep 1;
send \"Action: originate
Exten: " & numbertocall & "
Context: " & context & "
Channel: " & mynumber & "
Priority: 1
Callerid: Outgoing Call <" & callerid & ">
\";
sleep 1;
send \"Action: logoff
\";
"
-- display dialog expectscript
set results to do shell script "/usr/bin/expect -c '" & expectscript & "'"
-- if (results is not equal to "") then display dialog results
end DialOut
@bricky
Copy link
Author

bricky commented Feb 25, 2011

Related blog post is here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment