如何在 Dock 首选项中更改 "minimize-to-application" 的值

How Change value of "minimize-to-application" in Dock Preferences

搜索 CoreDockPreferences,我找到了这些私有系统 API coredock.h

我正在尝试使用 CoreDockSetPreferences 更改最小化到应用程序的值,但它不起作用

下面是我在 Obj-c 中的接口和实现:

@interface Symbolic : NSObject

extern void CoreDockSetPreferences(CFDictionaryRef preferenceDict);

- (void) setDictionaryDock: (CFDictionaryRef)dockDict;

@end

@implementation Symbolic

-(void) setDictionaryDock: (CFDictionaryRef)dockDict
{
    CoreDockSetPreferences(dockDict);
}

下面是我调用和传递 dock.plist 字典的方式 Swift:

func dockPrefChange(){
    if let dir = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first {
        let dockPlistPath = dir.appendingPathComponent("Preferences", isDirectory: true).appendingPathComponent("com.apple.dock.plist", isDirectory: false)
        guard let dockDictionary = NSMutableDictionary(contentsOf: dockPlistPath) else {return}
        dockDictionary["minimize-to-application"] = 1
        Symbolic().setDictionaryDock(dockDictionary as CFDictionary)
    }
}

谢谢Willeke你是对的。

DockDict 不是应该的。 阅读更多关于,我回答这个问题。

基本上我遵循 coredock.h

中的注释“序列用法”
// Example usage
/*
    CFArrayRef d = CFStringCreateArrayBySeparatingStrings(kCFAllocatorDefault, CFSTR("showRecents,showProcessIndicators,minimizeToApplication"), CFSTR(","));
    CFDictionaryRef dic;
    CoreDockCopyPreferences(d, &dic, nil);
 */

并重新创建如下代码:

@implementation Symbolic

- (NSDictionary*) getDictionaryDock
{
    CFArrayRef d = CFStringCreateArrayBySeparatingStrings(kCFAllocatorDefault, CFSTR("showRecents,showProcessIndicators,minimizeToApplication"), CFSTR(","));
    CFDictionaryRef dic;
    CoreDockCopyPreferences(d, &dic, nil);
    NSDictionary *nsDictionary = (__bridge NSDictionary*)dic;
    return nsDictionary;
}

- (void) setDictionaryDock: (NSDictionary*) newDictionary
{
    CFDictionaryRef dict = (__bridge CFDictionaryRef)newDictionary;
    CoreDockSetPreferences(dict);
}

并在 Swift 中:

public func setMinimizeToApp(value: Bool){
    if var dict = symbolic.getDictionaryDock() as? [String: Bool] {
        if dict["minimizeToApplication"] != value{
            dict["minimizeToApplication"] = value
            symbolic.setDictionaryDock(dict)
        }
    }
}