twiddler
twiddler

Reputation: 588

applescript access switch control through system preferences

My end goal is to click the "Enable Switch Control" checkbox, which can be found under Accessibility-> Switch Control in System Preferences. I have had some trouble getting the applescript to work and would like to find out what is causing the issue. My current code:

if running of application "System Preferences" then
    quit application "System Preferences"
    delay 1
end if
    
tell application "System Preferences"
    reveal anchor "Switch" of pane id "com.apple.preference.universalaccess"
    activate
end tell

I got the anchor Switch by using:

tell application "System Preferences"
    get anchors of pane id "com.apple.preference.universalaccess"
end tell

which returned all of the available anchors. Here is a snippet:

...anchor TextToSpeech of pane id com.apple.preference.universalaccess, anchor Dwell of pane id com.apple.preference.universalaccess, anchor Dictation of pane id com.apple.preference.universalaccess, anchor Switch of pane id com.apple.preference.universalaccess, anchor Siri of pane id com.apple.preference.universalaccess...

I tried testing the applescript with other anchors (Dictation, Siri, etc) and they all worked (directed me to the intended area), but Switch doesn't. I even tried replacing "Switch" with its numerical value, reveal anchor 11 of pane id "com.apple.preference.universalaccess", but that also fails. Why is that? Also, how would I modify the applescript to get the intended result?

Upvotes: 0

Views: 754

Answers (1)

user3439894
user3439894

Reputation: 7555

There appears to be a bug in that System Preferences will not properly reveal anchor "Switch" of pane id "com.apple.preference.universalaccess" and as such, here is a workaround.

Note that the coding in the example AppleScript code assumes that Switch Control has once previously been turned on manually so as to answer the initial System Preferences is trying to unlock Accessibility preferences dialog box where you have entered your User Name and Password then clicked the Unlock button.

The example AppleScript code, shown below, was tested in Script Editor under macOS Catalina and macOS Big Sur with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue to click the Enable Switch Control checkbox.1

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

and works for me to click the Enable Switch Control checkbox:

--  # 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 Accessibility pane.

tell application "System Preferences" to ¬
    reveal pane id "com.apple.preference.universalaccess"

--  # Use System Events to achieve the goal.

tell application "System Events"
    tell window 1 of application process "System Preferences"
        
        --  # Wait for target pane to be available.
        --  # The target in this case has a checkbox.
        
        my waitForUIelement(checkbox 1)
        
        --  # Ascertain the target row to select.
        
        set rowSwitchControl to the first row of ¬
            table 1 of scroll area 1 whose value of ¬
            static text 1 of UI element 1 is ¬
            "Switch Control"
        
        --  # Select the target row.
        
        select rowSwitchControl
        
        --  # Wait for target checkbox to be available.
        
        my waitForUIelement(checkbox 1 of tab group 1 of group 1)
        
        --  # Click the Enable Switch Control checkbox.
        
        click checkbox 1 of tab group 1 of group 1
        
    end tell
end tell

delay 0.02

quit application "System Preferences"


--  ## Handler(s) ##

on waitForUIelement(uiElement)
    tell application "System Events"
        tell window 1 of application process ¬
            "System Preferences"
            set i to 0
            repeat until exists uiElement
                delay 0.1
                set i to i + 1
                if i ≥ 30 then return
            end repeat
        end tell
    end tell
end waitForUIelement

Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

Upvotes: 2

Related Questions