Community
    • Login

    PythonScript: Check If Notepad++ Was Closed To Terminate an Infinite Loop

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    14 Posts 3 Posters 1.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.
    • PeterJonesP
      PeterJones @John Doe 1
      last edited by

      @john-doe-1 said in PythonScript: Check If Notepad++ Was Closed To Terminate an Infinite Loop:

      My script holds the alt key for column select and releases it at times that are necessary,

      Your paradigm is BAD and causing you problems.
      Try this instead.

      1 Reply Last reply Reply Quote 0
      • John Doe 1J
        John Doe 1 @Alan Kilborn
        last edited by

        @alan-kilborn Hey I tried testing the callback function with SHUTDOWN like so in a simple Python script:

        notepad.callback(my_callback(), [NOTIFICATION.SHUTDOWN])

        Where my_callback() is a function that writes “Hello world!” to a text file. I noticed that as soon as the script executes that text file gets “Hello world!” written to it, isn’t that not supposed to happen until Notepad++ is closed/shutdown? I thought that was the purpose of the [NOTIFICATION.SHUTDOWN]

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

          @john-doe-1 said in PythonScript Toggleable Script?:

          Where my_callback() is a function that writes…

          my_callback() is the invocation of that function – you’re running it right there, that’s why you see output immediately.

          You want simply my_callback there, like this:

          notepad.callback(my_callback, [NOTIFICATION.SHUTDOWN])

          No () after it.

          John Doe 1J 1 Reply Last reply Reply Quote 1
          • PeterJonesP
            PeterJones @John Doe 1
            last edited by

            @john-doe-1 ,

            I used my moderator powers to move your “hey” post from the “toggle” discussion to the “terminate on close” discussion (and moved Alan’s reply).

            Unlike the last post, where you started talking about it in one then created another for the same conversation, this is a valid separation of concepts, and it will help keep the conversations separate. Especially since going down this “I take control of your ALT key” is the wrong way to go for your end goal. But the shutdown notification is an interesting aside.

            You don’t need to be working on it any more, because you shouldn’t be going down this path for your project. Because it is the wrong path. But if you are still interested in talking about the shutdown event from a learning standpoint, keep that discussion here. And Alan’s explanation is why it triggered at the “wrong” time.

            1 Reply Last reply Reply Quote 1
            • John Doe 1J
              John Doe 1 @Alan Kilborn
              last edited by

              @alan-kilborn said in PythonScript: Check If Notepad++ Was Closed To Terminate an Infinite Loop:

              You want simply my_callback there, like this:
              notepad.callback(my_callback, [NOTIFICATION.SHUTDOWN])

              Hey I just tried that out and now it doesn’t write “Hello world!” to the text file at all. Not upon execution and not after closing Notepad++

              PeterJonesP 1 Reply Last reply Reply Quote 0
              • PeterJonesP
                PeterJones @John Doe 1
                last edited by

                @john-doe-1 said in PythonScript: Check If Notepad++ Was Closed To Terminate an Infinite Loop:

                Hey I just tried that out and now it doesn’t write “Hello world!” to the text file at all. Not upon execution and not after closing Notepad++

                Then you are doing something wrong. But since we are not looking over your shoulder, we cannot tell you what.

                1. Does my exact script work for you?
                  • if so, then you can start comparing what’s different between my script and yours that is causing yours not to work
                  • if not, then you are doing something wrong, because it works every time for me. Explain the exact procedure that makes you think it should be active but is not working.
                    1. Run script that registers the callback – it should only register the callback once
                    2. Exit Notepad++
                    3. Look at c:\temp\onShutdown.txt and see that it wrote to the file
                2. You have to be careful in NOTIFICATION.SHUTDOWN callbacks
                  • They cannot do too much, otherwise Notepad++ might finish shutting down before they have run.
                  • For example, my original debug callback tried to do a notepad.messageBox() call – the message box never showed, because Notepad++ was beyond the point of being able to do GUI like MessageBox.
                3. When I was debugging, I started with notepad.callback(myOnShutdown_22919, [NOTIFICATION.SHUTDOWN, NOTIFICATION.BUFFERACTIVATED]) , so that I could get the callback to run by just activating tabs; this allowed me to debug that there were no errors (I originally had some, because I forgot to double-backslash the paths). You may have to debug

                As a reminder, this is not the right solution for your column-selection issue. Do not continue down this path if your goal is still to get your column-selection hack to work.

                John Doe 1J 2 Replies Last reply Reply Quote 1
                • John Doe 1J
                  John Doe 1 @PeterJones
                  last edited by John Doe 1

                  @peterjones Okay I’ve done some more testing, and writing to a file after shutdown is working. Strangley the statement:

                  pyautogui.keyUp(“alt”)

                  Does nothing after shutdown even though it is placed right above the statement for writing to a file, I even tried including the library again in the my_callback definition and no luck. Its strange that this statement refuses to work properly in the shutdown callback but writing to a file works no problem.

                  PeterJonesP 1 Reply Last reply Reply Quote 0
                  • PeterJonesP
                    PeterJones @John Doe 1
                    last edited by PeterJones

                    @john-doe-1 ,

                    Its strange that this statement refuses to work properly in the shutdown callback but writing to a file works no problem (said above)
                    Works with my_callback upon shutdown SOMETIMES (said below)

                    It might be that GUI handles (hWnd and similar) have already been deleted/cleared by the time it gets that far (or at least sometimes), so whatever it is that pyautogui.keyDown() operates on might not exist anymore. Yet another reason that the SHUTDOWN callback might not be viable for every circumstance (especially yours).

                    1 Reply Last reply Reply Quote 1
                    • John Doe 1J
                      John Doe 1 @PeterJones
                      last edited by

                      @peterjones Couldn’t edit so I did a double reply, sorry. Okay I just noticed what’s even stranger is that the statement:

                      pyautogui.keyDown(“alt”)

                      Works with my_callback upon shutdown SOMETIMES. It is the strangest thing, I will run the script and then close out Notepad++ and hit Tab to see if it uses the Windows Alt+Tab feature (indicating Alt is still being held after shutdown). Sometimes it will Alt+Tab me (meaning Alt is still being held down and the callback didn’t work) and other times it will work perfectly and simply Tab (meaning Alt was properly released during the callback).

                      It seems completely random like a coin flip as to which result will occur, any idea why this may be?

                      PeterJonesP 1 Reply Last reply Reply Quote 0
                      • PeterJonesP
                        PeterJones @John Doe 1
                        last edited by

                        @john-doe-1 ,

                        (I was in the middle of an edit when you posted)

                        To finish my thought:

                        I believe the intention of the SHUTDOWN notification handler was to allow plugins to clean up their memory/objects/background files nicely when Notepad++ exits. It wasn’t intended as a place to do any GUI changes (including any win32 api calls like keyboard handlers and the like, which is presumably what pyautogui.keyDown works with).

                        No application or plugin or script should ever affect the keyboard or mouse input for the whole OS (like your persistent modification that lasts after Notepad++ exits) without the documentation making it very clear that it intentionally does so, and without the author of said application/plugin/script fully understanding all the nuances involved – and I’m sorry, but your questions and difficulties in implementing this tell me that you don’t understand the nuances. If I ever found that a Notepad++ plugin author or the author of a PythonScript script for Notepad++ ever messed with the keyboard in such a way that if I alt+tab to anther application, it will have hijacked my keyboard so that my keyboard doesn’t work as expected, or if Notepad++ exits and it still has hijacked my keyboard, I will immediately delete that plugin or script, and never trust anything written by that developer again. (The only application that I would be willing to have hijack the keyboard even when you are in another application is something like AutoHotKey, whose sole purpose is to provide hotkeys in situations where local hotkeys aren’t an option.)

                        John Doe 1J 1 Reply Last reply Reply Quote 0
                        • John Doe 1J
                          John Doe 1 @PeterJones
                          last edited by

                          @peterjones Yeah that is my concern, I’m gonna work on this some more later to see if I can get the script all cleaned up so that is no longer an issue. Thanks for your help so far!

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