Hack91.Hot-Switch Among Sound Devices


Hack 91. Hot-Switch Among Sound Devices

By attaching a script to a hotkey sequence, you can hot-switch among the sound devices used by Skypeeven during a call!

Works with: Windows, Linux, and Mac OS X versions of Skype.

You might want to switch from one sound device to another for many reasons. For instance, you might want to switch from a microphone and speaker to a handset for privacy from eavesdroppers during a call, or you might have a handset but you like to play your voicemail over a speaker. The scripts in this hack will show you how to hot-switch among sound devices with fast and easy hotkey sequences.

There are two ways of switching sound devices in Skype by means of scripts. First, you can drive its GUI interface to do the job, which is often ugly but avoids having to deal with the Skype API. The second method is to drive the Skype API from a script, which, when possible, is a little cleaner and less error prone. I'll present both methods in this hack. It's your choice as to what works best for your setup.

In the case of scripts that drive the Skype GUI, in effect you will simply have the script drive those interactions with Skype that you would otherwise do by hand. You will save yourself a world of frustration if, when writing these types of script, you have a notepad and pencil close at hand. Jot down your steps, no matter how small, so that when it comes time to replay them in a script, no steps are missed. Missing a single step in a script may mean that your script simply won't work, or that it does something totally unintended. You have been warned!

12.2.1. Windows

Whichever method you choosepure scripting or scripting the Skype APIit will simplify things if you first create a shortcut for Skype.exe on your desktop or somewhere in the Start menu and assign a hotkey sequence to itsay, Ctrl-Shift-S. This will make things easier because we'll be sending key sequences to Skype, but we can do that only if Skype is open and has focus. But Skype spends much of its time minimized in the Windows system tray. By assigning Skype a hotkey sequence of its own, we can make it open and pop up anytime we want.

You can enable hotkeys in Skype (select Skype Tools Options… Hotkeys) and assign a hotkey sequence to focus Skype. However, this method of gaining focus for Skype has the annoying side effect of minimizing Skype to the system tray when Skype is already open. This, clearly, is not the behavior required by a script that always needs to send keystrokes to an open window that has focus.


12.2.1.1. Script Skype's GUI.

Here are the beginnings of our script to hot-switch sound devices through Skype's GUI. This script, focus.vbs, will display Skype and give it focus:

 ' File: focus.vbs Dim objShell Set objShell = WScript.CreateObject("WScript.Shell") objShell.SendKeys "^+S" ' Send ctrl+shift+S 

Now, having gotten Skype's attention, we can send it a sequence of keys to get to the sound-device setup screen for Skype. The following script, sound_devices.vbs, will get you to the point show in Figure 12-4.

Figure 12-4. Skype sound device options screen


 ' File: sound_devices.vbs Dim objShell Set objShell = WScript.CreateObject("WScript.Shell") objShell.SendKeys "^+S"     ' Send ctrl+shift+S WScript.Sleep 1000          ' Wait for Skype to open and gain focus objShell.SendKeys "%(TO)"   ' Tools --> Options… objShell.SendKeys "{TAB}"   ' Move to left pane … objShell.SendKeys "{TAB}"   ' …done. objShell.SendKeys "{DOWN}"  ' Move to Sound Devices … objShell.SendKeys "{DOWN}"  ' … objShell.SendKeys "{DOWN}"  ' … objShell.SendKeys "{DOWN}"  ' …done. objShell.SendKeys "{TAB}"   ' Move to audio-in pulldown menu … objShell.SendKeys "{TAB}"   ' …done. objShell.SendKeys "%{DOWN}" ' Pull down the menu to display devices. 

From here on, all you have to do is replicate the key sequences you would enter by hand. In my case, the script, logitech.vbs, to make my Logitech USB headset the device used by Skype for all sound purposes looks like this:

 ' File: logitech.vbs Dim objShell Set objShell = WScript.CreateObject("WScript.Shell") objShell.SendKeys "^+S"     ' Send ctrl-shift-S WScript.Sleep 1000          ' Wait for Skype to open and gain focus objShell.SendKeys "%(TO)"   ' Tools --> Options… WScript.Sleep 100 objShell.SendKeys "{TAB}"   ' Move to left pane … WScript.Sleep 100 objShell.SendKeys "{TAB}"   ' …done. WScript.Sleep 100 objShell.SendKeys "{DOWN}"  ' Move to Sound Devices … WScript.Sleep 100 objShell.SendKeys "{DOWN}"  ' … WScript.Sleep 100 objShell.SendKeys "{DOWN}"  ' … WScript.Sleep 100 objShell.SendKeys "{DOWN}"  ' …done. WScript.Sleep 100 objShell.SendKeys "{TAB}"   ' Move to audio-in pulldown menu … WScript.Sleep 100 objShell.SendKeys "{TAB}"   ' …done. WScript.Sleep 100 objShell.SendKeys "L"       ' Set audio-in to Logitech WScript.Sleep 100 objShell.SendKeys "{TAB}" WScript.Sleep 100 objShell.SendKeys "L"       ' Set audio-out to Logitech WScript.Sleep 100 objShell.SendKeys "{TAB}" WScript.Sleep 100 objShell.SendKeys "L"       ' Set ringer to Logitech WScript.Sleep 100 objShell.SendKeys "{TAB}"   ' Move to save button … WScript.Sleep 100 objShell.SendKeys "{TAB}"   ' … WScript.Sleep 100 objShell.SendKeys "{TAB}"   ' …done. WScript.Sleep 100 objShell.SendKeys "{ENTER}" ' Press the save button. 

During a call, Skype is busy with many things. For Skype not to miss keystrokes sent by the previous script during a call, the script pauses for 100 milliseconds before issuing each sequence of keystrokes.

Another script, c-media.vbs, which is identical in every respect to logitech.vbs except that L is replaced with C, will switch Skype's sound devices back to the C-Media USB headphone set. All that remains is to create shortcuts for logitech.vbs and c-media.vbs and assign hotkeys to themsay, Ctrl-Shift-L and Ctrl-Shift-C, respectively. Then, switching between the two deviceseven during a callis just a short hotkey sequence away.

12.2.1.2. Script Skype's API.

Switching sound devices in Skype using the Skype API requires that three commands be sent to Skype: AUDIO_IN, AUDIO_OUT, and RINGER. This script, logitech_API.vbs, in combination with ActiveS, will have Skype switch to the Logitech USB headset sound device. An almost identical script, c-media_API.vbs, which replaces the words "Logitech USB Headset" with "C-Media USB Headphone Set," will switch Skype to use that device instead. These two scripts, logitech_API.vbs and c-media_API.vbs, are the Skype API equivalents of logitech.vbs and c-media.vbs (as discussed earlier).

 ' File: logitech_API.vbs Dim objSkypeAPI Set objSkypeAPI = WScript.CreateObject("SkypeAPI.Access") objSkypeAPI.SendBlockingCommand "SET AUDIO_IN Logitech USB Headset" objSkypeAPI.SendBlockingCommand "SET AUDIO_OUT Logitech USB Headset" objSkypeAPI.SendBlockingCommand "SET RINGER Logitech USB Headset" 

12.2.2. Linux

If you want to switch sound devices on Linux using a script, you have to use the Skype API. You can invoke this script, hot-switch.py (experimental), from the command prompt like this: python hot-switch.py '/dev/dsp1, where /dev/dsp1 is the name of the audio device that you want Skype to use for both audio-in and audio-out functions:

 #!/usr/bin/env python # -*- coding: iso-8859-15 -* # File: hot-switch.py (experimental) import dbus, sys class addon:     # Name of Skype API addon (that is, name of this script addon)     def name(self):         return 'NAME HotSwitch'     # Skype API protocol required by this addon     def protocol(self):         return 'PROTOCOL 1' class skype_api:     def __init__(self):         remote_bus = dbus.SystemBus()         system_service_list = remote_bus.get_service( \         'org.freedesktop.DBus').get_object( \         '/org/freedesktop/DBus', \         'org.freedesktop.DBus').ListServices()         skype_api_found = 0         for service in system_service_list:             if service=='com.Skype.API':                 skype_api_found = 1                 break         if not skype_api_found:          sys.exit('No running API-capable Skype found')         skype_service = remote_bus.get_service('com.Skype.API')          self.skype_api_object = skype_service.get_object( \          '/com/Skype', 'com.Skype.API')         this_addon = addon()         answer = self.send_message(this_addon.name())          if answer != 'OK':              sys.exit('Could not bind to Skype client')         answer = self.send_message(this_addon.protocol())                 if answer != this_addon.protocol():             sys.exit(this_addon.protocol() + \                  ' is not supported by the version' + \                  ' of Skype you are running')     def send_message(self, message):         answer = self.skype_api_object.Invoke(message)         print 'MESSAGE SENT TO SKYPE --> '    + message         print 'MESSAGE RETURNED BY SKYPE <-- ' + answer         return answer     def main():         skypeapi = skype_api()         skypeapi.send_message('SET AUDIO_IN '  + sys.argv[1])         skypeapi.send_message('SET AUDIO_OUT ' + sys.argv[1])         return 0 if __name__ == "__main__":     main() 

12.2.3. Mac OS X

On Mac OS X, using AppleScript, you can hot-switch sound devices either through Skype's GUI interface, or by using its API.

12.2.3.1. Script Skype's GUI.

This script, logitech.scpt, will switch the sound device used by Skype to the Logitech USB headset. To make it work for other sound devices, copy the file and rename it, and then change the name of the sound device used in the script. Note that you must be very careful with device names, as the name in your script and the name of the device for Skype must match exactly. For example, to switch to my "C-Media USB Headphone Set," the name I actually had to use in my script was "C-Media USB Headphone Set " (with two trailing blank characters). Just be careful with names and you should be hot switching between sound devices in no time.

 -- File: logitech.scpt tell application "Skype"     activate end tell tell application "System Events"     tell process "Skype"         click menu item "Preferences…

" of menu 1 of

menu bar item "Skype" of menu bar 1 try click button "Audio" of tool bar 1 of window "General" end try try click button "Audio" of tool bar 1 of window "Privacy" end try try click button "Audio" of tool bar 1 of window "Events" end try try click button "Audio" of tool bar 1 of window "Voicemail" end try ry click button "Audio" of tool bar 1 of window "Advanced" end try try click button "Audio" of tool bar 1 of window "Chat" end try click pop up button 1 of group 1 of group 1 of window "Audio" pick menu item "Logitech USB Headset" of menu of

pop up button 1 of group 1 of group 1 of window "Audio" delay 0.5 click pop up button 2 of group 1 of group 1 of window "Audio" pick menu item "Logitech USB Headset" of menu of

pop up button 2 of group 1 of group 1 of window "Audio" delay 0.5 click button 1 of window "Audio" end tell end tell

12.2.3.2. Script Skype's API.

The API script equivalent of logitech.scpt, logitech_API.scpt (experimental), is a lot shorter and a lot cleaner. But that same warning about sound device names also applies.

 -- File: logitech_API.scpt (experimental) tell application "Skype"      send command "SET AUDIO_IN Logitech USB Headset" script name "Audio in"     send command "SET AUDIO_OUT Logitech USB Headset" script name "Audio out" end tell 




Skype Hacks
Skype Hacks: Tips & Tools for Cheap, Fun, Innovative Phone Service
ISBN: 0596101899
EAN: 2147483647
Year: 2005
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net