DEV Community

Cover image for Track windows app usage time using python.
Tarun Khare
Tarun Khare

Posted on

Track windows app usage time using python.

We waste a lot of time in playing games, watching movies, listening to Spotify as well. So it becomes important to track usage of various apps in our Windows PC.

We will learn how to write a python script which can track the amount of time you spent on various desktop apps like VSCode, Spotify, File explorer etc. For running this script, you require Python version 3.6 or below. This script wonโ€™t work in Python 3.7 as the libraries we will use are not currently supported in Python 3.7.

First of all install the libraries psutil, pywin32, win32gui using the pip command:
pip install pywin32 win32gui psutil
Now write the following script in code editor:

from win32gui import GetForegroundWindow
import psutil
import time
import win32process

process_time={}
timestamp = {}
while True:
    current_app = psutil.Process(win32process.GetWindowThreadProcessId(GetForegroundWindow())[1]).name().replace(".exe", "")
    timestamp[current_app] = int(time.time())
    time.sleep(1)
    if current_app not in process_time.keys():
        process_time[current_app] = 0
    process_time[current_app] = process_time[current_app]+int(time.time())-timestamp[current_app]
    print(process_time)

Now run this script. You will see the app time getting printed every second on console. As you will switch between different applications, the time spent on them will keep getting printed on the console.


How this code works?
First of all, we maintain two dictionaries: timestamp and process_time. In the timestamp dictionary, we store the latest Unix timestamp at which a particular application is found in active/ foreground window. The application name and latest timestamp are stored as key value pairs inside this dictionary.

In process_time dictionary, we store the total time an application has spent in active/ foreground window. This is the time which we want to track through this script.

Using the line:

current_app = psutil.Process(win32process.GetWindowThreadProcessId(GetForegroundWindow())[1]).name().replace(".exe", "")

We get the name of the application which is currently in foreground window. First of all, we store the timestamp of this app in the timestamp dictionary.
Then, in order to calculate the process_time of this app. We will subtract the timestamp of this app from current unix time, and add this result to the process_time of this app:

process_time[current_app] = process_time[current_app]+(int(time.time())-timestamp[current_app])

We also use time.sleep() inside the code so that the while loop repeats only after a certain interval.

Thatโ€™s it! If you want, you can further tinker with this script by creating a frontend around it, adding database support for permanently storing data and so on. Hope you liked the article. Comment in case of any doubts. Also check out other articles on my blog as well!

Top comments (0)