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

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


  • @John-Murray

    Macros don’t always work as the user intends. Suggest you try a scripting language? For example, Pythonscript…here’s 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)

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


  • 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: 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.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
    File “C:\Users\JohnMurray\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\”, line 13
    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:


    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