MacOS – Using AppleScript to click radio buttons

applescriptdockmacos

I think this is a bug in Mac OS X: with a dual monitor setup, the Dock switches to the 2nd (not main) monitor from time to time.

To fix this issue, I open the Dock, and click both the "Left" and "Bottom" radio buttons for the "Position on Screen" item.

I need to automate this with AppleScript. I could open the Dock using this script, but I'm not sure how to click the Left and then Bottom radio button in AppleScript.

tell application "System Preferences"
    set the current pane to pane id "com.apple.preference.dock"
    activate
end tell

I tried to add some code in the script, but it does not work.

click radiobutton "Left"

enter image description here

Best Answer

You can click the radio buttons in more then one way.

After the tell application "System Preferences" block use a tell application "System Events" block, e.g.:

tell application "System Preferences"
    set the current pane to pane id "com.apple.preference.dock"
    activate
end tell

tell application "System Events"
   click radio button 1 of radio group 1 of window 1 of process "System Preferences"
end tell

In this first usage, radio button 1 is Left, radio button 2 is Bottom and radio button 3 is Right.

Or:

tell application "System Preferences"
    set the current pane to pane id "com.apple.preference.dock"
    activate
end tell

tell application "System Events"
   click radio button "Left" of radio group 1 of window "Dock" of process "System Preferences"
end tell

In this second usage, use Left, Bottom or Right directly and you can also use Dock in place of window 1. (The latter of which you could do in any of these examples.)

Or:

tell application "System Preferences"
    set the current pane to pane id "com.apple.preference.dock"
    activate
end tell

tell application "System Events"
    click (every radio button whose value of attribute "AXTitle" is "Left") of radio group 1 of window 1 of process "System Preferences"
end tell

In this third usage, use Left, Bottom or Right for the value of (every radio button whose value of attribute "AXTitle" is "?") where the ? is one of these values.

Note: These examples have been tested under OS X 10.11.6 and work as now edited. I added
of radio group 1 between radio button and window. For use in OS X 10.8.5 remove of radio group 1 from the code.