You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何切换菜单栏(MenuBar)自动隐藏设置?如何编程读写该配置

读取菜单栏自动隐藏状态

你可以通过以下几种方式获取开关当前状态:

  • 命令行方式:打开终端执行如下命令
    defaults read com.apple.dock autohide-menubar
    
    返回1代表开启自动隐藏,返回0代表关闭自动隐藏。
  • AppleScript 方式:执行如下脚本即可直接获取状态
    tell application "System Events" to tell dock preferences to get autohide menu bar
    
    返回true为开启,false为关闭。
  • Swift 编程方式:
    import Foundation
    
    if let dockDefaults = UserDefaults(suiteName: "com.apple.dock") {
        let isAutoHideMenuBar = dockDefaults.bool(forKey: "autohide-menubar")
        print("菜单栏自动隐藏状态:\(isAutoHideMenuBar)")
    }
    

修改菜单栏自动隐藏状态

以上方法适用于 macOS Big Sur(11.0)及以上版本,更低版本系统的菜单栏隐藏设置对应键值可能存在差异。

  • 命令行方式:修改后会自动重启Dock使设置立即生效
    开启自动隐藏:
    defaults write com.apple.dock autohide-menubar -bool true && killall Dock
    
    关闭自动隐藏:
    defaults write com.apple.dock autohide-menubar -bool false && killall Dock
    
  • AppleScript 方式:无需手动重启Dock,系统会自动应用设置
    开启自动隐藏:
    tell application "System Events" to tell dock preferences to set autohide menu bar to true
    
    关闭自动隐藏:
    tell application "System Events" to tell dock preferences to set autohide menu bar to false
    
  • Swift 编程方式:
    import Foundation
    
    if let dockDefaults = UserDefaults(suiteName: "com.apple.dock") {
        // 传入true开启自动隐藏,传入false关闭自动隐藏
        dockDefaults.set(true, forKey: "autohide-menubar")
        dockDefaults.synchronize()
        
        // 重启Dock使设置生效
        let killProcess = Process()
        killProcess.executableURL = URL(fileURLWithPath: "/usr/bin/killall")
        killProcess.arguments = ["Dock"]
        try? killProcess.run()
    }
    

内容的提问来源于stack exchange,提问作者a_kira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 20:15:07