Skip to content

Instantly share code, notes, and snippets.

@sumbach
Created February 16, 2010 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sumbach/305357 to your computer and use it in GitHub Desktop.
Save sumbach/305357 to your computer and use it in GitHub Desktop.
-- TODO: save as a bundle and include the RaopX application: not sure if I can do that since it also contains libsamplerate
on EnsureGuiScriptingEnabled()
tell application "System Events"
get system attribute "sysv"
if result is less than 4144 then -- Mac OS X 10.3.0
tell application me to activate
display dialog "This computer cannot run this script" & return & return & "The script uses GUI Scripting technology, which requires an upgrade to Mac OS X 10.3 Panther or newer." with icon caution buttons {"Quit"} default button "Quit"
return false
end if
if not UI elements enabled then
tell application me to activate
display dialog "GUI Scripting is not enabled" & return & return & "Open System Preferences and check Enable Access for Assistive Devices in the Universal Access preference pane, then run this script again." with icon stop
if button returned of result is "OK" then
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
end tell
end if
return false
end if
end tell
return true
end EnsureGuiScriptingEnabled
on IsSystemPreferencesRunning()
tell application "System Events"
if exists process "System Preferences" then
return true
else
return false
end if
end tell
end IsSystemPreferencesRunning
on HideSystemPreferences(shouldHide)
tell application "System Events"
set visible of process "System Preferences" to not shouldHide
end tell
end HideSystemPreferences
on ActivateSoundPreferencePane()
tell application "System Preferences"
set current pane to pane "com.apple.preference.sound"
end tell
end ActivateSoundPreferencePane
on ActivatePanelByName(panelName)
tell application "System Events"
tell process "System Preferences"
tell tab group 1 of window "Sound"
click radio button panelName
end tell
end tell
end tell
end ActivatePanelByName
on GetSelectedSourceName()
tell application "System Events"
tell process "System Preferences"
set sources to every row of table 1 of scroll area 1 of tab group 1 of window "Sound"
repeat with source in sources
if selected of source is true then
return value of text field 1 of source as text
end if
end repeat
end tell
end tell
end GetSelectedSourceName
on SetSelectedSource(sourceName)
tell application "System Events"
tell process "System Preferences"
set sources to every row of table 1 of scroll area 1 of tab group 1 of window "Sound"
repeat with source in sources
if (value of text field 1 of source as text) is equal to sourceName then
set selected of source to true
return
end if
end repeat
end tell
end tell
end SetSelectedSource
on ActivateInputPanel()
ActivatePanelByName("Input")
end ActivateInputPanel
on ActivateOutputPanel()
ActivatePanelByName("Output")
end ActivateOutputPanel
on GetSelectedInputName()
ActivateInputPanel()
GetSelectedSourceName()
end GetSelectedInputName
on GetSelectedOutputName()
ActivateOutputPanel()
GetSelectedSourceName()
end GetSelectedOutputName
on SetSelectedInput(inputName)
ActivateInputPanel()
SetSelectedSource(inputName)
end SetSelectedInput
on SetSelectedOutput(outputName)
ActivateOutputPanel()
SetSelectedSource(outputName)
end SetSelectedOutput
if not EnsureGuiScriptingEnabled() then
return
end if
set systemPreferencesAlreadyRunning to IsSystemPreferencesRunning()
tell application "System Preferences" to activate
ActivateSoundPreferencePane()
-- TODO: save current preference pane
set previousInputName to GetSelectedInputName()
SetSelectedInput("SoundFlower (2ch)")
set previousOutputName to GetSelectedOutputName()
SetSelectedOutput("SoundFlower (2ch)")
HideSystemPreferences(true)
do shell script "/usr/local/bin/RaopX &> /dev/null &"
display dialog "RaopX is running" buttons {"Quit"} default button "Quit"
do shell script "killall -KILL RaopX &> /dev/null || true"
SetSelectedOutput(previousOutputName)
SetSelectedInput(previousInputName)
if not systemPreferencesAlreadyRunning then
tell application "System Preferences" to quit
else
HideSystemPreferences(false)
end if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment