I want to get the application name that is currently opened using python. This is my code
import win32.win32gui as win32gui
w = win32gui
window_name = w.GetWindowText(w.GetForegroundWindow())
window_name = str(window_name)
start_time = datetime.now().replace(microsecond=0)
while True:
new_window_name = w.GetWindowText(w.GetForegroundWindow())
if window_name!=new_window_name:
stop_time = datetime.now().replace(microsecond=0)
print(f"{window_name} Time Spent: {stop_time-start_time}")
window_name =new_window_name
start_time = datetime.now().replace(microsecond=0)
and with this, I am getting only the application heading not the application name so how do i get it.
Using the psutil you can get the process name.
import psutil
import win32process, win32gui
hwnd = win32gui.GetForegroundWindow()
_,pid = win32process.GetWindowThreadProcessId(hwnd)
process = psutil.Process(pid)
process_name = process.name()