2010-03-11 28 views
5

有沒有一種方法可以使用Python測試系統在Mac上閒置了多久?或者,如果失敗,即使如果系統當前閒置?在Mac上測試不活動的Python

回答

使用從接受的解決方案的信息,這裏是工作中一個醜陋,但功能和相當有效的功能:

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 
+0

「空閒」是什麼意思?你如何定義它? – 2010-03-11 13:58:33

+0

無論系統定義爲「閒置」。即在制定屏幕保護程序或節能程序之前系統認爲空閒時間是什麼。我假設沒有鼠標或鍵盤移動會很好。 – 2010-03-11 14:17:40

回答

0

未經測試(現在),但according to this thread你可以解析的

輸出名爲ioreg -c IOHIDSystem

+0

好的,有效。在原始問題中發佈我的解決方案。感謝領先。 – 2010-03-11 15:46:33