• Login
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.
  • P
    Parth Kurani
    last edited by Jul 9, 2019, 5:37 PM

    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();
    
        }
    
    E 1 Reply Last reply Jul 10, 2019, 10:25 AM Reply Quote 0
    • R
      rinku singh
      last edited by Jul 10, 2019, 1:37 AM

      proc.StartInfo.UseShellExecute = false;

      1 Reply Last reply Reply Quote 2
      • E
        Ekopalypse @Parth Kurani
        last edited by Jul 10, 2019, 10:25 AM

        @Parth-Kurani

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

        1 Reply Last reply Reply Quote 1
        • P
          Parth Kurani
          last edited by Jul 10, 2019, 5:20 PM

          @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?

          R 1 Reply Last reply Jul 11, 2019, 3:03 PM Reply Quote 0
          • P
            Parth Kurani
            last edited by Jul 10, 2019, 5:48 PM

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

            E 1 Reply Last reply Jul 11, 2019, 10:30 AM Reply Quote 0
            • P
              PeterJones
              last edited by Jul 10, 2019, 7:06 PM

              @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
              • E
                Ekopalypse @Parth Kurani
                last edited by Jul 11, 2019, 10:30 AM

                @Parth-Kurani

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

                1 Reply Last reply Reply Quote 0
                • R
                  rinku singh @Parth Kurani
                  last edited by rinku singh Jul 11, 2019, 3:05 PM Jul 11, 2019, 3:03 PM

                  @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
                  2 out of 8
                  • First post
                    2/8
                    Last post
                  The Community of users of the Notepad++ text editor.
                  Powered by NodeBB | Contributors