1

Is there any way to list the Foreground apps (not processes) running on my computer? I tried using psutil but it shows me processes. But I just need to display visible apps.

Here is the code I used -


import psutil
for process in psutil.process_iter ():
    Name = process.name () # Name of the process
    ID = process.pid # ID of the process
    print ("Process name =", Name ,",","Process ID =", ID)

this lists the services that are running too, not just the apps. I need the specific section that are noted by taskmanager as APPS. Look the image below

Taskbar Image here

I tried using the code mentioned above but it didn't work

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This depends on the operating system and window manager. On Linux, the window system isn't part of the OS, it's layered on top. – Barmar Jan 25 '23 at 16:34
  • it's for the windows. If you could check the image link? – Cyber Bandit Jan 25 '23 at 16:37
  • If you can communicate with Powershell from python, try this `(Get-Process | Where-Object { $_.MainWindowHandle -ne 0 } | Select-Object -Property Name).Name`. This will return an array containing the list of foreground apps (and some system extras which you can filter out). – FiddlingAway Jan 25 '23 at 18:06

1 Answers1

0

The simplest way that I found to do this is:

import win32gui

def get_active_windows():
    active_windows = []
    toplist = []

    win32gui.EnumWindows(lambda hWnd, 
       param: toplist.append((hWnd, win32gui.GetWindowText(hWnd))), None)

    for (hwnd, title) in toplist:
        if win32gui.IsWindowVisible(hwnd) and title != '':
            active_windows.append(title)

    return active_windows

active_windows = get_active_windows()
del active_windows[-4:]
print(active_windows)

It consists in checking the windows seen by the system.

Then check if they are visible and if their title is not empty.

Finally, delete the last four records: 1) & 2) settings (x2), 3) Windows Input Environment 4) Program Manager

The result is almost the same as we see in the Windows Task Manager, but instead of the File Explorer, we see the name of the open folder in response.

The order in which we see them corresponds to the last displayed window.

Rich Lysakowski PhD
  • 2,702
  • 31
  • 44