This is a real necropost at roughly 6.5 years later, but I wanted to post an updated version of the earlier script made by @Scott-Sumner ; one that just feels a bit less hackish (sorry).
I call this variation SearchResultPanelGetText.py:
# -*- coding: utf-8 -*-
from __future__ import print_function
#########################################
#
# SearchResultPanelGetText (SRPGT)
#
#########################################
# references:
# https://community.notepad-plus-plus.org/topic/14181
# for newbie info on PythonScripts, see https://community.notepad-plus-plus.org/topic/23039/faq-desk-how-to-install-and-run-a-script-in-pythonscript
#-------------------------------------------------------------------------------
from ctypes import ( create_unicode_buffer, WinDLL, WINFUNCTYPE, )
from ctypes.wintypes import ( BOOL, HWND, LPARAM, )
import re
user32 = WinDLL('user32')
#-------------------------------------------------------------------------------
class SRPGT(object):
def __init__(self):
self.search_results_hwnd = None
WNDENUMPROC = WINFUNCTYPE(BOOL, HWND, LPARAM)
user32.EnumChildWindows(user32.FindWindowW(u'Notepad++', None), WNDENUMPROC(self.foreach_child_window), 0)
def foreach_child_window(self, hwnd, __):
length = user32.GetWindowTextLengthW(hwnd)
if length > 0:
buff = create_unicode_buffer(length + 1)
user32.GetWindowTextW(hwnd, buff, length + 1)
if re.match(r'(?:Search) ".*?" \(\d+ hits? in \d+ files? of \d+ searched', buff.value):
self.search_results_hwnd = hwnd
return False # stop searching windows
return True # continue searching windows
def get_text(self):
search_results_complete_text = ''
if self.search_results_hwnd is not None:
length = user32.GetWindowTextLengthW(self.search_results_hwnd)
if length > 0:
buff = create_unicode_buffer(length + 1)
user32.GetWindowTextW(self.search_results_hwnd, buff, length + 1)
search_results_complete_text = buff.value
return search_results_complete_text
def get_most_recent_search_result_text(self):
recent_text = ''
m = re.match(r'(?s)(?:Search) ".*?" \(\d+ hits? in \d+ files? of \d+ searched.*?(?=^Search|\Z)', self.get_text())
if m: recent_text = m.group(0)
return recent_text
#-------------------------------------------------------------------------------
if __name__ == '__main__': print(SRPGT().get_text()) # demo