• Login
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.
  • J
    John Murray
    last edited by Jul 13, 2018, 4:22 PM

    Many times I have the need to take a large file with many rows and split it into multiple files - the number of lines can vary. I would like to have a macro that when I run it it will select a prompted number of lines that I can then copy/delete.

    Generally the macro would:

    1. Do Edit - Begin/End Select starting the block select.
    2. Present the dialog box for Cntl-G where I can specify the line number to go to.
    3. Execute the Cntl-G jumping me to the specified line number.
    4. Do Edit - Begin End Select ending the block select.
    5. Macro ends.

    I can record a macro that does the above except the next time I run the macro, it used the number of lines I typed in for the Cntl-G command without displaying the prompt.

    Thanks in advance for any guidance.

    C 1 Reply Last reply Jul 13, 2018, 11:31 PM Reply Quote 1
    • C
      Claudia Frank @John Murray
      last edited by Jul 13, 2018, 11:31 PM

      @John-Murray

      edit the shortcuts.xml and put the following line into the place needed

      <Action type="2" message="0" wParam="43004" lParam="0" sParam="" />

      This should do the trick to let you see the goto dialog.

      Cheers
      Claudia

      1 Reply Last reply Reply Quote 0
      • J
        John Murray
        last edited by Jul 15, 2018, 8:33 PM

        That didn’t quit work - it did give me the “goto” dialog, but it didn’t do the block select.

        Here is the macro I recorded:

        <Macro name=“Block Select” Ctrl=“no” Alt=“no” Shift=“no” Key=“0”>
        <Action type=“2” message=“0” wParam=“42020” lParam=“0” sParam=“” />
        <Action type=“0” message=“2024” wParam=“5000” lParam=“0” sParam=“” />
        <Action type=“2” message=“0” wParam=“42020” lParam=“0” sParam=“” />
        /Macro>

        C S 2 Replies Last reply Jul 15, 2018, 8:50 PM Reply Quote 2
        • C
          Claudia Frank @John Murray
          last edited by Claudia Frank Jul 15, 2018, 8:51 PM Jul 15, 2018, 8:50 PM

          @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
          • S
            Scott Sumner @John Murray
            last edited by Jul 15, 2018, 10:44 PM

            @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
            • S
              Scott Sumner
              last edited by Scott Sumner Jul 15, 2018, 10:54 PM Jul 15, 2018, 10:53 PM

              @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
              • J
                John Murray
                last edited by Jul 20, 2018, 12:17 AM

                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.

                S 1 Reply Last reply Jul 20, 2018, 12:49 AM Reply Quote 2
                • S
                  Scott Sumner @John Murray
                  last edited by Scott Sumner Jul 20, 2018, 12:50 AM Jul 20, 2018, 12:49 AM

                  @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
                  • J
                    John Murray
                    last edited by Jul 20, 2018, 4:05 PM

                    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.

                    S 1 Reply Last reply Jul 20, 2018, 5:42 PM Reply Quote 1
                    • S
                      Scott Sumner @John Murray
                      last edited by Jul 20, 2018, 5:42 PM

                      @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
                      • J
                        John Murray
                        last edited by Jul 22, 2018, 4:55 PM

                        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

                        S 1 Reply Last reply Jul 22, 2018, 5:43 PM Reply Quote 0
                        • S
                          Scott Sumner @John Murray
                          last edited by Scott Sumner Jul 22, 2018, 5:44 PM Jul 22, 2018, 5:43 PM

                          @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
                          • J
                            John Murray
                            last edited by Jul 22, 2018, 10:15 PM

                            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
                            2 out of 13
                            • First post
                              2/13
                              Last post
                            The Community of users of the Notepad++ text editor.
                            Powered by NodeBB | Contributors