Community
    • Login

    PythonScript Toggleable Script?

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    34 Posts 3 Posters 3.5k 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 @PeterJones
      last edited by PeterJones

      @john-doe-1

      So here are some example screenshot-videos of using the two

      I assigned the ⇅ button to the “toggle” (the post that has the script with "alternate paradigm" in the comments).

      For the ⇅ , I record the sequence

      1. start selection with a click
      2. click the ⇅ button to start the rectangle/column selection
      3. end selection with a click
      4. click the ⇅ button to end the rectangle/column selection, and convert it to visually be a rectangle selection
      5. start new selection with a click
      6. click the ⇅ button to start the rectangle/column selection
      7. extend new selection with SHIFT+arrow
      8. click the ⇅ button to end the rectangle/column selection, and convert it to visually be a rectangle selection

      You will see that the status bar reflects the correct state of when you have clicked ⇅ the first time (so the script is in “active” mode), and that it clears itself when you do the second ⇅ (so it’s converted)

      ---------------------

      a1fa538a-4877-4745-ae14-de90d27d0b9d-image.png I assigned the other button to the “simplest” – the three-line version shown in my later post.

      For the a1fa538a-4877-4745-ae14-de90d27d0b9d-image.png , I recorded the sequence

      1. start the selection with a click
      2. extend the selection with SHIFT+ARROW
      3. convert the selection to rectangular by calling the 🛈 script

      This doesn’t show the status, because the button is a one-time thing, not a “in the active mode” thing.

      Either should work. Both avoid any low-level programming or callbacks – they just use standard, simple PythonScript calls.

      The second has the benefit of being really simple, and doesn’t even need to save state. If you or your user want to convert a normal selection to rectangle, just click the button after making the selection. Don’t need to worry about the status bar. Easiest to use. Easiest to maintain. You can always decide to use that to change any stream selection into a rectangle, whether you were thinking “I need to select a rectangle” or not. Fewer clicks.

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

        So…all this is great, but I really don’t see an advantage to it (the whole concept), and this is why:

        • if you’re a dedicated keyboardist, you’d have to dive for the mouse in order to hit the toolbar button to change the mode, spoiling being dedicated to the keyboard (where you could just add Alt to your Shift+arrows movement to get a column block).

        • if you’re not in love with keyboard-only actions, use the mouse to select text via click and drag; if you start this as a stream selection, you can add a press of Alt to it while you are dragging (can even just tap-n-release Alt) in order to change the selection to a column block type.

        But…people will want what they will want, and that’s ok. :-)

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

          Okay, going back to what the this thread was originally about, my toggle function is not working as I thought it would. If I go to click it to toggle it off, I get the following messagebox text:

          “Another script is currently running. Running two scripts at the same time could produce unpredictable results, and is therefore disabled.”

          I’m wondering how I can make it so that the user can break the infinite loop in my script by clicking on the button for the script again, essentially making it toggleable. Anyone got an idea? It would be much appreciated.

          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?:

            how I can make it so that the user can break the infinite loop in my script

            Possible solution: Don’t put an infinite loop in the script in the first place?

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

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

              I’m wondering how I can make it so that the user can break the infinite loop in my script by clicking on the button for the script again

              By writing it without an infinite loop. The three line script I supplied is all you need to be able to easily convert a normal selection to a rectangle/column selection after the selection is made. Or the ⇅ script I showed you will allow a toggle-on/toggle-off without requiring an infinite loop.

              I cannot say it any more plainly.

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

                @alan-kilborn @PeterJones The reason for the infinite loop is that I want to make the script act like a toggle that works for not only arrow keys but also clicking and dragging for selection. I have the following statement in my infinite loop (among others):

                editor.setSelectionMode(SELECTIONMODE.RECTANGLE)

                This is the only way to be able to click and drag rectangle select persistently, it works for arrow key selection as well, only thing is if it is not in an infinite loop then that functionality no longer works.

                Edit: Is there a function that can check if another PythonScript is being launched? That would be a good way to break the loop.

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

                  @john-doe-1

                  Did you not try my scripts? Because both ⇅ and the “simplest” will work, whether you use the keyboard or clicking and dragging. With ⇅ , click where you want to start the selection, then run the script, then click-and-drag to the end of the selection, then run the script, and it will convert that click-and-drag-selection to rectangle. With the “simplest” script, just click-and-drag your selection, then run the script, and it will convert that click-and-drag-selection to rectangle.

                  The infinite loop will not work for you. Stop trying to make it work. It is the wrong design. Every one of the problems you have run across has proven that to everyone (except you, who seems unwilling to accept an alternate solution that works better).

                  I have already written multiple scripts, and shown you how it works, in words and in video. If you are unwilling to go this route, I cannot help you beyond this. I am sorry. Good luck.

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

                    @john-doe-1

                    In case you don’t know which I mean by simplest, here is the version I have saved as SelectionToRectangle.py in my instance. I am actually going to use that in my workflow occasionally, because I am sometimes on a linux box and remote-desktop-connecting into my windows laptop, and the ALT key doesn’t transmit properly, so previously I wasn’t able to column selection; now I have a simple way to easily switch a normal selection to column selection, without using the ALT key.

                    # encoding=utf-8
                    """
                    Derived from 22890-simplest.py, in response to https://community.notepad-plus-plus.org/topic/22890/
                    
                    This is the simplest paradigm for converting a stream (normal) selection to rectangular (column):
                    1) Click or arrow to where you want to start selecting
                    2) Shift+Click or Shift+arrow to get to the end of the selection (ie, do a normal selection)
                    3) Run this script to convert the selection from STREAM to rectangle and refresh the screen automatically to see it
                    """
                    from Npp import notepad, editor, SELECTIONMODE, STATUSBARSECTION
                    editor.setSelectionMode( SELECTIONMODE.RECTANGLE )
                    notepad.activateFile(notepad.getCurrentFilename()) # use the activateFile() command to refresh UI; otherwise, it doesn't _look_ like column/rectangle select)
                    
                    1 Reply Last reply Reply Quote 1
                    • PeterJonesP PeterJones referenced this topic on
                    • John Doe 1J
                      John Doe 1 @PeterJones
                      last edited by John Doe 1

                      @peterjones Thank you, I do very much appreciate all of your help. It has helped greatly, I can be very picky trying to get a program to behave exactly how I want, my apologies for that, I understand it can be too much of a time sink and sometimes impossible.

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

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

                        I can be very picky trying to get a program to behave exactly how I want

                        Sure, I understand this; I have Notepad++ heavily customized with scripts to have it behave how I want. But…when you do something like this, you have to understand the limitations of the environment you are working within, in order to “not” overstep your bounds.

                        Using things which hold keys down, writing infinite loops, running multiple scripts before one is finished…all of these are bad ideas in the context of what PythonScript aims to provide to Notepad++ users.

                        I really don’t know how to provide advice on what to not do, though, until you do it and post here asking for advice on how to solve encountered problems. :-)

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

                          @alan-kilborn You’re exactly correct, this has been a great learning experience for me regarding Notepad++ plugin development. I plan to continue improving my current plugins and write new ones. Once again thank you @Alan-Kilborn and @PeterJones for pointing me in the right direction, you’ve been helped a great deal! : - )

                          1 Reply Last reply Reply Quote 1
                          • PeterJonesP PeterJones referenced this topic on
                          • PeterJonesP PeterJones referenced this topic on
                          • First post
                            Last post
                          The Community of users of the Notepad++ text editor.
                          Powered by NodeBB | Contributors