views:

34

answers:

1

Does anyone know of a way to query OS X in order to find out how much time is actually left before it enters either system sleep or activates the display sleep (or even disk sleep), either via the command line, or any other method (Ruby or Objective-C for instance)?

I thought something like pmset via the command line might have offered this information, but it appears to only show and update what the current settings are, rather than allow feedback of where in the cycle the OS currently is.

My requirement is that I currently have a Ruby script that I'd like to run only when I'm not using the machine and a simple 'whatcher script' would allow this, but what to 'watch' is the thing I need a little help with.

It seems like there should be a simple answer, but so far I've not found anything obvious. Any ideas?

A: 

Heres a bash script to show the commands, which are a little complicated. The idea is that the user sets the system sleep time in the energy saver preference pane. The system will actually go to sleep when no device attached to the computer has moved for that time. So to get the time until the system goes to sleep we need the difference between those 2 results.

#!/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
regulus6633
That's great. 'ioreg -c IOHIDSystem' was the key to it all.Many thanks.