Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidneys/0063043cfdb5f452fd8d3f275de2dac2 to your computer and use it in GitHub Desktop.
Save sidneys/0063043cfdb5f452fd8d3f275de2dac2 to your computer and use it in GitHub Desktop.
AppleScript | Toggle Accessibility Keyboard (macOS)
(*
* toggle-macos-accessibility-keyboard
* applescript
*
* description:
* macOS automation script to toggle the macOS Accessibility Keyboard.
* Shows and hides the keyboard depending on its current state.
*
* author: sidneys
* homepage: http://sidneys.github.io
* version: 2.0.1
* license: MIT
*)
-- Init
use AppleScript version "2.7"
use scripting additions
-- Persist start-up state of the "System Preferences" app
set didRunSystemPreferences to (get running of application "System Preferences")
-- Initialize storage for the checkbox value
set initialCheckboxValue to -1
set currentCheckboxValue to -1
-- Show "Keyboard" pane within the Accessibility preferences
tell application "System Preferences"
reveal anchor 2 of pane id "com.apple.preference.universalaccess"
end tell
-- Start automated interaction
tell application "System Events"
-- Wait for: System Preferences Window
repeat until tab group 1 of window 1 of process "System Preferences" exists
end repeat
-- Wait for: Settings Pane
repeat until (name of second radio button of tab group 1 of window 1 of process "System Preferences") is "Accessibility Keyboard"
end repeat
-- Select the "Accessibility Keyboard" Segmented Control
click radio button 2 of tab group 1 of window 1 of process "System Preferences"
-- Tick the "Enable Accessibility Keyboard" checkbox, remembering its before/after value
set initialCheckboxValue to value of checkbox 1 of tab group 1 of window 1 of process "System Preferences"
click checkbox 1 of tab group 1 of window 1 of process "System Preferences"
set currentCheckboxValue to value of checkbox 1 of tab group 1 of window 1 of process "System Preferences"
-- Did the checkbox value change?
if initialCheckboxValue is currentCheckboxValue then
-- No - Wait for: confirmation dialog
repeat until sheet 1 of window 1 of process "System Preferences" exists
end repeat
-- Dismiss dialog
click button 1 of sheet 1 of window 1 of process "System Preferences"
end if
end tell
-- Did the "System Preferences" app run on start-up?
if not didRunSystemPreferences then
-- No - Quit
quit application "System Preferences"
else
-- Yes - Return to the overview screen
tell application "System Events"
click menu item 3 of menu 1 of menu bar item 4 of menu bar 1 of process "System Preferences"
end tell
end if
@conscho
Copy link

conscho commented Mar 7, 2020

This doesn't seem to work under macOS Catalina anymore. Any advice on how this could be adapted?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment