• Login
Community
  • Login

Pythonscript plugin / console not working

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
19 Posts 6 Posters 1.6k 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.
  • F
    Fuel DZN
    last edited by Oct 3, 2023, 1:16 PM

    I made a script based off one of the FAQ entries to replace, divide, and round up some matched values:

    from Npp import editor
    
    import math
    
    def divide_2(m):
        return str(int(math.ceil((int(m.group(1)) / 2))))
    
    editor.rereplace(r'(?<=development = )(\d+'), divide_2);
    

    When it didn’t work I tried some simple test scripts:

    console.write(test)
    
    def test():
        print("test")
    
    test()
    

    These don’t seem to work as a console never appears. But this does:

    notepad.new()
    

    So python seems to be working in some way.

    Why do scripts 1, 2, and 3 not work? Am I supposed to be seeing a window/box spitting out errors? I’m not totally sure what I’m doing since I don’t code and only started doing some regex/python today.

    I have done some reinstalls of n++ and the plugin, looked at the plugin documentation, github, threads, videos, and I can’t find anything.

    A C 2 Replies Last reply Oct 3, 2023, 2:12 PM Reply Quote 2
    • A
      Alan Kilborn @Fuel DZN
      last edited by Oct 3, 2023, 2:12 PM

      @Fuel-DZN said in Pythonscript plugin / console not working:

      as a console never appears

      With the code you’ve shown, the PythonScript console won’t open if it is not already open. The simplest thing to try is to go to the plugin’s menu and get the console window open from there.

      BTW, code can write to the console even if it isn’t shown, and the data written will be buffered and shown once the console is (manually) opened.

      F 1 Reply Last reply Oct 4, 2023, 4:52 AM Reply Quote 3
      • M
        Mark Olson
        last edited by Mark Olson Oct 3, 2023, 2:32 PM Oct 3, 2023, 2:32 PM

        Off-topic, but I should also note that this specific task (doing math in a regex/replace) seems like the sort of thing that the Columns++ plugin is good for.

        P F 2 Replies Last reply Oct 3, 2023, 5:31 PM Reply Quote 2
        • C
          Coises @Fuel DZN
          last edited by Oct 3, 2023, 4:30 PM

          @Fuel-DZN said in Pythonscript plugin / console not working:

          editor.rereplace(r’(?<=development = )(\d+'), divide_2);

          I have no knowledge of Python, but if this is a copy/paste, it looks like it contains a typo — the second apostrophe should come after the adjacent closing parenthesis, should it not?

          I mention it because I don’t know whether you would get an error message in this context. Apologies for the interruption if it’s just a typo in the post.

          F 1 Reply Last reply Oct 4, 2023, 6:30 AM Reply Quote 2
          • P
            PeterJones @Mark Olson
            last edited by PeterJones Oct 3, 2023, 5:31 PM Oct 3, 2023, 5:31 PM

            @Mark-Olson said in Pythonscript plugin / console not working:

            off-topic, but I should also note that this specific task (doing math in a regex/replace) seems like the sort of thing that the Columns++ plugin is good for.

            I have now updated the Math Replacement FAQ to include a section about the Columns++ Plugin, because that is definitely a viable alternative to scripting plugin (Thanks @Coises : BTW, when are you going to submit Columns++ to the Plugins Admin list? It will be much easier for people to install your plugin if it’s in the official list.)

            C 1 Reply Last reply Oct 3, 2023, 6:43 PM Reply Quote 2
            • C
              Coises @PeterJones
              last edited by Oct 3, 2023, 6:43 PM

              @PeterJones said in Pythonscript plugin / console not working:

              BTW, when are you going to submit Columns++ to the Plugins Admin list ? It will be much easier for people to install your plugin if it’s in the official list.

              I want to feel confident that it is unlikely to misbehave for most people under most circumstances, and very unlikely to do something like hang and make a user force-close Notepad++, losing work in progress. I’m not yet confident of that — bugs are still turning up.

              I also want to feel confident that I am unlikely to make “architectural” changes which could introduce new instability, or user interface changes that could disrupt routines of anyone who had already come to rely on it working the way it does. I’m close to considering that part acceptable, though I’m still a little troubled that the Elastic tabstops feature is not as efficient as I’d like. (Variable-pitch fonts are one major cause of inefficiency, but I doubt people use those a lot in Notepad++ anyway, so I’m willing to accept the idea that if you have a large file and you’re not monospaced, don’t expect Elastic tabstops to perform well. However, large rectangular selections, as in thousands of lines, are a problem even in fixed-pitch, and I wish I could make them faster. As I recall, the “old” version of the Elastic tabstops plugin just let unexpected-to-useless results occur when you did something like click near the beginning of a file, scroll to the end, and then Shift+Alt+click to make a rectangular selection; and the “new” version of Elastic tabstops was apparently abandoned by its author shortly after he pushed to get it accepted as a replacement for the old official plugin.)

              I know I won’t get the amount of field testing I would like to make it official, though, until after I’ve made it official…

              1 Reply Last reply Reply Quote 3
              • F
                Fuel DZN @Mark Olson
                last edited by Oct 4, 2023, 4:46 AM

                @Mark-Olson Can it do multiple files at once? I also can’t quite figure out how to divide with it.

                C 1 Reply Last reply Oct 4, 2023, 5:09 AM Reply Quote 0
                • F
                  Fuel DZN @Alan Kilborn
                  last edited by Oct 4, 2023, 4:52 AM

                  @Alan-Kilborn Sorry I should have mentioned that ‘Show Console’ would be on in the plugin menu but nothing is visible. When when I activate it after opening n++ I see something very briefly flash but that’s it.

                  1 Reply Last reply Reply Quote 0
                  • C
                    Coises @Fuel DZN
                    last edited by Oct 4, 2023, 5:09 AM

                    @Fuel-DZN said in Pythonscript plugin / console not working:

                    @Mark-Olson Can it do multiple files at once? I also can’t quite figure out how to divide with it.

                    The Search in indicated region function in Columns++ only works on a region (which can be the entire file) in the active tab. The initial motivation for this function was to be able to search within a rectangular selection. It is unlikely that I’ll extend it to processing multiple tabs or files at once, since the notion of an “indicated region” wouldn’t make sense in that context.

                    If I’m following the intent of your Python, you would want:

                    Find what : (?<=development = )\d+
                    Replace with : (?=ceil(this/2))

                    1 Reply Last reply Reply Quote 1
                    • F
                      Fuel DZN @Coises
                      last edited by Fuel DZN Oct 4, 2023, 8:09 AM Oct 4, 2023, 6:30 AM

                      @Coises Thank you, I didn’t see that - it works now.

                      Since I need it for multiple files, I used this, but it doesn’t work:

                      from Npp import *
                      import math
                      
                      def divide_2(m):
                          return str(int(float(math.ceil((float(m.group(1)) / 2)))))
                      
                      for (filename, bufferID, index, view) in notepad.getFiles():
                              notepad.activateBufferID(bufferID)
                              print(notepad.getCurrentFilename()
                              editor.rereplace(r'(?<=development = )(\d+)', divide_2);
                              notepad.save()
                      

                      If anyone with experience in python (or an eye for typos) sees what’s wrong with this please let me know. (I’m not sure if I’m supposed to create a new post for this.)

                      P 1 Reply Last reply Oct 4, 2023, 12:58 PM Reply Quote 0
                      • P
                        PeterJones @Fuel DZN
                        last edited by Oct 4, 2023, 12:58 PM

                        @Fuel-DZN said in Pythonscript plugin / console not working:

                        print(notepad.getCurrentFilename()

                        unbalanced parentheses. It should be print(notepad.getCurrentFilename())

                        A 1 Reply Last reply Oct 4, 2023, 4:02 PM Reply Quote 3
                        • A
                          Alan Kilborn @PeterJones
                          last edited by Oct 4, 2023, 4:02 PM

                          OP really needs to get the console window working, because if other forum members have to spot such simple typos as print(notepad.getCurrentFilename() then we’ve got a problem…

                          P 1 Reply Last reply Oct 4, 2023, 5:29 PM Reply Quote 2
                          • P
                            PeterJones @Alan Kilborn
                            last edited by Oct 4, 2023, 5:29 PM

                            @Fuel-DZN said,

                            ‘Show Console’ would be on in the plugin menu but nothing is visible

                            Can you show a screenshot of the Plugins > Python Script menu, showing the checkmark really is on the Show Console entry?

                            Also, I think you should read the FAQ on Find Results – don’t be confused: that FAQ focuses on the Search Results window, but all the information about docking (where the docking resize are when a docked panel is tiny, or resetting the docking by editing the config file) are applicable to the PythonScript console as well, since it uses the same docking interface as other panels. (The only thing that’s not applicable from that FAQ is that F7 won’t toggle you to get into the PythonScript console panel, unlike the Search Results)

                            F 1 Reply Last reply Oct 12, 2023, 6:15 AM Reply Quote 2
                            • F
                              Fuel DZN @PeterJones
                              last edited by Oct 12, 2023, 6:15 AM

                              @PeterJones said in Pythonscript plugin / console not working:

                              Can you show a screenshot of the Plugins > Python Script menu, showing the checkmark really is on the Show Console entry?

                              a1.png

                              EkopalypseE 1 Reply Last reply Oct 12, 2023, 6:54 AM Reply Quote 0
                              • EkopalypseE
                                Ekopalypse @Fuel DZN
                                last edited by Oct 12, 2023, 6:54 AM

                                @Fuel-DZN

                                Can you take the same screenshot but with the whole npp window?

                                P 1 Reply Last reply Oct 12, 2023, 1:33 PM Reply Quote 1
                                • P
                                  PeterJones @Ekopalypse
                                  last edited by PeterJones Oct 12, 2023, 1:35 PM Oct 12, 2023, 1:33 PM

                                  @Ekopalypse said in Pythonscript plugin / console not working:

                                  @Fuel-DZN

                                  Can you take the same screenshot but with the whole npp window?

                                  @Fuel-DZN ,

                                  While you’re at it, also File > Open %AppData%\Notepad++\config.xml , search for GUIConfig name="DockingManager" and then copy the DockingManager section from that file, paste it into your reply, select it and click the </> toolbar button in the forum edit-your-post window, which will make it look something like this in your post:

                                  <GUIConfig name="DockingManager" leftWidth="200" rightWidth="200" topHeight="200" bottomHeight="200">
                                          <PluginDlg pluginName="Notepad++::InternalFunction" id="42052" curr="0" prev="-1" isVisible="yes" />
                                          <PluginDlg pluginName="Notepad++::InternalFunction" id="44084" curr="1" prev="-1" isVisible="yes" />
                                          <PluginDlg pluginName="Notepad++::InternalFunction" id="44080" curr="3" prev="-1" isVisible="yes" />
                                          <ActiveTabs cont="0" activeTab="0" />
                                          <ActiveTabs cont="1" activeTab="0" />
                                          <ActiveTabs cont="2" activeTab="-1" />
                                          <ActiveTabs cont="3" activeTab="0" />
                                  </GUIConfig>
                                  

                                  That section will tell us whether the problem was, as I suspected, that you have just shrunk your PythonScript console panel too small for you to notice. (Please note, the FAQ I linked you to above already told you to go looking in that file, and told you how to reset that section so that it will likely fix your problem; given your lack of mention in your reply, I am going to assume that you didn’t notice that piece of advice from my previous post.)

                                  F 1 Reply Last reply Oct 21, 2023, 9:20 AM Reply Quote 3
                                  • F
                                    Fuel DZN @PeterJones
                                    last edited by Fuel DZN Oct 21, 2023, 9:35 AM Oct 21, 2023, 9:20 AM

                                    @PeterJones

                                    Sorry for the late reply.

                                    Screenshot (105).png

                                    <GUIConfig name="DockingManager" leftWidth="247" rightWidth="200" topHeight="373" bottomHeight="0">
                                                <FloatingWindow cont="4" x="13" y="173" width="1635" height="966" />
                                                <PluginDlg pluginName="Notepad++::InternalFunction" id="44070" curr="0" prev="-1" isVisible="yes" />
                                                <PluginDlg pluginName="Python Script" id="-1" curr="3" prev="-1" isVisible="yes" />
                                                <PluginDlg pluginName="Notepad++::InternalFunction" id="0" curr="2" prev="4" isVisible="no" />
                                                <PluginDlg pluginName="Notepad++::InternalFunction" id="44084" curr="1" prev="-1" isVisible="no" />
                                                <PluginDlg pluginName="NppExec.dll" id="5" curr="3" prev="4" isVisible="yes" />
                                                <PluginDlg pluginName="Explorer.dll" id="0" curr="0" prev="-1" isVisible="no" />
                                                <ActiveTabs cont="0" activeTab="0" />
                                                <ActiveTabs cont="1" activeTab="-1" />
                                                <ActiveTabs cont="2" activeTab="-1" />
                                                <ActiveTabs cont="3" activeTab="-1" />
                                            </GUIConfig>
                                    

                                    (Indentation is messed up but I don’t know how to fix that.)

                                    I tried the FAQ suggestions which did not solve it.

                                    In case it is helpful, earlier I said:

                                    When when I activate it after opening n++ I see something very briefly flash but that’s it.

                                    P 1 Reply Last reply Oct 21, 2023, 4:18 PM Reply Quote 0
                                    • P
                                      PeterJones @Fuel DZN
                                      last edited by PeterJones Oct 21, 2023, 5:09 PM Oct 21, 2023, 4:18 PM

                                      <GUIConfig name="DockingManager" leftWidth="247" rightWidth="200" topHeight="373" bottomHeight="0">
                                      

                                      I tried the FAQ suggestions which did not solve it.

                                      Do you see that bottomHeight="0" ? That is the culprit, and the FAQ explains how to fix it.

                                      Also, the up-down arrow in the right place would have fixed it also. This gap from your screenshot is exacty the gap I was referencing in the FAQ. I have added the arrow to show you where to hover your mouse cursor to find it.
                                      797e3acf-d3bf-4fe1-907a-d6edc9a97fb8-image.png

                                      F 1 Reply Last reply Oct 22, 2023, 11:08 AM Reply Quote 2
                                      • F
                                        Fuel DZN @PeterJones
                                        last edited by Oct 22, 2023, 11:08 AM

                                        @PeterJones Thank you, resizing the panel just fixed it. I don’t know why it didn’t work before.

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