在Mac上测试Python中的不活动

Chr*_*ord 5 python macos

有没有办法使用Python测试系统在Mac上闲置了多长时间?或者,做不到这一点,即使如果系统当前空闲?

回答

使用来自已接受的解决方案的信息,这是一个丑陋但功能强大且相当高效的工作功能:

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
Run Code Online (Sandbox Code Playgroud)

Chr*_*heD 2

未经测试(目前),但根据此线程,您可以解析以下输出

ioreg -c IOHID系统