Last active
June 21, 2022 03:29
-
-
Save sidneys/2def36379833021dcd00ef694379f629 to your computer and use it in GitHub Desktop.
JavaScript for Automation (JXA) | Toggle macOS Accessibility Keyboard
This file contains hidden or 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 | |
* javascript for automation (jxa) | |
* | |
* 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.2 | |
* license: MIT | |
*/ | |
// Init | |
'use strict' | |
// Persist start-up state of the "System Preferences" app | |
var isRunningSystemPreferences = Application('System Preferences').running() | |
// Initialize storage for the checkbox value | |
var initialCheckboxValue | |
var currentCheckboxValue | |
// Show "Keyboard" pane within the Accessibility preferences | |
Application('System Preferences').panes.byId('com.apple.preference.universalaccess').anchors[1].reveal() | |
// Start automated interaction | |
var appSE = Application('System Events') | |
// Wait for: System Preferences Window | |
while (appSE.processes.byName('System Preferences').windows[0].tabGroups.length !== 1) {} | |
// Wait for: Settings Pane | |
while (appSE.processes.byName('System Preferences').windows[0].tabGroups[0].radioButtons[1].name() !== 'Accessibility Keyboard') {} | |
// Select the "Accessibility Keyboard" Segmented Control | |
appSE.processes.byName('System Preferences').windows[0].tabGroups[0].radioButtons[1].click() | |
// Tick the "Enable Accessibility Keyboard" checkbox, remembering its before/after value | |
initialCheckboxValue = appSE.processes.byName('System Preferences').windows[0].tabGroups[0].checkboxes[0].value() | |
appSE.processes.byName('System Preferences').windows[0].tabGroups[0].checkboxes[0].click() | |
currentCheckboxValue = appSE.processes.byName('System Preferences').windows[0].tabGroups[0].checkboxes[0].value() | |
// Did the checkbox value change? | |
if (initialCheckboxValue === currentCheckboxValue) { | |
// No – Wait for: confirmation dialog | |
while (appSE.processes.byName('System Preferences').windows[0].sheets.length === 0) {} | |
// Dismiss dialog | |
appSE.processes.byName('System Preferences').windows[0].sheets[0].buttons[0].click() | |
} | |
// Did the "System Preferences" app run on start-up? | |
// Restore initial state of the "System Preferences" application | |
if (!isRunningSystemPreferences) { | |
// No – Quit | |
Application('System Preferences').quit() | |
} else { | |
// Yes – Return to the overview screen | |
appSE.processes.byName('System Preferences').menuBars[0].menuBarItems[3].menus[0].menuItems[2].click() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment