Last active
May 26, 2021 15:08
AppleScript | Toggle Accessibility Keyboard (macOS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
* 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't seem to work under macOS Catalina anymore. Any advice on how this could be adapted?