Here's the GUI scripting solution:
tell application "System Preferences"
reveal anchor "Mouse" of pane id "com.apple.preference.universalaccess"
tell application "System Events"
tell process "System Preferences"
tell first window
tell first tab group
tell radio group 1
if value of radio button 1 is 1 then
# enabled, so disable
click radio button 2
else
# disabled, so enable
click radio button 1
end if
end tell
end tell
end tell
end tell
end tell
quit
end tell
It will launch System Preferences if required, but doesn't show the window and quits it afterwards, so from a UI POV, it at least doesn't show the window.
You can change the persistent setting on which the System Preferences configuration is based using the following commands:
defaults write ~/Library/Preferences/com.apple.universalaccess mouseDriver -bool YES
defaults write ~/Library/Preferences/com.apple.universalaccess mouseDriver -bool NO
Unfortunately, this only changes the configuration file and does not apply the changes to the running system.
While you can programmatically press Option keys using AppleScript, like the following:
tell application "System Events"
key down option
key up option
end tell
The press Option five times to toggle setting is at a higher level and doesn't get triggered by this. While all regular Option
key presses are ignored after you only run key down option
, you can still toggle Mouse Keys.
Debugging System Preferences while changing the setting shows calls to LaunchUAServerIfNeeded
and UAMouseKeysSetEnabled
in the private UniversalAccessCore
framework. Unless we reverse engineer that framework and write our own binaries, I don't think we'll get a proper programmatic way to do this.
As best as I've been able to tell, this feature has been removed.
There are two alternatives to the Lion-provided notification mechanism that provide a few more features and customizability.
If you have Growl installed, you can use GrowlTunes to provide Growl notifications on track and playback changes.
The official Last.fm Scrobbler app provides notifications via Growl as well, the bonus being that it's worth using if you also scrobble your playback history to Last.fm. If you don't you probably want to stick with GrowlTunes.
Growl is a generic notification system with a large amount of customizability, like the ability to customize the alert style. Growl has existed for OS X for as long as I can remember, years and years pre-dating even Tiger, iOS, and the Notification Center as well.
Best Answer
Unfortunately, changing system preference files will always require administrative permissions, which is something the user has to explicitly give your script.
Fortunately, there is a way to accomplish this without changing system preference files! Using AppleScript, one can ask the system event daemon to change the system appearance.
I'm testing this on my work laptop, which doesn't allow me to change Accessibility preferences, so I don't even know if Terminal is enabled there (I suspect it's not). Additionally, this does not do any GUI scripting! (I recorded the below gif with System Preferences not running) Instead, it performs the same action that checking the box in System Preferences performs, but without needing access to that checkbox.
Hope this works for you!
AppleScript
Bash
Don't wanna have something kick off an AppleScript? I'm with you. You can execute any AppleScript from bash (and thus the terminal) passing it as a string to the
osascript -e
command:I have made a proof-of-concept app that shows off this technique, plus a couple others:
macOS Dark Mode Switcher