如果您二次单击 Dock,则可以单击 Turn Hiding On
选项以自动隐藏 Dock。或者,您可以转到 System Preferences > Dock
并单击 Automatically hide and show the Dock
。
我想在我正在制作的应用程序(基本上是一个状态栏图标应用程序)中模仿该功能,最好是在 Swift 中。
到目前为止,我编写的用于打开 Dock 自动隐藏功能的代码如下:
// Update the value for key "autohide" in com.apple.dock.plist, located in ~/Library/Preferences/.
var dict = NSUserDefaults.standardUserDefaults().persistentDomainForName("com.apple.dock")
dict.updateValue(true, forKey: "autohide")
NSUserDefaults.standardUserDefaults().setPersistentDomain(dict, forName: "com.apple.dock")
// Send notification to the OS.
dispatch_async(dispatch_get_main_queue()) {
CFNotificationCenterPostNotification(CFNotificationCenterGetDistributedCenter(), "com.apple.dock.prefchanged", nil, nil, true)
}
代码的第一部分更新了 plist 文件中的一个值,我已经确认它正在运行。第二部分向操作系统发送通知,告诉它该 plist 中的一个值已更改,我也确认它正在工作。
但是,这两件事并没有让 Dock 隐藏起来,让我相信我需要做其他事情。还是我对问题的处理方式有误?如何让 Dock 开始隐藏?
PS:我读过一些关于名为 CoreDock 的私有(private)的、未记录的 API 的内容,但我想避免那样做,因为它可能会导致很多问题......
最佳答案
几乎可以肯定,使用 AppleScript 或 Scripting Bridge 来执行此操作会更好。以下脚本打开 Dock 自动隐藏:
tell application "System Events"
set autohide of dock preferences to true
end tell
您可以使用 NSAppleScript
运行它。
关于swift - 从应用程序中以编程方式停靠 "Turn Hiding On",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34184927/