Can't get AppleScript to toggle mouse scrolling on Mac
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I copied this script I found on the following website to toggle the mouse scrolling setting in System Preferences: https://discussions.apple.com/thread/5601614

I pasted this script into Apple's Automator, but I keep getting one of two errors when I run it:

1) "System Events got an error: Can’t get window "Accessibility" of application process "System Preferences"."

2) "System Events got an error: Can’t get row 17 of table 1 of scroll area 1 of window "Accessibility" of application process "System Preferences". Invalid index."

I tried playing around with the "select (row 10)" line the script, but nothing seems to work. It could be that the script is out-of-date compared to the current settings layout in the latest Mac OS. I'm running Mac OS High Sierra (10.13.4)

The mouse scrolling setting is accessed here: System Preferences > Accessibility > Mouse & Trackpad > Mouse Options > Scrolling

======================

tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
end tell

tell application "System Events"
tell application process "System Preferences"
tell table 1 of scroll area 1 of window "Accessibility"
delay 0.2
select (row 10)
end tell

    click button 5 of window 1
    delay 0.1
    if value of checkbox 1 of sheet 1 of window "Accessibility" is 1 then
        click checkbox 1 of sheet 1 of window "Accessibility"
    else
        click checkbox 1 of sheet 1 of window "Accessibility"
    end if

    click button 1 of sheet 1 of window "Accessibility"
end tell

end tell

tell application "System Preferences"
quit
end tell

awarded to CyteBode

Crowdsource coding tasks.

1 Solution

Winning solution

I only have access to OS X 10.10 Yosemite, but I have the following which works partially for me:

tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.universalaccess"
end tell

tell application "System Events"
    tell application process "System Preferences"
        try
            tell table 1 of scroll area 1 of window "Accessibility"
                delay 0.2
                select (row 12)
            end tell
        on error
            tell table 1 of scroll area 2 of window "Accessibility"
                delay 0.2
                select (row 12)
            end tell
        end try

        click button 2 of window 1
        delay 0.1
        --if value of checkbox 1 of sheet 1 of window "Accessibility" is 1 then
        --    click checkbox 1 of sheet 1 of window "Accessibility"
        --else
        --    click checkbox 1 of sheet 1 of window "Accessibility"
        --end if

        click button 1 of sheet 1 of window "Accessibility"
    end tell
end tell

tell application "System Preferences"
    --quit
end tell

I had to change select (row 10) to select (row 12) so that the "Mouse & Trackpad" row was selected properly. I also had to change click button 5 of window 1 to click button 2 of window 1 as otherwise it would minimize the window instead of clicking the "Mouse Options..." button. I also commented out the quit directive to see what the script was doing.

One big problem is that once the "Mouse Options..." dialog box is open, there simply isn't any checkbox that says "Scrolling." for me, but that might be because I don't have a Magic Mouse. If the checkbox is there for you, the script should work once you remove the --'s.

If things are still different in macOS 10.13 High Sierra, you should be able to play with the two lines I had to change to make the script do what it's supposed to do.

Edit: Made the script work even if the currently selected tab in the "Accessibility" window has a scroll area of its own. However, you will still need to figure out the proper row and button index for High Sierra.

CyteBode, what ever I do, I can't get past the line where it selects the row. I've entered every integer up to 25 and each one stops with the error: "System Events got an error: Can’t get row 9 of table 1 of scroll area 1 of window "Accessibility" of application process "System Preferences". Invalid index." Rows 1 and 4 seem to work, but then I get the second error when it opens the wrong tab. Perhaps something has changed in High Sierra where this script code no longer works. Here's the list of tabs in the Accessibility pane: General VoiceOver Zoom Display Speech Media Descriptions Captions Hearing Audio Interacting Dictation Siri Keyboard Mouse & Trackpad Switch Control
CuriousMynd 3 months ago
Okay, I think I figured it out. My tabs in Yosemite are [(Vision), Display, Zoom, VoiceOver, (Media), Descriptions, Captions, (Hearing), Audio, (Interacting), Keyboard, Mouse & Trackpad, Switch Control, Dictation], which is why selecting row 12 works (the labels count as rows too). However, if I first select row 7, which is "Captions", suddenly, trying to select 12 afterwards gives me the same error you're getting. I do not know what the "General" tab looks like in High Sierra, but I'm willing to bet it's got a scroll area of its own, while the "Display" tab does not, but the "Captions" tab does. For some reason, that seems to change the index of the scroll area we want to affect. If I'm right, the fix would be to tell scroll area 2 instead of scroll area 1.
CyteBode 3 months ago
CyteBode, I made some progress based on your suggestion. The change to "scroll area 2" seems to work! The new General tab contains a table of accessibility shortcuts, which looks like it may be a new scroll area. The problem I have now is that the script only runs without errors if I'm sitting on the Accessibility panel when I run the script and the table of tabs on the left has the focus. If the Accessibility tab isn't already open with the table having the focus, then I get one of several errors.
CuriousMynd 3 months ago
CyteBode, You got me half the way there. We have different Mac OS's, so that makes the solution harder. Thanks for your efforts.
CuriousMynd 3 months ago
Thanks for awarding me the bounty anyway! I wish I had my hands on a system running High Sierra, but even renting access to one would have been more expensive than the bounty, so there wasn't much I could do...
CyteBode 3 months ago