• Login
Community
  • Login

Using findText in an open file in pythonScript macro

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
open text filefindtext macro
9 Posts 3 Posters 7.9k 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.
  • N
    Nolan Jorger
    last edited by Apr 18, 2016, 2:03 PM

    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:
    editor.findText(“DELIMITER”)
    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.

    1 Reply Last reply Reply Quote 1
    • D
      dail
      last edited by Apr 18, 2016, 2:45 PM

      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")
      
      1 Reply Last reply Reply Quote 2
      • N
        Nolan Jorger
        last edited by Apr 18, 2016, 4:46 PM

        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.

        Thanks

        1 Reply Last reply Reply Quote 0
        • D
          dail
          last edited by Apr 18, 2016, 5:19 PM

          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")
          editor.gotoPos(pos[0])
          
          1 Reply Last reply Reply Quote 0
          • N
            Nolan Jorger
            last edited by Apr 18, 2016, 5:44 PM

            Thank you, you have been very helpful.

            1 Reply Last reply Reply Quote 0
            • N
              Nolan Jorger
              last edited by Apr 25, 2016, 6:17 PM

              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?

              1 Reply Last reply Reply Quote 0
              • D
                dail
                last edited by Apr 25, 2016, 6:29 PM

                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:
                    editor.gotoPos(pos[0])
                
                1 Reply Last reply Reply Quote 0
                • J
                  Justin Brewster
                  last edited by Apr 25, 2016, 7:16 PM

                  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

                  help(editor.insertText)

                  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

                  1 Reply Last reply Reply Quote 1
                  • N
                    Nolan Jorger
                    last edited by Apr 26, 2016, 3:17 PM

                    Thank you all for your help.

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