检查显示器是否在Applescript中处于休眠状态 [英] Check if display is sleeping in Applescript
问题描述
我正在尝试检查显示器是否在休眠,然后在AppleScript中执行一些代码.
I'm trying to check if the display is sleeping, then execute some code in AppleScript.
到目前为止,我已经尝试过此操作-
So far I've tried this-
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/确定macs屏幕状态使用applescript
但是display_sleep_state不包含睡眠或唤醒状态.
But display_sleep_state does not contain sleeping or awake.
我也尝试过这个,除了设置变量外,几乎是一样的
I also tried this, which is pretty much the same except it sets the variables
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
但这也行不通,并且会在说它处于睡眠状态或随机唤醒之间进行切换.
But that also does not work and will switch between saying it's sleeping or awake randomly.
有人知道该怎么做吗?
推荐答案
首先,当您使用"do shell script"命令时,结果将以文本形式返回给您.因此,您的if语句必须检查一些文本.因此,您的if语句应如下所示...请注意引号使单词变成文本.
First of all, when you use a "do shell script" command the results are given back to you as text. Therefore your if statement must check for some text. As such your if statement should look like this... notice the quotes making the word sleeping into text.
if display_sleep_state contains "sleeping" then
接下来,您需要知道从"do shell script"命令返回的文本中是否包含"sleeping".因此,单独运行该命令,然后查看返回的文本.查看当显示器处于清醒状态还是睡眠状态时文本如何变化,您会在if语句中找到可用于确定显示状态的单词.我没有显示器,所以我无法为您提供帮助.祝你好运.
Next, you need to know if "sleeping" is in the text returned from from the "do shell script" command. So run that command by itself and look at the text you get back. Look how the text changes when a display is awake versus sleeping and you'll find words you can use in your if statement to determine the display state. I don't have a sleeping display so I can't help you with that. Good luck.
do shell script "ioreg -n IODisplayWrangler |grep -i IOPowerManagement"
这篇关于检查显示器是否在Applescript中处于休眠状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!