Community
    • Login

    NppExec can't call python script through shortcut?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    6 Posts 2 Posters 2.2k 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.
    • 古旮古
      古旮
      last edited by

      I tried to invoke a python script through NppExec plugin with these 2 lines below.
      npp_save "D:\Notepad++_7.5.8\plugins\Config\PythonScript\scripts\someScript.py"
      npp_menucommand "Plugins\Python Script\Scripts\someScript"

      It worked fine.
      But when I invoke this NppExec script by pressing Ctrl+F6, the script could not invoke the python script. But npp_save was successfully processed.
      However, if I use mouse to click on Direct Execute Previous, instead of using shortcut, the python script was run successfully.

      Then I tried to put that NppExec script under **Macro **menu and assigned it a shortcut. I was all the same. When I click it, it can invoke the python script. But when I call the NppExec script via shortcut, the python script doesn’t run.

      Is it a bug of NppExec plug-in?

      Eko palypseE 1 Reply Last reply Reply Quote 0
      • Eko palypseE
        Eko palypse @古旮
        last edited by

        @古旮

        it is expected that the format for npp_menucommand is <menu\item\name>
        This doesn’t fit with the layout Plugins\Python Script\Scripts\someScript",
        so I assume it is needed to bring the python script into the main pythonscript menu level.

        May I ask you what your ultimate goal is?

        Eko

        1 Reply Last reply Reply Quote 0
        • 古旮古
          古旮
          last edited by 古旮

          @Eko-palypse
          The NppExec script is alright. It runs OK both in command line and when I click on Direct Execute Previous(Ctrl+F6). The problem is when I run the NppExec script by pressing shortcut, the second line(npp_menucommand "Plugins\Python Script\Scripts\someScript") of that NppExec script doesn’t run. Only the first line(npp_save "D:\Notepad++_7.5.8\plugins\Config\PythonScript\scripts\someScript.py" ) is run.

          1 Reply Last reply Reply Quote 0
          • 古旮古
            古旮
            last edited by

            Since you ask, my goal is just to save the python script file and run it within 1 shortcut.

            Eko palypseE 1 Reply Last reply Reply Quote 0
            • Eko palypseE
              Eko palypse @古旮
              last edited by

              @古旮 said:

              npp_menucommand “Plugins\Python Script\Scripts\someScript”

              To be honest, this does not work for me but if someScript is located one folder up like
              “Plugins\Python Script\someScript” then it works.

              Nevertheless I’m still unsure what you try to do.
              If you want to run python scripts with a locally installed python interpreter,
              then I guess you should do it differently if using NppExec

              Define something like this

              NPP_SAVE
              cd $(CURRENT_DIRECTORY)
              python.exe $(FULL_CURRENT_PATH)
              

              Assuming that python can be found otherwise replace with full path to python.exe
              This gives you also the benefit of being able to test different python interpreters
              and you don’t need to hard code the path of the python script.

              But if you want to execute python script via PythonScript plugin then I would say
              do either import it or use exec/execfile with something like

              from Npp import editor, notepad
              
              def run(selectedCode):
                  _file = notepad.getCurrentFilename()
                  if selectedCode:
                      print "running content from", _file
                      exec(editor.getTextRange(*editor.getUserCharSelection()))
                  else:
                      if editor.getLine(0).startswith("#NEW_NAMESPACE"):
                          print "running in own namespace",  _file
                          ns = {}
                          ns["__name__"] = "__main__"
                          exec(editor.getText(), ns, ns)
                      else:
                          print "running in current namespace",  _file
                          exec(editor.getText())
              
              run(not editor.getSelectionEmpty())
              

              and if you still want to save it before executing it, put a
              notepad.save() before run(not editor.getSelectionEmpty())

              Eko

              古旮古 1 Reply Last reply Reply Quote 2
              • 古旮古
                古旮 @Eko palypse
                last edited by

                @Eko-palypse
                Thank you for the reply. I tested again and still found the code works for me.
                But there is one thing I forgot to mention in my original post.
                When I call the NppExec script through shortcut(as opposed to mouse click), the command npp_menucommand "Plugins\Python Script\Scripts\someScript" still runs, but it doesn’t invoke the python script. Instead, it opens that *.py file in notepad++.
                Only if I call the same NppExec script by mouse clicking on its menu item, or through NppExec console, will that line of NppExec command invoke the specified python script, as expected.
                So, from my point of view, that must be a bug of NppExec. To be more specific, the npp_menucommand command’s bug.

                Here’s my versions below, in case anyone with the same versions as mine can reproduce the same peoblem:
                My Npp version: Notepad++ v7.5.8
                My NppExec version: ver. 0.6 RC2 Unicode for Notepad++
                My python script version: 1.0.8.0

                NppExec 0.6 RC2 seems to be the newest version, so maybe I will upgrate my python script to 1.3 , to see if that’ll fix the problem, or still have the problem.

                And thank you again for the info and solution you provided. I’ll look into that later.

                1 Reply Last reply Reply Quote 0
                • Vitalii DovganV Vitalii Dovgan referenced this topic on
                • First post
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors