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



  • 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.



  • @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



  • 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>



  • @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



  • @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()
    


  • @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)
    


  • 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.



  • @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.



  • 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.



  • @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?



  • 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



  • @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…



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


Log in to reply