Using findText in an open file in pythonScript macro

  • I am trying to write a macro in pythonScript.

    I want to find a text string and insert some text at that position.

    When I try to use the findText method in my script and run the script on my file I get this in the console:
    Boost.Python.ArgumentError: Python argument types in
    Editor.findText(Editor, str)
    did not match C++ signature:
    findText(class NppPythonScript::ScintillaWrapper {lvalue}, int flags, int start, int end, class boost::python::api::object ft)

    The only information I can find on the Scintilla methods is:

    Editor.findText(flags, start, end, ft) → object

    Find some text in the document.
    See Scintilla documentation for SCI_FINDTEXT

    There are no examples of to use these methods and there is no explanation of what these error messages mean. When I click on the SCI_FINDTEXT link I see a completely different set of parameters than what are mentioned above.

    Can someone please give me an example of how to use this method.

    Thank you.

  • The wrapper has to deviate from the Scintilla version of SCI_FINDTEXT since that takes a structure with some values filled in.

    The documentation you found on that PythonScript method is correct. It takes more parameters than just the text. As you stated, the method is:

    Editor.findText(flags, start, end, ft)

    flags is how you want it to search. Use a combination of these options.

    start is the position you want to start searching.

    end is the position you want to stop searching.

    ft is of course the text you want to find.

    So for example you can try something like:

    editor.findText(FINDOPTION.WHOLEWORD | FINDOPTION.MATCHCASE, 0, editor.getLength(), "DELIMITER")

  • Thank you for your quick answer. My question now is: How do I use that to set the current position?

    When I output the result to the console I get (547, 559). I assume these are the start and end positions of the text that was found.


  • You are correct. It returns a tuple (think of it as an array that you can’t change). You index it just like an array as well.

    pos = editor.findText(FINDOPTION.WHOLEWORD | FINDOPTION.MATCHCASE, 0, editor.getLength(), "DELIMITER")

  • Thank you, you have been very helpful.

  • How would I handle the condition when the search fails to find the searched for text? Is there an error thrown that I can trap?

  • You can run commands in the console to test things out and see what they return. If it fails it returns None.

    pos = editor.findText(FINDOPTION.WHOLEWORD | FINDOPTION.MATCHCASE, 0, editor.getLength(), "DELIMITER")
    if pos is not None:

  • Just for reference in the console you can run help() on a lot of the editor functions and it will give you some idea how to use them. Not sure if you tried that already, but I found running dir(editor) to get a list of all the available functions to be very useful.

    For instance if you run


    it returns

    Help on method insertText:
    insertText(...) method of Npp.Editor instance
        insertText( (Editor)arg1, (int)pos, (object)text) -> None :
            Insert string at a position.

    Hope that helps a little too :D

  • Thank you all for your help.