-- # Check to see if System Preferences is
-- # running and if yes, then close it.
-- #
-- # This is done so the script will not fail
-- # if it is running and a modal sheet is
-- # showing, hence the use of 'killall'
-- # as 'quit' fails when done so, if it is.
-- #
-- # This is also done to allow default behaviors
-- # to be predictable from a clean occurrence.
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
delay 0.1
end if
-- # Make sure System Preferences is not running before
-- # opening it again. Otherwise there can be an issue
-- # when trying to reopen it while it's actually closing.
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
-- # Open System Preferences to the Ouput tab of the Sound pane.
tell application "System Preferences" to ¬
reveal anchor "output" of ¬
pane id "com.apple.preference.sound"
-- # Wait for the target UI element to be available to minulate.
-- # Then minulate the target UI element.
tell application "System Events"
tell application process "System Preferences"
set i to 0
repeat until exists slider 1 of group 1 of tab group 1 of window 1
delay 0.1
set i to i + 1
if i ≥ 30 then return
end repeat
set the value of slider 1 of group 1 of tab group 1 of window 1 to 0.5
end tell
delay 0.2
end tell
tell application "System Preferences" to quit