• Login
Community
  • Login

Execute N++ "Find in Files" via cmdline/script?

Scheduled Pinned Locked Moved General Discussion
7 Posts 4 Posters 4.1k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C
    Cameron Young
    last edited by Jul 20, 2017, 10:31 AM

    Love the product, but curious to know if it is possible to execute the “Find in Files” from the command-line / batch / vbscript.

    The output seen within N++ is something I like and I’d like to incorporate this output for some automated tasks if possible.

    Cheers, Cam.

    C 1 Reply Last reply Jul 20, 2017, 2:04 PM Reply Quote 0
    • C
      Claudia Frank @Cameron Young
      last edited by Jul 20, 2017, 2:04 PM

      @Cameron-Young

      not 100% sure what you want to achieve.

      Do you have some automated tasks while npp is not running at all
      and at some step you do want to search something in files and the
      output should be similar to the one npp shows

      or

      do you have npp running, do something and then you want to run
      find in files dialog and this output should be forwarded to
      some automated task?

      Maybe you wanna describe in more detail what exactly you want to do.

      Cheers
      Claudia

      S 1 Reply Last reply Jul 20, 2017, 8:11 PM Reply Quote 1
      • S
        Scott Sumner @Claudia Frank
        last edited by Jul 20, 2017, 8:11 PM

        @Claudia-Frank

        I also am not sure what @Cameron-Young is really wanting to do, but it got me to thinking about how to handle Find-in-Files output programmatically “within” Notepad++ via Pythonscript.

        I came up with the following that will get the textual contents of the Find result panel (based upon the code ideas here –a pretty shallow rip-off in fact!). This could be prefaced with setting the Find dialog’s control fields and pressing the Find-in-Files (or Find-All-…) button (pressing a button example here ) under program control. Once you have the output text, it can be parsed at will to do whatever is needed from that.

        Here’s FindResultPanelGetText.py:

        import ctypes
        from ctypes.wintypes import BOOL, HWND, LPARAM
        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)
        
        find_result_panel_complete_text = ''
        
        def foreach_window(hwnd, lParam):
            global find_result_panel_complete_text
            length = GetWindowTextLength(hwnd)
            if length > 0:
                buff = ctypes.create_unicode_buffer(length + 1)
                GetWindowText(hwnd, buff, length + 1)
                if buff.value.startswith('Search "'):  # perhaps change to more rigorous test...
                    find_result_panel_complete_text = buff.value
                    return False
            return True
        
        EnumChildWindows(FindWindow(u'Notepad++', None), WNDENUMPROC(foreach_window), 0)
        editor.copyText(find_result_panel_complete_text)  # result in clipboard
        
        C 1 Reply Last reply Jul 20, 2017, 9:09 PM Reply Quote 1
        • C
          Claudia Frank @Scott Sumner
          last edited by Jul 20, 2017, 9:09 PM

          @Scott-Sumner

          nice one and could be already half the solution. :-)
          Maybe we should think about python_automation_api_for_npp ;-)

          Cheers
          Claudia

          S 1 Reply Last reply Jul 21, 2017, 12:24 PM Reply Quote 0
          • S
            Scott Sumner @Claudia Frank
            last edited by Jul 21, 2017, 12:24 PM

            @Claudia-Frank

            python_automation_api_for_npp

            Maybe not an all-encompasing automation API, but one focused on the ever-controversial Find functionality…
            Are you up for a joint project with me on this?
            :-D

            C 1 Reply Last reply Jul 21, 2017, 5:09 PM Reply Quote 0
            • C
              Claudia Frank @Scott Sumner
              last edited by Jul 21, 2017, 5:09 PM

              @Scott-Sumner

              in general yes, but not for the moment, currently I’m doing 3 projects in parallel
              And I don’t see that this progresses. :-)

              Cheers
              Claudia

              1 Reply Last reply Reply Quote 0
              • P PeterJones unlocked this topic on Dec 4, 2023, 2:09 PM
              • A
                Alan Kilborn
                last edited by Alan Kilborn Dec 4, 2023, 5:25 PM Dec 4, 2023, 5:24 PM

                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
                
                1 Reply Last reply Reply Quote 2
                • A Alan Kilborn referenced this topic on Dec 4, 2023, 5:24 PM
                7 out of 7
                • First post
                  7/7
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors