Get a list of apps currently visible in the windows task bar from python
Asked Answered
G

2

8

I want to know which apps are open in windows 10 and that are currently showing in windows task bar from python. Like in my case(in the picture below) Chrome, File explorer, and Spyder(python) are currently open and showing in task bar. Task bar.

Hence, windows task manager is showing these 3 applications in the Apps section. So, i want the same output (not the CPU usage or other things just the names/list of opened applications). Task manager.

Glandular answered 19/9, 2017 at 13:7 Comment(0)
M
4

I think this is what you are looking for:

from pywinauto import Desktop

windows = Desktop(backend="uia").windows()
[print(w.window_text()) for w in windows]
Monia answered 19/6, 2022 at 8:41 Comment(0)
I
1

I've done something similar to this before for a Python overlay I've made, and extracted the code to this:

import win32gui # Import the windows API for getting open apps
apps = [] # Leave blank as the getWindows() function will autofill this in
ignoredApps = ['ABC'] # Any apps you want to exclude (such as built-in apps or extra windows such as paint.net windows) go here, or leave the list empty
def getWindows(hwnd, *args)->list: # This returns always returns a list, and it takes in a hwnd argument and multiple other arguments to be ignored.
 global apps
 if win32gui.IsWindowVisible( hwnd ):
  title = win32gui.GetWindowText( hwnd ) # Gets title
  if not title == '': # To ensure the title of the program is not blank
   if not title in apps and not title in ignoredApps: # Prevent duplicate app listings and listing ignored apps
    apps.append(title) # Append to public apps variable, you can do other stuff with the title variable such as only get the content after the last dash symbol as I did for my overlay

win32gui.EnumWindows( getWindows, '' ) # Call the function with hwnd argument filled
print(apps) # Print the list of open apps, excluding any apps in ignoredApps

The list appears to be sorted from most recently focused/opened. Example: If you just focused on Google Chrome, then Google Chrome will be at the top of the list. If you focus/open another application then said application will be at the top.

Make sure that you have the pywin32 module installed, otherwise you will get a ModuleNotFoundError: No module named 'win32gui' exception. You can directly install it via pip install pywin32. I tested this using pywin32==303 as listed by pip freeze which shows all installed PIP modules.

Ignazio answered 19/6, 2022 at 9:37 Comment(2)
Note that this does get all the apps that are active, such as NVIDIA GeForce Overlay so you may want to add those to the ignoredApps. If you have File Explorer open, it will most likely just show as the directory instead of File Explorer unless you add extra code to get other info on the app such as the .exe file name and change it based on that.Ignazio
If you want a shorter version, I'd recommend Cristian Anton's solution. Just remember to use pip install pywinauto.Ignazio

© 2022 - 2024 — McMap. All rights reserved.