I happened to need the same thing a few months ago:
[php]
tell application "System Preferences" to activate
tell application "System Events"
get properties
tell process "System Preferences"
click menu item "Sound" of menu "View" of menu bar 1
delay 2
set theRows to every row of table 1 of scroll area 1 of ¬
tab group 1 of window "sound"
set theOutputs to {} as list
repeat with aRow in theRows
copy (value of text field 1 of aRow as text) to the end of theOutputs
end repeat
tell application "Finder"
activate
set desiredOutput to display dialog ¬
"Choose Sound Output: " buttons theOutputs default button "Internal Speakers"
end tell
repeat with aRow in theRows
if (value of text field 1 of aRow as text) is equal to ¬
(button returned of desiredOutput as text) then
set selected of aRow to true
exit repeat
end if
end repeat
end tell
end tell
tell application "System Preferences" to quit
[/php]
However - I'd recommend the free SoundSource menuItem instead:
http://www.rogueamoeba.com/freebies/