Using psutil to find all foreground apps
Asked Answered
O

0

8

When I enter Task Manager I can see two sections. Apps(3) and Background processes(94). What I'm trying to do is get only those 3 that are not in the background.

My code:

for proc in psutil.process_iter():
    print(proc.name())

But this gives me the names of all of those 90+ apps running in both foreground and background. How do I separate one from the other?

Odlo answered 21/3, 2019 at 21:45 Comment(4)
"separate one from the other?": There is no way to do this beforehand. Use for p in psutil.process_iter(attrs=['name', 'username']) and filter by username.Wheeling
@Wheeling But I would personally need to know the name of the foreground apps. What I want is for my program to recognize them. My ultimate goal is to make a function that closes all foreground running apps. There is no way to do this?Odlo
"need to know the name": As you see in my comment example, there is a name field too. Read filtering-and-sorting-processesWheeling
@Wheeling Please re-read my question. I know how to get the name of the process. What I don't know is how to differentiate one process from another based on whether they're running the background or foreground. What I mean by foreground is the ones you see opened in the bottom of taskbar. When I filter based on username, I get processes which are not on my taskbar too.Odlo

© 2022 - 2024 — McMap. All rights reserved.