Search code examples
macosscriptingapplescript

How to use Applescript to Change Preference Pane Slider value?


I'm trying to create an AppleScript for setting the value of Input Volume of the Input category under the Sound menu in System Preferences.

How does one change the value of a Slider?

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"
            tell tab group 1 of window "Sound"
                click radio button "Input"
                select row 1 of table 1 of scroll area 1
                set selected of row 1 of table 1 of scroll area 1 to true
                set deviceselected to "Microphone"
                set slider "Input Volume" of group "Input Volume" of tab group "Input" to 0
                select row 2 of table 1 of scroll area 1
                set selected of row 2 of table 1 of scroll area 1 to true
                set deviceselected to "Microphone"
                set slider "Input Volume" of group "Input Volume" of tab group "Input" to 0


            end tell
        end tell
    end try
end if
end tell

This doesn't seem to work. I also tried using Accessibility Inspector to find out how to hierarchically access the elements in the following manner,

 value of slider of group "Input volume" of tab group "Input" of window "Sound" 

That doesn't seem to be a correct way either.

What's wrong here?

EDIT

set content of slider "Input volume" of tab group "Input" of window "Sound" of tab group     1 of window "Sound" of application process "System Preferences" to 0
        --> error number -1700 from content of slider "Input volume" of tab group "Input" of window "Sound" of tab group 1 of window "Sound" of application  **

So it returns an error. I couldn't find any description for error code -1700, what does it mean?


Solution

  • You can access the volume settings directly without using gui scripting. These commands are in the standard additions osax of applescript. To see the volume settings you can change use this. Notice it requires the word "get" in the command.

    get volume settings
    

    Looking at those results you can see that the input volume is one of the volume settings you can access. It's a value from 0 to 100. Here's how you can set it...

    set volume input volume 64
    

    The above command is a little strange because there is no "to" word in that command. You're not setting the volume "to" something so it's strange. Anyway Good luck!

    EDIT: Here's how to access it using gui scripting. Also if you want to know the error codes, I posted a script for that here. See post #9 for the latest version.

    tell application "System Preferences"
        activate
        set current pane to pane "com.apple.preference.sound"
    end tell
    
    tell application "System Events"
        tell application process "System Preferences"
            tell tab group 1 of window "Sound"
                click radio button "Input"
                get value of slider 1 of group 2
            end tell
        end tell
    end tell