我有一个 Bash 脚本,可以在 Mac 进入睡眠模式时执行某些操作。该脚本在我的配备 Intel 芯片的 Mac 上运行良好,但使用 Apple Silicon 时无法检测它是否处于睡眠状态。
这是脚本的一部分:
commandSleepStatus='echo $(ioreg -n IODisplayWrangler | grep -i IOPowerManagement | perl -pe '\''s/^.*DevicePowerState"=([0-9]+).*$/\1/'\'')/4 | bc'
sleepStatus=$(eval "$commandSleepStatus")
# echo "$sleepStatus"
# Set isRunning to false if the sleep is active
if [ "$sleepStatus" -eq 0 ]; then
isSleep=true
fi
问题出在 ioreg -n IODisplayWrangler | grep -i IO 电源管理。 Apple 似乎已从 Apple Silicon 上的 ioreg 中删除了 IOPowerManagement。
IODisplayWrangler <class IODisplayWrangler, id 0x1000002a1, registered, matched, active, busy 0 (0 ms), retain 5>
| | {
| | "IOClass" = "IODisplayWrangler"
| | "CFBundleIdentifier" = "com.apple.iokit.IOGraphicsFamily"
| | "IOProviderClass" = "IOResources"
| | "IOGraphicsIgnoreParameters" = {"aupc"=Yes,"auph"=Yes," bpc"=Yes,"aums"=Yes,"aupp"=Yes}
| | "IOGraphicsPrefsParameters" = {"thue"=Yes,"pscn"=Yes,"vbst"=Yes,"oscn"=Yes,"tbri"=Yes,"cyuv"=268435456,"tsat"=Yes}
| | "IOResourceMatch" = "IOKit"
| | "IOProbeScore" = 0
| | "IOMatchCategory" = "IOGraphics"
| | "IOMatchedAtBoot" = Yes
| | "IOGeneralInterest" = "IOCommand is not serializable"
| | "IOPersonalityPublisher" = "com.apple.iokit.IOGraphicsFamily"
| | "CFBundleIdentifierKernel" = "com.apple.iokit.IOGraphicsFamily"
| | }
我的问题是是否有另一种方法可以达到同样的效果。
好吧,也许我找到了解决方案。
对于
Apple Silicon
,IOPowerManagement
部分内没有 IODisplayWrangler
。我不得不以另一种方式寻找信息,我发现system_profiler SPDisplaysDataType
这里,其实有关于集成显示器和连接到Mac的外接显示器的信息。当显示屏进入待机状态时,会出现一条附加行 Display Asleep: Yes
,显示屏打开时不会出现该行。
在图像中您可以看到差异:
因此,我编写了以下脚本,我将在此处重写该脚本,以防将来有人需要此信息。
# Obtein the output of system_profiler SPDisplaysDataType
display_info=$(system_profiler SPDisplaysDataType)
# Count the number of "Display Asleep: Yes" and "Display Asleep: No"
count_asleep=$(echo "$display_info" | grep -c "Display Asleep: Yes")
count_awake=$(echo "$display_info" | grep -c "Display Asleep: No")
# If there is "Display Asleep: Yes", print "true", else "false"
if [ $count_asleep -gt 0 ] && [ $count_awake -eq 0 ]; then
isSleep=true
else
isSleep=false
fi
脚本检查所有文本并查看行
Display Asleep: Yes
是否存在。如果存在,则打印 true
,如果 2 个中的一个是 Display Asleep: No
(现在没有必要,但也许将来会添加),或者如果不存在,则打印 false
。