Community
    • Login

    Need help on Macro that prompts for number of rows to select

    Scheduled Pinned Locked Moved General Discussion
    13 Posts 3 Posters 3.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.
    • Claudia FrankC
      Claudia Frank @John Murray
      last edited by Claudia Frank

      @John-Murray

      Looks like the macro doesn’t wait for the dialog to end but continues with the other statements.
      Can you split the macro in two parts or is it only those 3 steps to do, namely
      begin select - move cursor - end select?

      Cheers
      Claudia

      1 Reply Last reply Reply Quote 1
      • Scott SumnerS
        Scott Sumner @John Murray
        last edited by

        @John-Murray

        Macros don’t always work as the user intends. Suggest you try a scripting language? For example, Pythonscript…here’s SelectFromCurrentLineToADestinationLIne.py which I think meets your spec:

        def SFCL2ADL__main():
            line1 = editor.lineFromPosition(editor.getCurrentPos())
            line_count = editor.getLineCount()
            user_input = notepad.prompt(
                'Enter line # from 1 to {}:'.format(line_count),
                'Select from line {} to a specified line'.format(line1 + 1),
                '')
            try: line2 = int(user_input) - 1
            except: return
            if not (0 <= line2 <= line_count - 1): return
            pos1 = editor.positionFromLine(line1 + (1 if line2 < line1 else 0))
            pos2 = editor.positionFromLine(line2 + (0 if line2 < line1 else 1))
            editor.setSelection(pos2, pos1)
        
        SFCL2ADL__main()
        
        1 Reply Last reply Reply Quote 2
        • Scott SumnerS
          Scott Sumner
          last edited by Scott Sumner

          @John-Murray

          Additionally, if you want the destination line to be visible when you are done, add this new line between the pos2 =... line and the editor.setSelection(... line:

          editor.gotoLine(line2)
          
          1 Reply Last reply Reply Quote 1
          • John MurrayJ
            John Murray
            last edited by

            Thanks Scott. However, I’ll admit now I’m super dumb on how to have Notepad++ run the script. I copied the script and saved in the file name you had in the Notepad++ directory. I then tried Menu/Run and pointed to that file - W10 opened that file up in “Code Writer”- I don’t think that is what was supposed to happen.

            Scott SumnerS 1 Reply Last reply Reply Quote 2
            • Scott SumnerS
              Scott Sumner @John Murray
              last edited by Scott Sumner

              @John-Murray

              Scripting languages aren’t built in; they’re plugins. Get the Pythonscript plugin via the Plugin Manager. Once installed, select Plugins -> Pythonscript -> New Script. Give it a name (suggest: SelectFromCurrentLineToADestinationLIne.py) and a new file will open up with that name. Paste the contents from earlier in there and save the file. Then choose Plugins -> Pythonscript -> Scripts and choose the script which will cause it to execute in the current file. That script selection method is just for testing everything is working. When you determine that it is, you can bind it to a keycombo of your choosing (via the Plugins -> Pythonscript -> Configuration menu). If you run into trouble, just ask here again.

              1 Reply Last reply Reply Quote 2
              • John MurrayJ
                John Murray
                last edited by

                Thanks Scott for such detailed instructions. I followed them and it worked like a champ. However, when I did add the line to display the destination line, the script appeared to do nothing - so I removed it and it worked again.

                Scott SumnerS 1 Reply Last reply Reply Quote 1
                • Scott SumnerS
                  Scott Sumner @John Murray
                  last edited by

                  @John-Murray

                  Did you add it at the same indent level as the lines around it?

                      ...
                      pos2 = editor.positionFromLine(line2 + (0 if line2 < line1 else 1))
                      editor.gotoLine(line2)
                      editor.setSelection(pos2, pos1)
                      ...
                  

                  After running the script is there any information (in the form of “errors”) shown in this window?: Plugins -> Pythonscript -> Show Console

                  Is your file long enough to not all fit on screen at the same time, and your destination line specified is not on-screen when the script is run?

                  1 Reply Last reply Reply Quote 1
                  • John MurrayJ
                    John Murray
                    last edited by

                    Yes I have it at the same indent level, the file has 23K lines, and yes I do get an error in the console window:

                    Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)]
                    Initialisation took 313ms
                    Ready.
                    File “C:\Users\JohnMurray\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\SelectFromCurrentLineToADestinationLIne.py”, line 13
                    editor.gotoLine(line2)
                    ^
                    IndentationError: unexpected indent

                    Scott SumnerS 1 Reply Last reply Reply Quote 0
                    • Scott SumnerS
                      Scott Sumner @John Murray
                      last edited by Scott Sumner

                      @John-Murray

                      So very likely is that you have a “tab character” in there. If you turn visible-whitespace on (View (menu) -> Show Symbol -> Show White Space and TAB), I would think you would see something like this:

                      Imgur

                      See that long arrow…highlighted in yellow for me (wouldn’t be yellow for you)…that’s going to be the problem. If you go in and delete that arrow, and then put 4 spaces at the beginning of that line, I think that will solve it.

                      Of course, I could be wrong…

                      1 Reply Last reply Reply Quote 3
                      • John MurrayJ
                        John Murray
                        last edited by

                        You were right - that took care of it - and everything works perfectly now. Thanks a bunch Scott for hanging in there with me.

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