import ctypes, json, subprocess, time from pathlib import Path import win32con, win32gui, win32process, win32service library = str(Path(r'F:\Sync Tool Skyworth\Work Maps\Skyworth_CBB_Lib\Mentor_lib_EE7.9\mentor_lib_EE7.9\mentor_lib.lmc')) library = str(Path(r'F:\Sync Tool Skyworth\Work Maps\Skyworth_CBB_Lib\Mentor_lib_EE7.9\mentor_lib.lmc')) name = 'Audio_D2128_ESD' desktop = win32service.CreateDesktop('CbbPrivateLMProbe', 0, win32con.GENERIC_ALL, None) desktop.SetThreadDesktop() from pywinauto import Desktop from pywinauto.controls.common_controls import TreeViewWrapper from pywinauto import win32defines startup = win32process.STARTUPINFO(); startup.lpDesktop = r'winsta0\CbbPrivateLMProbe' exe = r'D:\Mentor\7.9.5EE\SDD_HOME\lm\win32\bin\LibraryManager.exe' hp, ht, pid, tid = win32process.CreateProcess(exe, subprocess.list2cmdline([exe, library]), None, None, False, 0, None, None, startup) ht.Close() for _ in range(120): windows = [w for w in Desktop(backend='win32').windows() if w.is_visible() and w.window_text().startswith('Library Manager:')] if len(windows) == 1: break time.sleep(.5) lm = windows[0] tree = TreeViewWrapper([w for w in lm.descendants() if 'TreeView' in w.class_name()][0].handle) item = tree.get_item(['Central Libraries', Path(library).name, 'Reusable Blocks', 'Logical-Physical', name], exact=True) item.ensure_visible() ctypes.windll.user32.SetFocus(tree.handle) tree.send_message(win32defines.TVM_SELECTITEM, win32defines.TVGN_CARET, item.elem) ui = Desktop(backend='uia').window(handle=lm.handle) uitem = [x for x in ui.descendants(control_type='TreeItem') if x.window_text() == name][0] r = uitem.rectangle(); x = (r.left+r.right)//2; y = (r.top+r.bottom)//2 print('selected', item.is_selected(), 'rect', r, 'tree', tree.handle, flush=True) print('keyboard context', win32gui.PostMessage(tree.handle, win32con.WM_KEYDOWN, win32con.VK_APPS, 0), flush=True) win32gui.PostMessage(tree.handle, win32con.WM_KEYUP, win32con.VK_APPS, 0) time.sleep(.8) menus = [m for m in Desktop(backend='uia').windows(control_type='Menu') if m.is_visible()] print('menus',len(menus),flush=True) for menu in menus: print([(x.window_text(), x.element_info.automation_id, x.is_enabled()) for x in menu.descendants(control_type='MenuItem')],flush=True) for x in menu.descendants(control_type='MenuItem'): if x.window_text().replace('&','') == 'Verify': x.invoke(); print('INVOKED',flush=True) time.sleep(4)