Inactivity testing in Python on Mac

Is there a way to check, using Python, how long the system has been idle on a Mac? Or if this is not possible, even if the system is currently down?

Answer

Using the information from the decision made, here is an ugly, but functional and sufficiently effective function for the task:

from subprocess import *

def idleTime():
    '''Return idle time in seconds'''

    # Get the output from 
    # ioreg -c IOHIDSystem
    s = Popen(["ioreg", "-c", "IOHIDSystem"], stdout=PIPE).communicate()[0]
    lines = s.split('\n')

    raw_line = ''
    for line in lines:
        if line.find('HIDIdleTime') > 0:
            raw_line = line
            break

    nano_seconds = long(raw_line.split('=')[-1])
    seconds = nano_seconds/10**9
    return seconds
+5
source share
1 answer

Disabled (for now), but according to this thread you can analyze the output

ioreg -c IOHIDSystem

0
source

All Articles