Community
    • Login

    Invoking python script in plugin method

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    8 Posts 4 Posters 1.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.
    • Parth KuraniP
      Parth Kurani
      last edited by

      I am using NPPlugins.NET for plugin generation. Right now, I am trying to invoke a python script and pass in the current document into it; however, the script itself does not seem to be executing. Here is the code:

          internal static void GetCurrentDocumentText()
          {
              IntPtr curScintilla = PluginBase.GetCurrentScintilla();
              String s = GetDocumentText(curScintilla);
              string progToRun = "C:\\test.py";
      
              Process proc = new Process();
              proc.StartInfo.FileName = "C:\\Users...\\python.exe";
      
              proc.StartInfo.UseShellExecute = true;
              proc.StartInfo.RedirectStandardOutput = false;
      
              proc.StartInfo.Arguments = string.Concat(progToRun, " ", s)
              proc.Start();
      
              proc.WaitForExit();
      
          }
      
      EkopalypseE 1 Reply Last reply Reply Quote 0
      • rinku singhR
        rinku singh
        last edited by

        proc.StartInfo.UseShellExecute = false;

        1 Reply Last reply Reply Quote 2
        • EkopalypseE
          Ekopalypse @Parth Kurani
          last edited by

          @Parth-Kurani

          Just curious, you know that there is a python script plugin available, don’t you?

          1 Reply Last reply Reply Quote 1
          • Parth KuraniP
            Parth Kurani
            last edited by

            @gurikbal-singh Even when I set useShellExecute to false, the python script is still not being invoked. The txt file that my python script is supposed to generate cannot be found anywhere. Any advice on how to resolve this issue?

            rinku singhR 1 Reply Last reply Reply Quote 0
            • Parth KuraniP
              Parth Kurani
              last edited by

              @Ekopalypse are you referring to PyNPP? If so, it doesn’t have all the capabilities that I would like.

              EkopalypseE 1 Reply Last reply Reply Quote 0
              • PeterJonesP
                PeterJones
                last edited by

                @Parth-Kurani said:

                are you referring to PyNPP

                The Python Script plugin is not PyNPP (which I had not heard of until now). It is a plugin which embeds a full Python 2.7 interpreter inside the Notepad++ environment, and allows you to run a script from within Notepad++ that gives Python direct access to the contents of all the files open in Notepad++, along with access to many aspects of the Notepad++ GUI.

                For instructions on how to install the Python Script in Notepad++ v7.6.3 and later, see this Guide by @Meta-Chuh (because, unfortunately, Python Script plugin doesn’t currently install using the Plugins Admin interface).

                1 Reply Last reply Reply Quote 2
                • EkopalypseE
                  Ekopalypse @Parth Kurani
                  last edited by

                  @Parth-Kurani

                  No, i was referring to the one @PeterJones mentioned.

                  1 Reply Last reply Reply Quote 0
                  • rinku singhR
                    rinku singh @Parth Kurani
                    last edited by rinku singh

                    @Parth-Kurani said:

                    @gurikbal-singh Even when I set useShellExecute to false, the python script is still not being invoked. The txt file that my python script is supposed to generate cannot be found anywhere. Any advice on how to resolve this issue?

                    proc.StartInfo.Arguments = string.Concat(progToRun, " ", s)

                             try
                            {
                                Process Process = new Process();
                                string keyValues = currentDirectory() + "\\User_Command_Line_Tools_PATH";
                                string keyValue = currentDirectory() + "\\GnuWin32\\bin";
                                string envVar = Process.StartInfo.EnvironmentVariables["path"];
                                if (envVar != null)
                                {
                                    string value = envVar + ";" + keyValue + ";" + keyValues;
                                    Process.StartInfo.EnvironmentVariables["path"] = value;
                                }
                                else
                                {
                                    Process.StartInfo.EnvironmentVariables.Add("path", keyValue);
                                    Process.StartInfo.EnvironmentVariables.Add("path", keyValues);
                                }
                                Process.StartInfo.FileName = windir() + "\\cmd.exe";
                                Process.StartInfo.Arguments = "/c " + richTextBox1.Text;
                                if (filepath().ToString() == filex().ToString())
                                {
                                    Process.StartInfo.WorkingDirectory = currentDirectory() + "\\GnuWin32\\bin";
                                }
                                else
                                {
                                    Process.StartInfo.WorkingDirectory = path.ToString();
                                }
                                Process.StartInfo.RedirectStandardError = true;
                                Process.StartInfo.RedirectStandardOutput = true;
                                Process.StartInfo.RedirectStandardInput = true;
                                Process.StartInfo.UseShellExecute = false;
                                Process.StartInfo.CreateNoWindow = true;
                                Process.Start();
                                string lined = Process.StandardOutput.ReadToEnd();
                                string line = Process.StandardError.ReadToEnd();
                                if (String.IsNullOrEmpty(lined))
                                {
                                    richTextBox2.AppendText(line + Environment.NewLine);
                                }
                                if (String.IsNullOrEmpty(line))
                                {
                                    richTextBox2.AppendText(lined + Environment.NewLine);
                                }
                                Process.WaitForExit();
                                if (checkBox2.Checked == true)
                                {
                                    Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_SETTEXT, 0, richTextBox2.Text);
                                }
                    
                            }
                            catch (Exception ex)
                            {
                                System.Diagnostics.Debug.Write(ex.Message);
                            }
                    
                    1 Reply Last reply Reply Quote 1
                    • First post
                      Last post
                    The Community of users of the Notepad++ text editor.
                    Powered by NodeBB | Contributors