Sunday, October 7, 2007

reverse lookup of names with Apple Script in Address Book.app

thought with an asterisx server on the network it should be possible to get incoming calls signaled on the mac - with Address Book.app lookup for names and pictures...

incoming (ISDN) calls are handled by the chan_capi module and then notified via app_notify
  • at the end of extensions.conf something like the following has to go (put in values for MSN and HOSTNAME_TO_BE_NOTIFIED)
[capi-in]
exten => MSN,1,Notify(${CALLERID(num)}|${CALLERID(name)}|${EXTEN}/HOSTNAME_TO_BE_NOTIFIED)
exten => MSN,2,Wait(120)
exten => MSN,3,Hangup()

update (9/11/2007):
i had a wrong Notify call in my extensions.conf and that was why the notification part didn't work... the AppleScript, still, exemplifies a lookup in Addressbook.app for a given number.
 
the OS X client comes from the same page, however for me the notification part with Address Book.app lookup didn't work. to have growl notification i then put an AppleScript into Library/Application Support/AsteriskNotifier/Scripts

global callerName, callerNumber, callerImg

-- notification("01792259314||4066585")
-- notification("||4066585")

on notification(message)
  
  -- message comes in "${CALLERID(num)}|${CALLERID(name)}|${EXTEN}"
  set the text item delimiters to {"|"}
  
  -- first the number
  set callerNumber to text item 1 of message
  
  -- second the name - if available
  if (text item 2 of message) is not equal to "" then
    set callerName to text item 2 of message
  else
    set callerName to "Unknown"
  end if
  
  -- third the number that was called
  set calledNumber to text item 3 of message
  
  -- for further reference...
  set callerImg to null
  
  set the text item delimiters to {" "}
  
  if callerNumber is not equal to "" then
    
    -- this is necessary if the number in Address Book.app is in a different format
    -- e.g. "034567890" doesn't match "+12 (345) 67890"
    set lookForNum to callerNumber
    set lookForNum to my trim_line(lookForNum, "00", 0)
    set lookForNum to my trim_line(lookForNum, "0", 0)
    
    tell application "Address Book"
      
      set found to false
      set phoneNums to {}
      
      repeat with loopPerson in people
        
        set PhoneNumsCount to (count every phone of loopPerson)
        repeat with i from 1 to PhoneNumsCount
          set phoneNum to (get value of phone i of loopPerson)
          
          set newString to my remove_specialChars(phoneNum)
          
          ignoring white space
            if newString contains lookForNum then
              -- beep
              -- display dialog phoneNum as string
              set phoneNums to phone i of loopPerson
              set callerNumber to phoneNum
              
              if callerName is not equal to "Unknown" then
                set callerName to first name of loopPerson & " " & last name of loopPerson
              end if
              
              if image of loopPerson exists then set callerImg to image of loopPerson
              exit repeat
            end if
          end ignoring
          
        end repeat
        
        if phoneNums is not {} then
          set found to true
          exit repeat
        end if
      end repeat
      
      -- if found then
      --  activate
      --  set selection to loopPerson
      -- end if
      
    end tell
    
  end if
  
  tell application "GrowlHelperApp"
    -- Make a list of all the notification types
    -- that this script will ever send:
    set the allNotificationsList to ¬
      {"Incoming Call"}
    
    -- Make a list of the notifications
    -- that will be enabled by default.
    -- Those not enabled by default can be enabled later
    -- in the 'Applications' tab of the growl prefpane.
    set the enabledNotificationsList to ¬
      {"Incoming Call"}
    
    -- Register our script with growl.
    -- You can optionally (as here) set a default icon
    -- for this script's notifications.
    register as application ¬
      "Asterisk Notification" all notifications allNotificationsList ¬
      default notifications enabledNotificationsList ¬
      
    -- Send a Notification...
    
    if callerImg is not null then
      notify with name ¬
        "Incoming Call" title ¬
        callerName & " @ " & (time string of (current date)) description ¬
        callerNumber as string application name ¬
        "Asterisk Notification" image (callerImg)
      -- callerNumber & " on " & calledNumber
      
    else
      notify with name ¬
        "Incoming Call" title ¬
        callerName & " @ " & (time string of (current date)) description ¬
        callerNumber as string application name ¬
        "Asterisk Notification" image from location "~/Library/Application Support/AsteriskNotifier/phone.jpg"
    end if
    
  end tell
end notification


on remove_specialChars(this_text)
  set copy_flag to true
  set the clean_text to ""
  repeat with this_char in this_text
    set this_char to the contents of this_char
    if this_char is not "(" and this_char is not ")" and this_char is not "+" and this_char is not "x" and this_char is not "-" and this_char is not "_" then
      set the clean_text to the clean_text & this_char as string
    end if
  end repeat
  return the clean_text
end remove_specialChars


on trim_line(this_text, trim_chars, trim_indicator)
  -- 0 = beginning, 1 = end, 2 = both
  set x to the length of the trim_chars
  -- TRIM BEGINNING
  if the trim_indicator is in {0, 2} then
    repeat while this_text begins with the trim_chars
      try
        set this_text to characters (x + 1) thru -1 of this_text as string
      on error
        -- the text contains nothing but the trim characters
        return ""
      end try
    end repeat
  end if
  -- TRIM ENDING
  if the trim_indicator is in {1, 2} then
    repeat while this_text ends with the trim_chars
      try
        set this_text to characters 1 thru -(x + 1) of this_text as string
      on error
        -- the text contains nothing but the trim characters
        return ""
      end try
    end repeat
  end if
  return this_text
end trim_line

references:
  1. Notify application module for the Asterisk PBX: http://mezzo.net/asterisk/app_notify.html
  2. AppleScript Support for Growl: http://growl.info/documentation/applescript-support.php#usingImages

  3. http://www.apple.com/applescript/guidebook/sbrt/pgs/sbrt.07.htm
  4. http://www.faqintosh.com/risorse/en/othutil/addrbook/lookupphn/
  5. http://www.mactech.com/articles/mactech/Vol.21/21.10/ScriptingAddressBook/index.html
  6. http://www.mumbles-project.org/
  7. http://www.acm.uiuc.edu/macwarriors/workshops/applescript/1999/nutshell/control.html

No comments: