3

I wanted to write a time tracking software using python for windows. How do I approach finding the total screen time of individual applications? Is there any windows service which tracks the total on screen time spent by a particular application during its current session? Or is there some other approach which could be used?

Tarun Khare
  • 1,447
  • 6
  • 25
  • 43

1 Answers1

2

maybe pywin32 is what you looking for:

from win32gui import GetWindowText, GetForegroundWindow
print(GetWindowText(GetForegroundWindow()))

you have to run in repeatedly in the background and calculate the total screen time by yourself.

stefan
  • 188
  • 1
  • 7
  • I didn't know about this module. It looks like a good solution to my problem. Thanks! – Tarun Khare Mar 02 '19 at 18:38
  • Just a follow up question, how you will get application name for cases like 'This PC' where title of the window is usually the current directory we are in, and not the application name. I wanted to only get 'This PC' as output and NOT 'Local disk (D:)' or something else – Tarun Khare Mar 03 '19 at 20:18
  • 1
    with a little workaround you can get the process name of the foreground window, therefore you also need `psutil` to get the process name based on a PID, the PID you can get with `GetWindowThreadIdProcessId`. `psutil.Process(win32process.GetWindowThreadProcessId(GetForegroundWindow())[1]).name` – stefan Mar 03 '19 at 20:56