2010-08-17 81 views
2

有没有人知道一种方法来查询OS X,以便查明在进入系统睡眠或通过命令行或任何其他方式激活显示器睡眠(或甚至磁盘睡眠)之前实际剩下多少时间方法(例如Ruby或Objective-C)?OS X直到(系统/显示器/磁盘)睡眠的时间?

我以为像pmset通过命令行可能提供了这些信息,但它似乎只显示和更新当前设置是什么,而不是允许反馈当前操作系统在循环中的位置。

我的要求是,我目前有一个Ruby脚本,我只想在不使用机器的情况下运行,并且一个简单的'whatcher脚本'会允许这样做,但'看'的是什么我需要一点帮助。

这似乎应该有一个简单的答案,但到目前为止我还没有发现任何明显的。有任何想法吗?

回答

5

继承人一个bash脚本来显示命令,这有点复杂。这个想法是,用户在节能器首选项窗格中设置系统休眠时间。当没有设备连接到计算机时,系统实际上会进入休眠状态。所以为了得到系统进入睡眠的时间,我们需要这两个结果之间的差异。

#!/bin/bash 

# this will check how long before the system sleeps 
# it gets the system sleep setting 
# it gets the devices idle time 
# it returns the difference between the two 
# 
# Note: if systemSleepTimeMinutes is 0 then the system is set to not go to sleep at all 
# 

systemSleepTimeMinutes=`pmset -g | grep "^[ ]*sleep" | awk '{ print $2 }'` 

if [ $systemSleepTimeMinutes -gt "0" ]; then 
    systemSleepTime=`echo "$systemSleepTimeMinutes * 60" | bc` 
    devicesIdleTime=`ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print $NF/1000000000; exit}'` 
    secondsBeforeSleep=`echo "$systemSleepTime - $devicesIdleTime" | bc` 
    echo "Time before sleep (sec): $secondsBeforeSleep" 
    exit 0 
else 
    echo "The system is set to not sleep." 
    exit 0 
fi 
+0

太好了。 'ioreg -c IOHIDSystem'是这一切的关键。 非常感谢。 – timecode 2010-08-20 15:57:39