如何通过命令行在Mac系统中反转屏幕颜色?
实现屏幕颜色反转的脚本方案
屏幕颜色反转与深色主题是独立的系统功能,你之前使用的命令仅用于切换深色模式。以下是无需注销即可生效的屏幕颜色反转脚本实现方法:
AppleScript 一键切换(推荐)
该脚本会自动打开系统偏好设置的辅助功能面板,切换反转颜色状态后自动关闭面板,全程无需手动操作:
tell application "System Events" tell preferences of application "System Preferences" set current pane to pane id "com.apple.preference.universalaccess" end tell tell application process "System Preferences" set invertCheckbox to UI element "Invert colors" of scroll area 1 of window "Accessibility" set invertEnabled to value of attribute "AXEnabled" of invertCheckbox set value of attribute "AXEnabled" of invertCheckbox to not invertEnabled end tell quit application "System Preferences" end tell
你也可以将其压缩为单行命令,直接在终端执行:
osascript -e 'tell application "System Events" to tell preferences of application "System Preferences" to set current pane to pane id "com.apple.preference.universalaccess"' -e 'tell application "System Events" to tell application process "System Preferences" to set invertCheckbox to UI element "Invert colors" of scroll area 1 of window "Accessibility"' -e 'tell application "System Events" to tell application process "System Preferences" to set invertEnabled to value of attribute "AXEnabled" of invertCheckbox' -e 'tell application "System Events" to tell application process "System Preferences" to set value of attribute "AXEnabled" of invertCheckbox to not invertEnabled' -e 'tell application "System Events" to quit application "System Preferences"'
命令行结合 defaults 切换
通过修改系统配置文件并重启相关进程实现切换,适合批量脚本场景:
# 切换反转颜色状态(取反当前值) defaults write com.apple.universalaccess invertColors -bool $(defaults read com.apple.universalaccess invertColors | tr -d '\n' | sed 's/1/0/;s/0/1/') # 重启系统事件进程使更改生效 killall System Events killall CoreServicesUIAgent
注意事项
- 运行上述脚本需要给终端(或执行脚本的工具)赋予辅助功能权限:打开「系统偏好设置」→「安全性与隐私」→「隐私」→「辅助功能」,将终端添加到列表中。
- 颜色反转与深色模式可同时启用,可根据实际需求组合使用。
内容的提问来源于stack exchange,提问作者Pepe Segura
相关产品推荐
相关产品推荐

