Track windows app usage time using python.
Tags: python, productivity
Published: 2019-08-08 16:27:00

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:
<code>pip install pywin32 win32gui psutil</code>
Now write the following script in code editor:

```python
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.

![](https://thepracticaldev.s3.amazonaws.com/i/jvwuqmd5j7nrgrkx8pem.PNG)
How this code works?
First of all, we maintain two dictionaries: <code>timestamp</code> and <code>process_time</code>. In the <code>timestamp</code> 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 <code>process_time</code> 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:
```python
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 <code>timestamp</code> dictionary.
Then, in order to calculate the <code>process_time</code> of this app. We will subtract the <code>timestamp</code> of this app from current unix time, and add this result to the <code>process_time</code> of this app:
```python
process_time[current_app] = process_time[current_app]+(int(time.time())-timestamp[current_app])
```
We also use <code>time.sleep()</code> 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 <a href="http://www.codeharvest.io">blog</a> as well!