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

macOS 14 Sonoma下如何通过编程方式控制屏幕保护程序的选中状态?

macOS 14 Sonoma下如何通过编程方式控制屏幕保护程序的选中状态?

兄弟,我完全懂你遇到的坑——在macOS 14 Sonoma里,原来靠defaults命令直接改屏保配置的老办法确实失效了,系统现在不再监听这个配置的变更来切换屏保。别慌,下面给你几个亲测可行的解决方案:

  • 方法一:用AppleScript模拟系统设置操作(最可靠)
    Sonoma的系统设置可以通过AppleScript操控,直接模拟用户选择屏保的操作,这是目前最稳定的方式。比如要切换到「Drift」屏保,可用这段脚本:
tell application "System Settings"
    activate
    set current pane to pane id "com.apple.ScreenSaver-Settings.extension"
    delay 1 -- 给系统设置一点加载时间,可根据机器性能调整
    tell application "System Events"
        tell window "Screen Saver" of application process "System Settings"
            -- 精准匹配屏保名称,避免因列表位置变化失效
            select (first row of table 1 of scroll area 1 of group 1 of group 2 whose value of static text 1 is "Drift")
        end tell
    end tell
    quit -- 操作完成后退出系统设置
end tell

⚠️ 注意:运行这个脚本的应用(比如终端、Script Editor)需要被授予「辅助功能」权限,你可以在「系统设置」→「隐私与安全性」→「辅助功能」里找到对应的应用并勾选。如果是第三方屏保,只需要把 "Drift" 替换成该屏保在系统设置里显示的名称即可。

  • 方法二:命令行调用AppleScript(适合shell脚本集成)
    如果要在shell脚本里直接执行,用osascript命令调用上面的AppleScript就行:
osascript -e 'tell application "System Settings"
    activate
    set current pane to pane id "com.apple.ScreenSaver-Settings.extension"
    delay 1
    tell application "System Events"
        tell window "Screen Saver" of application process "System Settings"
            select (first row of table 1 of scroll area 1 of group 1 of group 2 whose value of static text 1 is "Drift")
        end tell
    end tell
    quit
end tell'

同样需要给终端开启辅助功能权限。

  • 方法三:结合defaults命令重启相关进程(不稳定但可选)
    虽然直接改defaults不会立刻生效,但修改后重启屏保相关进程,偶尔能触发系统读取新配置:
    先执行你的原始命令:
defaults -currentHost write com.apple.screensaver moduleDict -dict moduleName Drift path /System/Library/Screen\\ Savers/Drift.saver type 0

然后重启进程:

killall ScreenSaverEngine
killall System Settings

⚠️ 这个方法因为Sonoma的沙盒机制,有时候会不生效,所以更推荐前两种方法。

备注:内容来源于stack exchange,提问作者Hersh Bhargava

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.21 10:43:18