Edit: my original question, below, assumes there is a secret copy (a cache) of the system preferences settings. I have since learned that is not the case. However, my problem still needed a solution, which I have found and also posted, below. No special Objective-C process is needed. /edit.
I want to use the defaults write command to change a checkbox in System Preferences. E.g:
defaults write ./com.apple.speech.synthesis.general.prefs TimeAnnouncementPrefs -dict-add TimeAnnouncementsEnabled -bool YES However, the OS caches the preference files, so the defaults command has no effect until the cache is updated. The System Preferences app updates the cache after a change is made in its UI -- so it is possible. Some people have suggested sudo killall cfprefsd but that does not work in Big Sur.
Elsewhere, user3439894 has posted a good AppleScript that manipulates the System Preferences UI. But such UI manipulation does not work when the Screen Saver is running.
I'd like to figure out how the System Preferences app manages to update the preferences cache. Doing strings of the System Preferences app (at /System/Applications/System\ Preferences.app/Contents/macOS/System\ Preferences) shows a method updateCacheFileWithPrefPane: that might do the job, but I don't know how to call that method. The method, whatever it is, is private, but I only need this for myself (not a public app).
Is it possible to use dtrace or dtruss to figure out what the System Preferences app is doing to signal the OS to update its cache for a particular checkbox? I want to write an Objective-C program to do the same thing, but I need a dtrace/dtruss expert to help me.
defaults(read or write), butdefaults writedoes not affect that cached copy (in Big Sur). However, when the checkbox is changed using the System Preferences app, the value seen bydefaults readAND the value seen by the OS are both changed, so the change takes effect. I want to usedefaults writeand then do whatever it is that the System Preferences app does to update the copy that the OS sees.tell application "System Events" to stop screen saversas the first line of code in the script. In my testing after you mentioned it in the linked answer, it worked for me. -- Another method I use is to use/usr/libexec/PlistBuddyinstead ofdefaults. Additionally either can be appended with; killall cfprefsdor; sudo killall cfprefsd, the latter of which needs to be done from an Admin account. If this doesn't work then rebooting will necessary.