检查Applescript中的显示是否正在休眠

时间:2015-08-31 20:52:10

标签: 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

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

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

任何人都知道怎么做?

1 个答案:

答案 0 :(得分:0)

首先,当您使用“do shell script”命令时,结果将作为文本返回给您。因此,您的if语句必须检查一些文本。因此,你的if语句看起来应该是这样的......注意引号使得单词睡眠成文本。

if display_sleep_state contains "sleeping" then

接下来,您需要知道“do shell script”命令返回的文本中是否存在“sleep”。所以自己运行该命令并查看你得到的文本。看看当显示器处于清醒状态与睡眠状态时文本如何变化,您将在if语句中找到可用于确定显示状态的单词。我没有睡觉的显示器,所以我无法帮助你。祝你好运。

do shell script "ioreg -n IODisplayWrangler |grep -i IOPowerManagement"
相关问题