Community
    • Login

    Using the PythonScript plugin to automate N++

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    pythonscriptautomation
    53 Posts 6 Posters 5.2k 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.
    • Alan KilbornA
      Alan Kilborn @TBugReporter
      last edited by Alan Kilborn

      @TBugReporter said in Using the PythonScript plugin to automate N++:

      I wouldn’t characterize it that way, but yes.

      LOL.
      And thanks for not having an emotional overreaction to my comments the way some people do.


      because of that, my opinion of N++ has changed. I used to think of it as a well-polished alternative to the text editor supplied with Windows, but now I see it more as a collection of parts that don’t quite fit together - and that getting them to work together as well as they do is a major accomplishment in itself.

      IMO, Notepad++ is “well polished” but maybe it has enough quirks to keep things interesting. :-)

      And I don’t know that I’d agree that it is a “collection of parts that don’t quite fit together”. Probably if it were a sack of disjointed parts even the author would’ve given up on it long ago.


      That’s not to say that I won’t keep pushing to get it working better, but now I see the rough edges and understand why they’re so rough.

      I don’t know if your “rough” assessment is accurate, but I agree that all any of us (that are interested) can do, is some “pushing”.

      TBugReporterT 1 Reply Last reply Reply Quote 0
      • TBugReporterT
        TBugReporter @Alan Kilborn
        last edited by TBugReporter

        @Alan-Kilborn said in Using the PythonScript plugin to automate N++:

        thanks for not having an emotional overreaction to my comments the way some people do.

        I have experience with people who seem brusque when communicating over the Internet - heck, I am “those people” sometimes.

        IMO, Notepad++ is “well polished” but maybe it has enough quirks to keep things interesting. :-)

        Okay, I’ll go with “quirky”.


        @Ekopalypse:
        Two questions for you -

        1. I’m trying to import your taskdialog.py script into mine, but I’m getting this error:
          File "{...}\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\taskdialog.py", line 400, in show
            None if self.checked_verification is None else ctypes.byref(wintypes.INT(self.checked_verification))
        ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: expected LP_TASKDIALOGCONFIG instance instead of pointer to TASKDIALOGCONFIG
        

        Is this a bug in your script, or mine (or both)?

        1. I want my dialog to contain a group of checkboxes, but your script seems to only allow for one. What do I need to do to get this? Never mind. I found the Windows documentation for the TaskDialog routines and realized that this is just a slightly fancier version of “Abort, Retry, Ignore?” - IOW, it’s still not configurable enough for what I want, and everybody uses GUI IDEs to build these now, so there’s very little info out there on how to “roll your own”.
        EkopalypseE 2 Replies Last reply Reply Quote 0
        • EkopalypseE
          Ekopalypse @TBugReporter
          last edited by

          @TBugReporter

          Let me check - I will follow up later today.

          1 Reply Last reply Reply Quote 0
          • EkopalypseE
            Ekopalypse @TBugReporter
            last edited by

            @TBugReporter

            I was not able to replicate the error you mentioned, but unfortunately while testing I found a serious problem that could lead to a hanging or even crashing npp. This needs to be investigated further.

            1 Reply Last reply Reply Quote 1
            • TBugReporterT
              TBugReporter @Ekopalypse
              last edited by TBugReporter

              @Ekopalypse said in Using the PythonScript plugin to automate N++:

              maybe it should be wrapped in a try block and if it can’t be found a MessageBox is called explaining this.

              @TBugReporter said in Using the PythonScript plugin to automate N++:

              You mean like this one? 😉

              @Ekopalypse said in Using the PythonScript plugin to automate N++:

              Yes, but no - more in the direction pointing to the package that contains the Tkinter module.

              I’ve now added code to my script that comes close to doing this. An excerpt:

                  tk_ok = False
                  try:                                                        # see if we can do pretty dialogs
                      import Tkinter as tk
                      import ttk
                      import qq                                               # DEBUG:  bogus name to force error triggering
                      tk_ok = True
                  except ImportError as e:                                    # if not, tell user
                      console.show()
                      user_response = notepad.messageBox(
                          (tk_err_msg + e.message
                          ), "Missing Library",
                          MESSAGEBOXFLAGS.OKCANCEL | MESSAGEBOXFLAGS.ICONWARNING | MB_HELP)
                      if user_response == MESSAGEBOXFLAGS.RESULTCANCEL:
                          print("== RESULTCANCEL loading Tkinter")            # DEBUG
                          notepad.runPluginCommand('Python Script', 'Stop Script')
              

              This produces the following box:
              Missing Library.png
              Given that MB_HELP isn’t defined as part of PythonScript’s MESSAGEBOXFLAGS, it seems that no one anticipated anyone wanting to add a “Help” button to a MessageBox. Can someone here help me finish this feature for my script? Apparently, the “Help” button sends a message to the parent process, but do I have to add code to listen for and react to this message? Examples I’ve seen for other languages make it seem like I only need to define a URL to be accessed when the button is clicked, but again, I’m having a hard time translating those examples to PythonScript.

              (Also, I realize that with this question, I’m again straying away from N++ - for which I was soundly rebuked last time - but I’m thinking that interest in the larger goal of developing a more elegant way to inform PythonScript users of missing libraries might outweigh that concern.)

              Alan KilbornA 1 Reply Last reply Reply Quote 0
              • Alan KilbornA
                Alan Kilborn @TBugReporter
                last edited by Alan Kilborn

                @TBugReporter said in Using the PythonScript plugin to automate N++:

                notepad.runPluginCommand('Python Script', 'Stop Script')

                I thought we told you this was a “bad idea”; wasn’t there a discussion of how to “properly” end a script?

                I’m again straying away from N++ - for which I was soundly rebuked last time

                Nah, you go ahead if you want…

                but I’m thinking that interest in the larger goal of developing a more elegant way to inform PythonScript users of missing libraries might outweigh that concern.

                IMO, nobody’s concern is all that great, but if you think so then by all means proceed.

                Given that MB_HELP isn’t defined as part of PythonScript’s MESSAGEBOXFLAGS, it seems that no one anticipated anyone wanting to add a “Help” button to a MessageBox.

                A full-fledged polished programming environment/suite was never really what PS’s designers had in mind. A lot of times Python programs are referred to as “scripts” (which is really a poor thing to do for Python), but here we really are “scripting”, which is intended to be rather lightweight. It’s even in the name of the plugin.

                I use it for the power it can give me over the editor, not its UI capabilities, or its “full-fledgeness”.

                IMO you are heading down the wrong road, but let’s see where you end up…

                TBugReporterT 2 Replies Last reply Reply Quote 0
                • TBugReporterT
                  TBugReporter @Alan Kilborn
                  last edited by TBugReporter

                  @Alan-Kilborn said in Using the PythonScript plugin to automate N++:

                  wasn’t there a discussion of how to “properly” end a script?

                  Yeah, I just never got around to changing that. (I’m retired, so I tend to do whatever I find entertaining at the moment.)

                  @TBugReporter said in Using the PythonScript plugin to automate N++:

                  I’m again straying away from N++ - for which I was soundly rebuked last time

                  @Alan-Kilborn said in Using the PythonScript plugin to automate N++:

                  Nah, you go ahead if you want…

                  nobody’s concern is all that great, but if you think so then by all means proceed.

                  Okay, perhaps my own enthusiasm for the project may have caused me to misread the temperature of the room - but as I said, I do as I like, and this project has intrigued me. It may end up as just another of my unfinished projects, but for now, I’ll keep banging on it.

                  IMO you are heading down the wrong road, but let’s see where you end up…

                  So what do you consider “the right road”? Maybe that road will intrigue me more. 🙂

                  In software development, nothing is impossible, except making every user happy.
                  — Notepad++ (@Notepad_plus)

                  EkopalypseE 1 Reply Last reply Reply Quote 0
                  • EkopalypseE
                    Ekopalypse @TBugReporter
                    last edited by

                    @TBugReporter and All

                    I have created a taskdialog module that hopefully,
                    works with PS2 and PS3 without any side effects.
                    See the readme for more information on how to use it.
                    If there is anything unclear or difficulties, do not hesitate to open an issue in the repo.

                    1 Reply Last reply Reply Quote 3
                    • TBugReporterT
                      TBugReporter @Alan Kilborn
                      last edited by

                      @Alan-Kilborn said in Using the PythonScript plugin to automate N++:

                      I thought we told you this was a “bad idea”; wasn’t there a discussion of how to “properly” end a script?

                      I now remember why I didn’t change this. Your “proper” method doesn’t work for me when the reason to end the script is detected in a sub-(sub-sub-…)routine - it just ends the subroutine. Or am I (again) doing it wrong?

                      Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
                      • Alan KilbornA
                        Alan Kilborn @TBugReporter
                        last edited by Alan Kilborn

                        @TBugReporter said in Using the PythonScript plugin to automate N++:

                        Your “proper” method doesn’t work for me when the reason to end the script is detected in a sub-(sub-sub-…)routine - it just ends the subroutine.

                        Not my method; Python’s.

                        Here’s some more pure Python (that is on-topic for this forum only because we’re talking about exiting PythonScripts) that answers the question; simply run it to see the effect, varying the if in func3() between if 1 and if 0 for a couple of runs.

                        # -*- coding: utf-8 -*-
                        from __future__ import print_function
                        
                        class Exit_exception(Exception): pass
                        
                        def func3():
                            if 0:
                                print('func3 determined we should end and do it QUICKLY')
                                raise Exit_exception()
                            else:
                                print('func3 determined we should proceed in an orderly fashion')
                        
                        def func2():
                            print('entered func2, about to call func3')
                            func3()
                            print('back in func2, after calling func3')
                        
                        def func1():
                            print('entered func1, about to call func2')
                            func2()
                            print('back in func1, after calling func2')
                        
                        def main():
                            print('entering main, about to call func1')
                            try:
                                func1()
                            except Exit_exception:
                                print('back in main and making an exceptional exit!')
                                return
                            print('back in main, after calling func1')
                        
                        main()
                        

                        This is a bit more “advanced” than what was discussed before; I didn’t share it then because it is more complicated and didn’t seem necessary at the time.

                        So for the if 0 case, all proceeds normally and in-sequence:

                        entering main, about to call func1
                        entered func1, about to call func2
                        entered func2, about to call func3
                        func3 determined we should proceed in an orderly fashion
                        back in func2, after calling func3
                        back in func1, after calling func2
                        back in main, after calling func1
                        

                        If the code is changed to if 1, then we obtain:

                        entering main, about to call func1
                        entered func1, about to call func2
                        entered func2, about to call func3
                        func3 determined we should end and do it QUICKLY
                        back in main and making an exceptional exit!
                        

                        BTW, this is just a “nice” way of achieving the goal. If you want to end abruptly and rudely, just insert this line of code where you want to achieve it:

                        1/0 <-- yep, just a 3-character line of code

                        Side note: The exception technique can also be used to get out of deeply nested loops, example new func2:

                        def func2():
                            print('entered func2, about to call func3')
                            try:
                                while True:
                                    while True:
                                        while True:
                                            while True:
                                                while True:
                                                    while True:
                                                        while True:
                                                            while True:
                                                                func3()
                                                                raise Exit_exception()
                            except Exit_exception: pass
                            print('back in func2, after calling func3')
                        
                        TBugReporterT 1 Reply Last reply Reply Quote 2
                        • PeterJonesP
                          PeterJones @TBugReporter
                          last edited by PeterJones

                          @TBugReporter ,

                          Just as an aside from @Alan-Kilborn’s excellent example of using an exception to bomb out: he handled the exception in main() – and that’s probably Python best-practice, and the right way to show it in an example that others follow

                          But just so that you know: if you ever forget to handle an exception, the PythonScript plugin will be forgiving, and won’t exit Notepad++ for you. It will show the traceback for the exception, and will relinquish control back to Notepad++. You’ll have a nice red message

                          raise Exception()
                          8826f69e-ee18-4372-b2bd-4163fdeea8a6-image.png

                          And if you want additional information in your exception printout, you can add it as a text argument when you raise the exception

                          raise Exception("extra text")

                          6472ce88-bdf4-4e28-99c1-aa1234189ccb-image.png

                          Or, using Alan’s Exit_exception() sub-class, the name of the class becomes an indicator of what kind of exception it was:

                          97a350ea-ad16-4a8f-9bdf-d81e891d03ef-image.png

                          281df0f5-a230-4b78-b33f-cc6d9cdccffd-image.png

                          But however you call the exception: after seeing such a red error, you will then want to fix your script so that it won’t happen again.

                          And if you want a clean exit – it’s not an “error”, just a way to leave a deep subroutine early – then handling that clean-exit-exception in main() is the Right Thing To Do™

                          TL;DR summary

                          Catching exceptions at some point is Python best-practice, but if you miss catching one, the PythonScript plugin will not exit Notepad++. Thus, if you want to exit a script early or promptly, raising an exception is a much better choice than using notepad.runPluginCommand('Python Script', 'Stop Script') to push the menu button for you

                          Alan KilbornA 1 Reply Last reply Reply Quote 2
                          • Alan KilbornA
                            Alan Kilborn @PeterJones
                            last edited by

                            I just noticed that we dealt with the topic of “early return” from a PythonScript before, HERE. That time, the simple return-from-main() technique satisfied the petitioner. :-)

                            TBugReporterT 1 Reply Last reply Reply Quote 1
                            • TBugReporterT
                              TBugReporter @Alan Kilborn
                              last edited by

                              This post is deleted!
                              1 Reply Last reply Reply Quote 0
                              • TBugReporterT
                                TBugReporter
                                last edited by

                                Is there any way that the PythonScript plugin can determine the size and position of the N++ window?

                                EkopalypseE 1 Reply Last reply Reply Quote 0
                                • EkopalypseE
                                  Ekopalypse @TBugReporter
                                  last edited by Ekopalypse

                                  @TBugReporter

                                  as an example

                                  import ctypes
                                  from ctypes import wintypes
                                  
                                  rect = wintypes.RECT()
                                  
                                  user32 = ctypes.WinDLL("user32")
                                  hwnd = user32.FindWindowW(u"Notepad++", None)
                                  user32.GetWindowRect(hwnd, ctypes.byref(rect))
                                  print("position = ({},{})".format(rect.top, rect.left))
                                  print("width = {}".format(rect.right - rect.left))
                                  print("height = {}".format(rect.bottom - rect.top))
                                  

                                  Be careful though, these are the outputs for my main and secondary monitor for example

                                  position = (0,459)
                                  width = 1079
                                  height = 1087
                                  
                                  >>> 
                                  position = (-440,1912)
                                  width = 1096
                                  height = 1936
                                  

                                  and when you start making more and more C interop calls, do yourself a favor and create your own startup.py script (you create it like any other script, but just call it startup.py. It should be stored alongside your other scripts so that it doesn’t conflict with the startup.py that comes with PS by default)

                                  and do something like this

                                  from win_api import FindWindow, FindWindowEx
                                  notepad.hwnd = FindWindow(u'Notepad++', None)
                                  editor1.hwnd = FindWindowEx(notepad.hwnd, None, u"Scintilla", None)
                                  editor2.hwnd = FindWindowEx(notepad.hwnd, editor1.hwnd, u"Scintilla", None)
                                  

                                  win_api is another file that contains all your C type declarations.
                                  Just a suggestion.

                                  Alan KilbornA 1 Reply Last reply Reply Quote 1
                                  • Alan KilbornA
                                    Alan Kilborn @Ekopalypse
                                    last edited by

                                    @Ekopalypse said in Using the PythonScript plugin to automate N++:

                                    FindWindow(u’Notepad++', None)

                                    If you have multiple instances of Notepad++ running, is this guaranteed to find the one that the script code is executing within?

                                    EkopalypseE 1 Reply Last reply Reply Quote 0
                                    • EkopalypseE
                                      Ekopalypse @Alan Kilborn
                                      last edited by

                                      @Alan-Kilborn

                                      normaly yes - but 100% guaranteed - I assume no.

                                      Alan KilbornA 1 Reply Last reply Reply Quote 0
                                      • Alan KilbornA
                                        Alan Kilborn @Ekopalypse
                                        last edited by

                                        @Ekopalypse said in Using the PythonScript plugin to automate N++:

                                        normaly yes - but 100% guaranteed - I assume no.

                                        I asked because I have some more complicated code to find the correct N++, and I was wondering if it was necessary. I’ll keep it. :-)

                                        EkopalypseE TBugReporterT 2 Replies Last reply Reply Quote 1
                                        • EkopalypseE
                                          Ekopalypse @Alan Kilborn
                                          last edited by Ekopalypse

                                          @Alan-Kilborn

                                          It would be nice if PS would provide this, I know there is an open issue …

                                          Alan KilbornA 1 Reply Last reply Reply Quote 0
                                          • Alan KilbornA
                                            Alan Kilborn @Ekopalypse
                                            last edited by

                                            @Ekopalypse said in Using the PythonScript plugin to automate N++:

                                            would be nice if PS would provide this, I know there is an open issue

                                            Yes, HERE, but from the comment HERE the primary person maintaining PS seemed to have no clue as to why it would be valuable. :-(

                                            dinkumoilD 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors