How to script the keyboard backlight settings

defaultskeyboardmojaveplistpreferences

I'm looking to change settings with a script, but I can't find where the plist data for the keyboard backlight settings is stored.

For example, in the System Preferences, I'd like to modify the preferences in the Keyboard section with a script. These ones:

enter image description here

Normally how I find a setting is

  • I run defaults read > before and defaults read -g > before-g
  • Then I change the setting in System Preferences
  • Then I run defaults read > after and defaults read -g > after-g
  • Then I compare the differences from the before files to the after files to see what changed.

But for some reason, this doesn't work with the keyboard backlight settings, and I see no difference from before files to after files when I've changed the keyboard backlight settings in between before/after.

How can I script the keyboard backlight settings?

Best Answer

Short answer, TLDR: No, a simple script will not suffice for what you are trying to do. But ...

You will have better luck using a third party like this one: https://github.com/pirate/mac-keyboard-brightness. His code can take user input and manipulate the keyboard for you. But I am not sure if this was tested in Mojave but you are welcome to try.

Long Answer, for the curious:

Certain system settings or running hardware tasks on a Mac are initiated and ran with kernel extensions and continue to run with the kextd daemon. What drivers are for Windows are how kernel extensions are for Mac OS. Without kernel extensions your Mac OS would not boot properly.

You can safely test with your sound on your Mac using information about removing kexts here, but be sure to back up your files: http://osxdaily.com/2012/05/30/how-to-completely-disable-audio-sound-in-mac-os-x/

Take a look the code written there by Pirate, in his code in keyboard-brightness.c he does a good job dissecting the Mac. You can see how a bit more complex your script would be if you tried to tackle this through code. A simple bash script would not suffice.

In Pirate's code you can see that he accesses the keyboard light with the Foundation framework and the IOKit framework, written in Objective C/C++. If you are comfortable with this language then you should try to dabble more with his code.

Why running defaults didn't catch anything? The reason defaults doesn't show anything is because defaults is meant to edit plist files only.

How can I see my kernel extensions? You can see a full list of kernel extensions currently running in terminal with the following command:

kextstat

You can also see a list of all your installed kernel extensions under:

/System/Library/Extensions

In terminal you can see which keyboard kext file you have by running grep:

kextstat | grep "Keyboard"

I do not have a laptop, but I am safely assuming you will also have a keyboard kext file that will look similar to mine:

enter image description here

Where can I learn more about the IOFramework? https://developer.apple.com/documentation/iokit?language=objc. On Pirate's git page you can see he also wrote his code in swift4, which might be easier to understand. You can switch back and forth from ObjC and Swift language types on the Apple Developer Page.

I hope this answer was useful to you.