I am just learning python and I am relativity new to it. I created the following script that will get the current active windows title and print it to the window.
Install WMI
package first (and pywin32
of cause):
pip install wmi
Then:
import win32process
import wmi
c = wmi.WMI()
def get_app_path(hwnd):
"""Get applicatin path given hwnd."""
try:
_, pid = win32process.GetWindowThreadProcessId(hwnd)
for p in c.query('SELECT ExecutablePath FROM Win32_Process WHERE ProcessId = %s' % str(pid)):
exe = p.ExecutablePath
break
except:
return None
else:
return exe
def get_app_name(hwnd):
"""Get applicatin filename given hwnd."""
try:
_, pid = win32process.GetWindowThreadProcessId(hwnd)
for p in c.query('SELECT Name FROM Win32_Process WHERE ProcessId = %s' % str(pid)):
exe = p.Name
break
except:
return None
else:
return exe