-
Notifications
You must be signed in to change notification settings - Fork 0
/
activeAppTracker.py
executable file
·57 lines (48 loc) · 1.62 KB
/
activeAppTracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
import sys, time, subprocess, operator
if len(sys.argv) != 2:
print "NEEDED: File to write statistics to"
sys.exit()
statisticsFile = sys.argv[1]
usedApps = {}
maxIdleTime = 300.0
writeEvery = 300
updateEvery = 1
def loadStatistics():
f = open(statisticsFile, 'r')
dic = {}
for line in f:
split = line.rstrip().partition(":")
if split[0] != "":
key = split[0] + "\n"
else:
key = split[0]
dic[key] = int(split[2].rstrip())
f.close()
return dic
def writeStatistics():
sort = sorted(usedApps.iteritems(), key=operator.itemgetter(1), reverse=True)
f = open(statisticsFile, 'w')
for i, j in sort:
if i.endswith("\n"):
i = i[:-1]
f.write(i + ":" + str(j) + "\n")
f.close()
i = 0
usedApps = loadStatistics()
while True:
activeAppCMD = """arch -i386 osascript \
-e 'tell application "System Events"' \
-e 'set app_name to name of the first process whose frontmost is true' \
-e 'end tell' """
idleTimeCMD = """ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle,"\n"; last}'"""
idleTime = float(subprocess.Popen(idleTimeCMD, shell=True, stdout=subprocess.PIPE).stdout.read())
if idleTime < maxIdleTime:
activeApp = str(subprocess.Popen(activeAppCMD, shell=True, stdout=subprocess.PIPE).stdout.read())
if activeApp not in usedApps:
usedApps[activeApp] = 0
usedApps[activeApp] += updateEvery
i+=1
time.sleep(updateEvery)
if i % writeEvery == 0:
writeStatistics()