I want to monitor the output of defaults read to detect changes in the preferences of macOS and apps without user interaction, and then show the corresponding defaults write commands that would have the same effect. I would like to improve my workflow for declaratively configuring my settings, as follows:
- Start the monitoring tool.
- Change preferences in macOS or apps.
- The tool shows
defaults writecommands. - Copy and paste the commands into my setup script. I found a script that could be used as a starting point: macos-defaults. However, it doesn't exactly do what I want as it prompts for pressing a key and doesn't show defaults write commands.
My ideal tool would be analogous to Karabiner-EventViewer, which solves a different problem but in a similar manner.
How can I create a macOS monitoring tool that detects preference changes and generates the appropriate defaults write commands? Any guidance, libraries, or existing solutions that could help me achieve this would be greatly appreciated.
By the way, defaults read? More like defaults dread, am I right?
Depends on your goal and restrictions.
Can you monitor how the user modify preferences by executing the
defaults write? - Yes.You will need to have an EndpointSecurity system extension / daemon running to do this, which requires you to get custom entitlements from Apple.
Is executing the commandline defaults utility the only way to change preferences? - No
Can you monitor any changes in apps preferences, including CFPreferences API and etc?
I would say, with SIP enabled - No, because there are preferences daemon/agents stores preferences cache, and the communication between client and such processes most likely the xpc - there are no allowed interactions with such IPC from Apple.
Can you achieve this with disabled SIP? Yes, write the driver, inject some hook library into process, hook preferences API or whatever.