Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is an AppleScript to toggle between 2 Sound Output Devices. Set your own Devices to toggle on lines 32 and 33
# Tell our specifc panel options to wake up.
tell application "System Preferences"
reveal anchor "output" of pane id "com.apple.preference.sound"
end tell
# Save our Current Output Device as well as a List of Available Output Devices.
tell application "System Events"
tell process "System Preferences"
tell window "Sound"
tell tab group 1
tell scroll area 1
tell table 1
set outputDeviceList to {}
repeat with deviceName in (first text field of every UI element)
set end of outputDeviceList to value of deviceName
end repeat
set selectedRow to (first UI element whose selected is true)
end tell
end tell
end tell
end tell
end tell
set currentOutputDevice to (value of text field 1 of selectedRow)
end tell
set switchToDeviceName to false
# log currentOutputDevice
# Set the Toggle Terms.
if currentOutputDevice is "5.1 Channel Audio" then set switchToDeviceName to "Yeti Stereo Microphone"
if currentOutputDevice is "Yeti Stereo Microphone" then set switchToDeviceName to "5.1 Channel Audio"
# log switchToDeviceName
# Only if the Toggle Terms have successfully been set do we start.
# e.g.: If our Output Device is 'Internal Speakers' our script doesn't do anything.
if switchToDeviceName is not false then
# If our intended Device doesn't exist in the list of devices, bail out.
if outputDeviceList contains switchToDeviceName then
# Switch to our device.
tell application "System Events"
tell process "System Preferences"
select (row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" whose value of text field 1 is switchToDeviceName)
end tell
end tell
end if
# Get some Growl Notifications for our Toggling Actions
tell application "Growl"
set the allNotificationsList to {"Sound Notification"}
set the enabledNotificationsList to {"Sound Notification"}
register as application "Toggle Sound Output" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Script Editor"
notify with name "Sound Notification" title "Audio Output" description switchToDeviceName application name "Toggle Sound Output"
end tell
end if
# Finish by Cleaning Up!
tell application "System Preferences" to quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment