13

How can I change the Dock preferences programmatically?

 2 years ago
source link: https://www.codesd.com/item/how-can-i-change-the-dock-preferences-programmatically.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How can I change the Dock preferences programmatically?

advertisements

I'm new to Cocoa/macOS programming. I just found out that NSUserDefaults can be used to change application/system settings, like the way the defaults command does.

But I don't know how to cause these settings to update.

e.g., I use NSUserDefaults to set the dock size to 32, and synchronize the setting. But the dock retains the old setting.

Even when I use defaults write com.apple.dock tilesize 32 to change its size, it won't be updated until I logout and login.

Is there any other technology to notify the dock to get the update? I know that System Preferences can do that.

Thank you!


The Mac OS X dock doesn't reload its settings until it is killed and restarted. In the same way that you'd have to change its settings manually via the terminal (defaults write com.apple.dock tilesize 32; killall Dock), you have to do that in code. So, while you've written the defaults portion of the code, you have to write the kill portion:

NSRunningApplication *dock = [NSRunningApplication runningApplicationWithBundleIdentifier:@"com.apple.dock"];
[dock terminate];

If you want to do this without killing the dock, sorry, but you're out of luck. While there might be a hidden API to force the dock to reload its settings on the fly, in all my searching I have never found any hints of how one can do this (there are no notifications posted on the hidden distributed notification center that most applications to interact with one another).


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK