Mac 定时重启 TouchBar 脚本(缓解闪烁问题)
•
移动开发
背景
Mac 笔记本 TouchBar 是真的脆啊,合盖使用一段时间就废了,右侧一直闪烁简直亮瞎眼 😂
经过观察,总结出闪烁规律如下:
- 工作状态:不断操作电脑时,触控栏处于工作状态,几乎不闪烁;
- 中间状态:停止操作电脑一段时间后,触控栏变暗,闪烁较明显;
- 闲置状态:停止操作电脑直到熄屏,TouchBar 不再显示内容,却闪烁剧烈;
因此,网上提出一种定时重启 TouchBar,防止其进入休眠的方法,从而规避其闪烁问题。
创建重启 TouchBar 脚本
打开 「脚本编辑器」,编写重启 TouchBar 脚本
set idleTime to (do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print $NF; exit}'") as integer
if idleTime is greater than 4.5E+10 then
do shell script "pkill TouchBarServer" user name "USERNAME" password "PASSWORD" with administrator privileges
end if
上述代码,首先获取电脑闲置的时间,如果超过 45 秒,则重启 TouchBar。
注意,需要替换 USERNME 为你电脑的用户名,PASSWORD 为你电脑的密码。

点击「文件」=>「导出」,将上述代码导出为脚本,存储路径可以放到任意位置,例如 /Users/xxx/TouchBar/TouchBar.scpt,其中 xxx 为你的用户名。
创建定时任务
打开 Terminal 终端命令行,执行如下代码:
进入任务脚本目录
cd /Library/LaunchDaemons
创建配置文件
sudo vi my.touchbar.cron.plist
贴入如下内容
Label
my.touchbar.cron.plist
ProgramArguments
osascript
/Users/xxx/TouchBar/TouchBar.scpt
StartInterval
30
StandardInPath
/Users/xxx/TouchBar/log/run-in-drink.log
StandardOutPath
/Users/xxx/TouchBar/log/run-out-drink.log
StandardErrorPath
/Users/xxx/TouchBar/log/run-err-drink.log
大概意思就是每 30 秒执行一次 /Users/xxx/TouchBar/TouchBar.scpt
执行如下命令,加载定时任务
launchctl load my.touchbar.cron.plist
检查任务是否已成功加载(以下命令显示出 my.touchbar.cron.plist 即证明加载成功)
launchctl list | grep touchbar
如果需要停止定时任务,可以通过如下命令卸载
launchctl unload my.touchbar.cron.plist
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://www.net2asp.com/9152f4ff25.html
