macOS根据当前屏幕亮度自动切换暗黑模式

macOS根据当前屏幕亮度自动切换暗黑模式

macos从Mojave支持系统暗黑模式,并且Mojave是不支持动态切换的,需要自己去系统偏好里设置。从catalina支持根据日出日落自动切换,但是平常工作的时候办公室都是开着灯的。。。So

自己搜索了下使用applescript写了个脚本,放在crontab里自动运行,纯粹闲的没事 瞎折腾。

#!/usr/bin/osascript
# 20210713
# 根据屏幕亮度自动设置外观模式,只支持Mojave以后的版本
# 运行方式终端运行 osascript auto_swap_dark_mode.scpt

# 查看当前外观是否为Dark模式,是则返回true
on current_dark_mode()
	tell application "System Events"
		tell appearance preferences
			get dark mode
		end tell
	end tell
end current_dark_mode

# 获取当前的屏幕亮度值(只输出主显示器)
# 依赖brightness工具
set current_screen to do shell script "/usr/local/bin/brightness -l 2>&1 |grep 'display 0: brightness' |awk '{print $NF}'"

(*
	1、根据当前的屏幕亮度值进行判断
	2、如果当前亮度值小于70%,且当前外观模式不是Dark则将当前外观设置为Dark模式
	3、如果当前亮度值大于70%,且当前外观模式为Dark则将当前外观设置为Light
*)
if current_screen < 0.7 and not current_dark_mode() then
	# say "将外观模式设置为Dark"
	tell application "System Events"
		tell appearance preferences
			set dark mode to true
		end tell
	end tell
end if

if current_screen > 0.7 and current_dark_mode() then
	# say "将外观模式设置为Light"
	tell application "System Events"
		tell appearance preferences
			set dark mode to false
		end tell
	end tell
end if

# 打印当前屏幕亮度值
do shell script "echo 当前屏幕亮度值为 " & current_screen
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值