SoundSource from Rogue Amoeba is a much better way to switch sound source than this Applescript, and it's free.
Description: This script will toggle between sound output options
Requirement: Mac OS 10.3.x
-- Script Start (* Toggle Sound Output via GUI AppleScriping 2004-01-31 by Fredrik Jonsson <mailto(colon)fredrik(at)combonet(dot)se> home page <http://xdeb.org/wiki/AppleScript> *) tell application "System Preferences" activate set current pane to pane "com.apple.preference.sound" end tell tell application "System Events" if UI elements enabled then try tell application process "System Preferences" -- Go to the "Sound out" tab tell tab group 1 of window 1 click radio button 2 set the sound_out to the (title of radio button 2) as string -- Alternate between the first two options tell table 1 of scroll area 1 if (selected of row 2) then set selected of row 1 to true set the unit_name to the (value of text field 1 of row 1) as string beep 1 else set selected of row 2 to true set the unit_name to the (value of text field 1 of row 2) as string beep 2 end if end tell end tell -- Hide the System Preferences click menu item 5 of menu 1 of menu bar item 2 of menu bar 1 -- Display a dialog with the new sound output setting -- display dialog sound_out & " is set to: " & "\"" & unit_name & "\"" with icon 1 ¬ -- buttons "OK" default button 1 -- Use text to speech to say the sound output setting -- say "Sound output is set to" & unit_name end tell on error errString display dialog "Could not toggle sound output: " & return & "Error: " & errString ¬ with icon 2 buttons "OK" default button 1 end try else tell application "System Preferences" activate set current pane to pane "com.apple.preference.universalaccess" display dialog "UI element scripting is not enabled. Check \"Enable access for assistive devices\"" end tell end if end tell -- tell application "System Preferences" to quit -- Script Stop