Changing Universal Access preferences

Hi there,

I would like to change the increaseContrast option within a script. I use this shell command:

defaults write com.apple.universalaccess increaseContrast -bool true

On Mojave I don’t have the permissions anymore and so I’ve to add sudo or do shell script with administrator privileges.

The problem is, that unlike using the checkbox in the system preferences, running apps seem not to be informed about this changed preference. Is there a way to tell it to them so they change their appearance?

Is it possible with ASObjC? I’ve found NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification in the docs, but I don’t know how to use it.

I don’t like your chances, but you could try this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set theNotif to current application's NSNotification's notificationWithName:(current application's NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification) object:(missing value)
current application's NSDistributedNotificationCenter's defaultCenter()'s postNotification:theNotif

No, that’s wrong. That notification can only be sent by a special function, and you need to specify the target. I fear you’re out of luck.

As there seems to be no other way, have you simply considered scripting System Preferences and System Events to do this ? I normally abhor UI scripting, but System Preferences is the one exception for which it lends itself very cleanly to.

Hi there,

as I hate UI scripting I tried other stuff. Now I’ve a solution I can live with. It uses the keyboard shortcut for inverting the screen colors. This will refresh all windows with the current contrast mode setting.

if (do shell script "defaults read com.apple.universalaccess increaseContrast; true") is "1" then
	do shell script "defaults write com.apple.universalaccess increaseContrast -bool false"
else
	do shell script "defaults write com.apple.universalaccess increaseContrast -bool true"
end if

tell application "System Events"
	-- Toggle Invert Colors on German keyboard
	keystroke "{" using {command down, option down, control down}
	keystroke "{" using {command down, option down, control down}
	-- Toggle Invert Colors on international keyboard
	keystroke 28 using {command down, option down, control down}
	keystroke 28 using {command down, option down, control down}
end tell

To let the script allow changing the prefs via shell script, you have to give the script full disc access in Mojave and later.

Hi Teki.

If you’re using the key numbers, the command is key code:

tell application "System Events"
	key code 28 using {command down, option down, control down}
	key code 28 using {command down, option down, control down}
end tell

Thanks, now it works universal. I got the snippet from Stack Overflow. :wink: