@Alan-Kilborn said in PythonScript: How to interact with dialogue boxes?:
That was my exact experience too. And even though inspect.exe shows the search result window, pywinauto still can’t find it.
I instead opted to use this code made by someone here. With it I could create a script that suited my own uses, which I’ve now adapted it into something more general purpose below. It searches across multiple directories, reads the search results and allows you to open the files. (To anyone that uses it: make sure ‘Purge for every search’ is enabled in the search results context menu.)
# encoding=utf-8 from ctypes.wintypes import BOOL, HWND, LPARAM import ctypes import os.path import re import time from Npp import editor from pywinauto.application import Application console.clear() console.show() console.editor.setReadOnly(False) def get_search_results(): WNDENUMPROC = ctypes.WINFUNCTYPE(BOOL, HWND,LPARAM) FindWindow = ctypes.windll.user32.FindWindowW GetWindowText = ctypes.windll.user32.GetWindowTextW GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW EnumChildWindows = ctypes.windll.user32.EnumChildWindows GetClassName = ctypes.windll.user32.GetClassNameW curr_class = ctypes.create_unicode_buffer(256) search_results = '' def foreach_window(hwnd, lParam): global search_results length = GetWindowTextLength(hwnd) if length > 0: buff = ctypes.create_unicode_buffer(length + 1) GetWindowText(hwnd, buff, length + 1) if buff.value.startswith('Search "'): search_results = str(buff.value) return False return True EnumChildWindows(FindWindow(u'Notepad++', None), WNDENUMPROC(foreach_window), 0) def open_files(): try: file_list = re.findall(r'(C:\\.*)(?: \([0-9] hits?\))', search_results) except: console.editor.addStyledText(Cell('\nNo search results detected', [1])) count = str(len(file_list)) if count == '0': console.editor.addStyledText(Cell('\nNo files in results', [1])) return prompt = notepad.messageBox('Open all ' + count + ' files?', '', 1) if prompt == 1: for file in file_list: if os.path.isfile(file): notepad.open(file) else: console.editor.addStyledText(Cell('\nFiles/directory not found', [1])) break app.wait_cpu_usage_lower(threshold=20, timeout=600) # wait until CPU usage is lower than 20% notepad.menuCommand(MENUCOMMAND.SEARCH_FINDINFILES) app = Application().connect(path=r"C:\Program Files\Notepad++\notepad++.exe") find_in_files = app.FindInFiles directories_list = 'list of directories' pattern = r'pattern' for directory in directories_list: notepad.menuCommand(MENUCOMMAND.SEARCH_FINDINFILES) app.find_in_files.FindWhat_Edit.set_text(pattern) app.find_in_files.Directory_Edit.set_text(directory) app.find_in_files.FindAll.click() app.wait_cpu_usage_lower(threshold=20, timeout=600) get_search_results() open_files() time.sleep(0.1) # this is needed if the prompt is removed from open_files()