检查显示器是否在 Applescript 中休眠 [英] Check if display is sleeping in Applescript

查看:31
本文介绍了检查显示器是否在 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-screen-state-using-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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆