如何在MacOS系统中通过Terminal设置Screen Time屏幕使用时间密码
macOS 10.15.7 终端设置Screen Time密码的实现方案
前置准备
- 给终端开启完全磁盘访问权限:打开「系统偏好设置」>「安全与隐私」>「隐私」标签页,左侧列表找到「完全磁盘访问」,点击左下角解锁后勾选「终端」,重启终端后生效。
- 同步开启辅助功能权限:同样在「隐私」标签页左侧找到「辅助功能」,勾选「终端」,避免UI操作权限不足报错。
- 提前手动开启一次屏幕使用时间功能,避免脚本执行时出现初始化异常。
核心实现方法
你可以通过AppleScript调用系统UI完成密码设置,该方案适配10.15.7版本,不需要篡改系统底层配置,稳定性更高,可直接在终端执行,也可以封装到你已有的Python/C定时逻辑中调用。
将下方命令中的OLD_PASS替换为上一次设置的密码、1234替换为本次要设置的随机4位密码即可运行:
osascript -e 'tell application "System Preferences" activate reveal pane id "com.apple.preference.screentime" delay 1 tell application "System Events" to tell process "System Preferences" click button "选项…" of window 1 delay 0.5 if exists button "更改密码…" of sheet 1 of window 1 then -- 已有旧密码的场景 click button "更改密码…" of sheet 1 of window 1 delay 0.5 keystroke "OLD_PASS" keystroke tab keystroke "1234" keystroke tab keystroke "1234" keystroke return delay 0.5 keystroke return else -- 首次设置密码的场景 click checkbox "使用屏幕使用时间密码" of sheet 1 of window 1 delay 0.5 keystroke "1234" keystroke tab keystroke "1234" keystroke return delay 0.5 keystroke return end if end tell quit application "System Preferences" end tell'
注意事项
- 脚本中的
delay值可根据设备运行速度调整,设备响应慢的话适当调大数值,避免UI未加载完成就执行下一步导致出错。 - 你可以在自己已有的逻辑中,先读取上一次生成的密码文本文件获取旧密码、生成新的4位随机密码,替换掉命令中的对应字段后执行即可。
- 运行脚本时不要操作系统偏好设置窗口,避免打断脚本执行流程。
内容的提问来源于stack exchange,提问作者user676464327
相关产品推荐
相关产品推荐

