- 1 year ago
Get the list of FOREGROUND apps using python psutil
Comment
1 / 1
You can use the psutil
package to get the list of foreground applications on Windows. The psutil
package provides a cross-platform way to retrieve information about running processes, system utilization, and system resources.
To get the list of foreground applications, you can use the following code:
pythonimport psutil
for proc in psutil.process_iter(['pid', 'name', 'username']):
try:
if proc.status() == psutil.STATUS_RUNNING and proc.cpu_percent(interval=0) > 0:
if proc.memory_percent() > 0 and proc.username() != 'SYSTEM':
if proc.name() not in ['System Idle Process', 'System']:
print(proc.name())
except psutil.AccessDenied:
pass
This code iterates over all running processes, checks if the process is running in the foreground and has some CPU and memory usage, and then prints the name of the process.
Note that you may need administrative privileges to get information about some processes, and some information may not be available due to security restrictions.