import ctypes, sys, os
from ctypes import wintypes
import win32gui
from PIL import ImageGrab
import psutil

sys.stdout.reconfigure(encoding='utf-8')
user32 = ctypes.windll.user32
hdesk = user32.OpenInputDesktop(0, False, 0x01FF)
if hdesk:
    user32.SetThreadDesktop(hdesk)

pids = [p.pid for p in psutil.process_iter(['pid', 'name']) if 'JXHelper' in (p.info['name'] or '')]
print('JXHelper PIDs:', pids)
if not pids:
    print('No JXHelper process found!')
    sys.exit(0)

target_pid = pids[0]
hwnds = []
def cb(h, _):
    pid = wintypes.DWORD()
    user32.GetWindowThreadProcessId(h, ctypes.byref(pid))
    if pid.value == target_pid:
        txt_len = user32.GetWindowTextLengthW(h) + 1
        txt_buf = ctypes.create_unicode_buffer(txt_len)
        user32.GetWindowTextW(h, txt_buf, txt_len)
        if user32.IsWindowVisible(h):
            hwnds.append((h, txt_buf.value))
    return True

WNDENUMPROC = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
user32.EnumWindows(WNDENUMPROC(cb), 0)

import time

for h, txt in hwnds:
    print(f'Found HWND {hex(h)}: "{txt}"')
    if "JXHelper" in txt:
        user32.PostMessageW(h, 0x0100, 0x1B, 0) # WM_KEYDOWN VK_ESCAPE
        user32.PostMessageW(h, 0x0101, 0x1B, 0) # WM_KEYUP VK_ESCAPE
        time.sleep(0.3)
        rect = win32gui.GetWindowRect(h)
        print('Rect:', rect)
        if rect[2] > rect[0] and rect[3] > rect[1]:
            img = ImageGrab.grab(bbox=rect)
            art_path = r'C:\Users\Admin\.gemini\antigravity\brain\5f637e30-06d3-43e8-a064-a259fea7be1a\jxhelper_char_names_live.png'
            img.save(art_path)
            print(f'Saved screenshot to {art_path}')
