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'''
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
source
share