Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing for Inactivity in Python on Mac

Tags:

python

macos

Is there a way to test, using Python, how long the system has been idle on Mac? Or, failing that, even if the system is currently idle?

Answer

Using the information from the accepted solution, here is an ugly but functional and fairly efficient function for the job:

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
like image 606
Chris Redford Avatar asked Mar 11 '10 13:03

Chris Redford


People also ask

Does IDLE Python work on Mac?

Your best way to get started with Python on macOS is through the IDLE integrated development environment, see section The IDE and use the Help menu when the IDE is running. If you want to run Python scripts from the Terminal window command line or from the Finder you first need an editor to create your script.

What is Python IDLE Mac?

A File Editor Every programmer needs to be able to edit and save text files. Python programs are files with the . py extension that contain lines of Python code. Python IDLE gives you the ability to create and edit these files with ease.

How do I get IDLE time in python?

Call get_idle_duration() to get idle time in seconds.


1 Answers

Untested (for now), but according to this thread you could parse the output of

ioreg -c IOHIDSystem

like image 56
ChristopheD Avatar answered Oct 01 '22 20:10

ChristopheD