Last active
July 28, 2024 13:46
-
-
Save lethee/beeef662cc627688c40f to your computer and use it in GitHub Desktop.
Get a active window title and its executable path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pywin32 must be installed | |
# pip install wmi | |
import win32gui | |
import win32process | |
import win32pdhutil | |
import wmi | |
procs = wmi.WMI().Win32_Process() | |
pycwnd = win32gui.GetForegroundWindow() | |
tid, pid = win32process.GetWindowThreadProcessId(pycwnd) | |
for proc in procs: | |
if proc.ProcessId == pid: | |
print 'pid', pid | |
print 'exec', proc.ExecutablePath | |
print 'title', win32gui.GetWindowText(pycwnd) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from subprocess import Popen, PIPE | |
import re | |
def get_active_window_title(): | |
root_check = '' | |
root = Popen(['xprop', '-root'], stdout=PIPE) | |
if root.stdout != root_check: | |
root_check = root.stdout | |
for i in root.stdout: | |
if '_NET_ACTIVE_WINDOW(WINDOW):' in i: | |
id_ = i.split()[4] | |
id_w = Popen(['xprop', '-id', id_], stdout=PIPE) | |
id_w.wait() | |
buff = [] | |
for j in id_w.stdout: | |
buff.append(j) | |
title = "Active window not found" | |
app_title = "" | |
for line in buff: | |
match = re.match("WM_NAME\((?P<type>.+)\) = (?P<name>.+)", line) | |
if match != None: | |
type = match.group("type") | |
if type == "STRING" or type == "COMPOUND_TEXT": | |
title = match.group("name") | |
match = re.match("_OB_APP_CLASS\((?P<type>.+)\) = (?P<name>.+)", line) | |
if match != None: | |
app_title = match.group("name") | |
return title, app_title | |
title, app_title = get_active_window_title() | |
print title | |
print app_title |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good work,
How can we get the same for all "opened windows" in Windows10?