检查显示器是否在 Applescript 中休眠

Check if display is sleeping in Applescript

我正在尝试检查显示器是否正在休眠,然后在 AppleScript 中执行一些代码。

到目前为止我已经试过了-

set display_sleep_state to do shell script "ioreg -n IODisplayWrangler |grep -i IOPowerManagement"
if display_sleep_state contains sleeping then
   -- Display is asleep
else if display_sleep_state contains awake then
   -- Display is awake
else
   -- We don't know.
end if

我发现这里- https://superuser.com/questions/182018/determine-macs-screen-state-using-applescript

但display_sleep_state不包含睡眠或清醒。

我也试过这个,除了设置变量外几乎一样

delay 6 -- test with display awake, then test with display asleep (display sleep hotkeys are ctrl+shift+eject)
set sleeping to 1
set awake to 4
set display_sleep_state to do shell script "ioreg -n IODisplayWrangler |grep -i IOPowerManagement"
if display_sleep_state contains sleeping then
   say "display asleep"
else if display_sleep_state contains awake then
   say "display awake"
else

   say "unknown"
end if

来源- http://macscripter.net/viewtopic.php?id=25003

但这也不起作用,会随机切换说它正在睡觉或醒来。

有人知道怎么做吗?

首先,当您使用 "do shell script" 命令时,结果将以文本形式返回给您。因此,您的 if 语句必须检查某些文本。因此,您的 if 语句应如下所示...请注意引号使单词 sleeping into text。

if display_sleep_state contains "sleeping" then

接下来,您需要知道 "sleeping" 是否在从 "do shell script" 命令返回的文本中。所以 运行 该命令本身并查看您返回的文本。查看当显示器处于唤醒状态与睡眠状态时文本如何变化,您会发现可以在 if 语句中使用的词来确定显示状态。我没有睡眠显示器,所以我无法帮助你。祝你好运。

do shell script "ioreg -n IODisplayWrangler |grep -i IOPowerManagement"