How can I change the Dock preferences programmatically?

前端 未结 3 974
庸人自扰
庸人自扰 2021-01-07 06:03

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

3条回答
  •  花落未央
    2021-01-07 06:57

    With the magic of the Xcode debugger and some formatted disassembly, I've created this short header file you can paste into your code (GitHub gist is here). Function names are hopefully self-explanatory.

    // TO USE THESE INTERFACES, you MUST link against ApplicationServices.framework.
    
    #pragma once
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
        // Boolean preferences
        extern void CoreDockSetLaunchAnimationsEnabled(bool enable);
        extern void CoreDockSetMagnificationEnabled(bool enable);
        extern void CoreDockSetAutoHideEnabled(bool enable);
        extern void CoreDockSetMinimizeInPlace(bool enable);
    
        // Sets other preferences such as whether the indicators below the app icons are shown
        // 'preferenceDict' is a CFDictionary containing a magic key value
        // Will require further inspection of Dock.prefpane to find all the keys
        // (I haven't noted them down)
        extern void CoreDockSetPreferences(CFDictionaryRef preferenceDict);
    
    #ifdef __cplusplus
    } // extern "C"
    #endif
    

    Preferences updated in this way are reflected instantaneously, because these functions actually messagethe "com.apple.dock" mach service internally.
    Have fun!

    PLEASE NOTE: These are private system APIs. Any apps submitted to the Mac App Store that use these APIs will be rejected. On another note, if you have no App Store intentions, there's no harm in using these interfaces. They appear to have existed since the very dawn of Mac OS X, and it's highly improbable they'll be disappearing in the near future, if ever.

提交回复
热议问题