changeset 16:1fee5287e956

Added skype command
author Atul Varma <varmaa@toolness.com>
date Sat, 27 Mar 2010 16:12:36 -0700
parents e96c724d3c7a
children 557f0edc2ca4
files my-enso-commands.py
diffstat 1 files changed, 56 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/my-enso-commands.py	Sat Mar 27 15:13:15 2010 -0700
+++ b/my-enso-commands.py	Sat Mar 27 16:12:36 2010 -0700
@@ -38,6 +38,62 @@
 
     _do_service(ensoapi, "Look Up in Dictionary", word)
 
+DEFAULT_COUNTRY_CODE = "1"
+DEFAULT_AREA_CODE = "415"
+
+def number_to_pstn(number, default_country_code=DEFAULT_COUNTRY_CODE,
+                    default_area_code=DEFAULT_AREA_CODE):
+    """
+    Converts a colloquial phone number to a pstn.
+
+    >>> number_to_pstn('123.456.7890', default_country_code='1')
+    '+11234567890'
+
+    >>> number_to_pstn('456.7890', default_country_code='1',
+    ...                default_area_code='614')
+    '+16144567890'
+
+    >>> number_to_pstn('5') is None
+    True
+    """
+
+    DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
+
+    chars = "".join([char for char in number if char in DIGITS])
+
+    if len(chars) == 11:
+        pass
+    if len(chars) == 10:
+        chars = default_country_code + chars
+    elif len(chars) == 7:
+        chars = default_country_code + default_area_code + chars
+    else:
+        chars = None
+
+    if chars:
+        return "+%s" % chars
+    return None
+
+def cmd_skype(ensoapi, number=None):
+    "Call the given number with Skype."
+
+    if not number:
+        number = ensoapi.get_selection().get("text", "").strip()
+
+    if not number:
+        ensoapi.display_message("No text selection!")
+        return
+
+    number = number_to_pstn(number)
+
+    if not number:
+        ensoapi.display_message("That doesn't look like a phone number.")
+        return
+
+    _runAppleScript('tell application "Skype" '
+                    'to send command "CALL %s" '
+                    'script name "Enso"' % number)
+
 def cmd_speak(ensoapi, what=None):
     "Speak the given text out loud."