• Login
Community
  • Login

Need command to restart Notepad ++

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
19 Posts 5 Posters 5.4k 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.
  • A
    andrecool-68
    last edited by Jun 3, 2019, 11:52 PM

    In the file shortcuts.xml I wrote this code:

    		<Command name="Restart" Ctrl="no" Alt="no" Shift="no" Key="0">Restart.bat</Command>
    

    It runs the Restart.bat file with this code:

    taskkill /f /im notepad++.exe
    start notepad++.exe
    

    Is there a simpler solution to restart Notepad ++ from the menu “Run”?

    D 1 Reply Last reply Jun 4, 2019, 6:31 AM Reply Quote 2
    • D
      dinkumoil @andrecool-68
      last edited by dinkumoil Jun 4, 2019, 7:26 AM Jun 4, 2019, 6:31 AM

      @andrecool-68

      IMHO your code is even too simple. The TASKKILL command doesn’t really kill a process, instead it sends a signal to a process requesting to terminate itself. This way an application can show a confirmation dialog when it has unsaved data/documents. Notepad++ behaves exactly like that. Thus the start notepad++ command may fail to start a new instance of Npp if it is set to single-instance-mode.

      That’s the code I use for the same purpose (VB Script). It expects as the first command line argument the path to the Npp directory.

      Option Explicit
      
      
      '-------------------------------------------------------------------------------
      'Variables declaration
      '-------------------------------------------------------------------------------
      Dim objFSO, objWshShell, objWMIService
      Dim colProcesses, objProcess, strInstanceQuery
      Dim colEvents, objEvent, strEventQuery, intInterval
      Dim strNppDirPath, strNppExeName, strNppExePath
      
      
      '-------------------------------------------------------------------------------
      'Variables initialization
      '-------------------------------------------------------------------------------
      Set objFSO        = CreateObject("Scripting.FileSystemObject")
      Set objWshShell   = CreateObject("WScript.Shell")
      Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2" )
      
      strNppExeName     = "notepad++.exe"
      intInterval       = 1
      
      
      '-------------------------------------------------------------------------------
      ' Request termination of running Npp, wait for its termination and restart it
      '-------------------------------------------------------------------------------
      If WScript.Arguments.Count > 0 Then
        strNppDirPath = WScript.Arguments(0)
        strNppExePath = objFSO.BuildPath(strNppDirPath, strNppExeName)
      
        If objFSO.FileExists(strNppExePath) Then
          strInstanceQuery = "SELECT * FROM Win32_Process" & _
                             " WHERE ExecutablePath = '" & EscapeForWMI(strNppExePath) & "'"
      
          strEventQuery    = "SELECT * FROM __InstanceOperationEvent" & _
                             " WITHIN " & intInterval & _
                             " WHERE TargetInstance ISA 'Win32_Process'" & _
                             " AND TargetInstance.ExecutablePath = '" & EscapeForWMI(strNppExePath) & "'"
      
          Set colProcesses = objWMIService.ExecQuery(strInstanceQuery)
      
          For Each objProcess In colProcesses
            objWshShell.Run "taskkill /im " & Quote(strNppExeName), 0, False
      
            Set colEvents = objWMIService.ExecNotificationQuery(strEventQuery)
      
            Do
              Set objEvent = colEvents.NextEvent()
      
              Select Case objEvent.Path_.Class
                Case "__InstanceDeletionEvent" Exit Do
              End Select
            Loop
      
            objWshShell.Run Quote(strNppExePath), 1, False
      
            Exit For
          Next
        End If
      End If
      
      
      
      
      '===============================================================================
      ' Surround a string with double quotes
      '===============================================================================
      
      Function Quote(ByRef strString)
        Quote = """" & strString & """"
      End Function
      
      
      '===============================================================================
      ' Escape special chars of string for using it with WMI
      '===============================================================================
      
      Function EscapeForWMI(ByRef strAString)
        EscapeForWMI = Replace(strAString, "\", "\\")
      End Function
      

      This is the required entry for shortcuts.xml:

      <Command name="Restart" Ctrl="no" Alt="no" Shift="no" Key="0">wscript.exe /nologo "<Path-To-Restart-Script>" "$(NPP_DIRECTORY)"</Command>
      
      M 1 Reply Last reply Jun 4, 2019, 7:43 AM Reply Quote 3
      • M
        Meta Chuh moderator @dinkumoil
        last edited by Jun 4, 2019, 7:43 AM

        @dinkumoil

        The TASKKILL command doesn’t really kill a process, instead it sends a signal to a process requesting to terminate itself.

        not quite. with the /f switch in taskkill /f /im notepad++.exe, the process will be terminated non graceful and immediately.
        but your script is cool.

        @andrecool-68

        you can put it directly into shortcut.xml, without an extra batch file:

        <Command name="Restart" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /c taskkill /f /im notepad++.exe && "FULL_PATH_TO_YOUR\notepad++.exe"</Command>
        
        D A 2 Replies Last reply Jun 4, 2019, 7:50 AM Reply Quote 3
        • D
          dinkumoil @Meta Chuh
          last edited by dinkumoil Jun 4, 2019, 7:55 AM Jun 4, 2019, 7:50 AM

          @Meta-Chuh said:

          with the /f switch in taskkill /f /im notepad++.exe, the process will be terminated non graceful and immediately.

          You are right. I assumed that nobody wants to loose his unsaved documents, but there may be cases.

          1 Reply Last reply Reply Quote 1
          • A
            andrecool-68 @Meta Chuh
            last edited by Jun 4, 2019, 8:07 AM

            @Meta-Chuh
            And how is such a combination of two code variants … does it violate the laws of criminal law?

            <Command name="Restart" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /c taskkill /f /im notepad++.exe && start notepad++.exe</Command>
            
            D 1 Reply Last reply Jun 4, 2019, 8:22 AM Reply Quote 1
            • D
              dinkumoil @andrecool-68
              last edited by dinkumoil Jun 4, 2019, 8:30 AM Jun 4, 2019, 8:22 AM

              @andrecool-68

              You have to escape the & characters to be compliant with the XML syntax:

              <Command name="Restart" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /c taskkill /f /im notepad++.exe &amp;&amp; start notepad++.exe</Command>
              

              But why do you want to do that? It is not necessary to explicitly launch cmd.exe since taskkill is a console command, hence cmd.exe is started automatically. Or, to be exact, taskkill is started and attached to a new console.

              EDIT: Forget what I wrote above, you want to start Npp after terminating the running instance, thus you need to call cmd.exe.

              A 1 Reply Last reply Jun 4, 2019, 8:27 AM Reply Quote 3
              • A
                andrecool-68 @dinkumoil
                last edited by Jun 4, 2019, 8:27 AM

                @dinkumoil I just want to get the most minimal code.

                D 1 Reply Last reply Jun 4, 2019, 8:42 AM Reply Quote 1
                • D
                  dinkumoil @andrecool-68
                  last edited by dinkumoil Jun 4, 2019, 8:43 AM Jun 4, 2019, 8:42 AM

                  @andrecool-68

                  Ah, now I understand your point, you erased the FULL_PATH_TO_YOUR\ part of @Meta-Chuh 's code.

                  Well, I would say you are lucky that it works without the full path to Npp. I guess currently Npp sets its own path as the current path when it executes the command. But I would not rely on that, maybe this behaviour changes in the future.

                  Over the years I got used to ALWAYS use fully qualified and quoted paths. This makes your scripts error proof and thus your life much more easy.

                  A 1 Reply Last reply Jun 4, 2019, 8:44 AM Reply Quote 2
                  • A
                    andrecool-68 @dinkumoil
                    last edited by Jun 4, 2019, 8:44 AM

                    @dinkumoil I always use the portable version of Notepad ++, and try not to write full paths.

                    D 1 Reply Last reply Jun 4, 2019, 8:46 AM Reply Quote 0
                    • D
                      dinkumoil @andrecool-68
                      last edited by Jun 4, 2019, 8:46 AM

                      @andrecool-68

                      You can use the Npp environment variable $(NPP_DIRECTORY).

                      A 1 Reply Last reply Jun 4, 2019, 8:48 AM Reply Quote 3
                      • A
                        andrecool-68 @dinkumoil
                        last edited by Jun 4, 2019, 8:48 AM

                        @dinkumoil said:

                        @andrecool-68

                        You can use the Npp environment variable $(NPP_DIRECTORY).

                        This is the most suitable way.

                        1 Reply Last reply Reply Quote 1
                        • R
                          rinku singh
                          last edited by Jun 4, 2019, 8:54 AM

                          try

                          @echo off

                          taskkill /f /im notepad++.exe
                          “C:\Program Files\Notepad++\notepad++.exe”

                          cls

                          A 1 Reply Last reply Jun 4, 2019, 9:01 AM Reply Quote 0
                          • A
                            andrecool-68 @rinku singh
                            last edited by Jun 4, 2019, 9:01 AM

                            @gurikbal-singh said:

                            try

                            @echo off

                            taskkill /f /im notepad++.exe
                            “C:\Program Files\Notepad++\notepad++.exe”

                            cls

                            I tried to get rid of it … and you offer me to return to this?

                            M 2 Replies Last reply Jun 4, 2019, 9:18 AM Reply Quote 1
                            • M
                              Meta Chuh moderator @andrecool-68
                              last edited by Jun 4, 2019, 9:18 AM

                              @andrecool-68

                              I tried to get rid of it … and you offer me to return to this?

                              the translator of @gurikbal-singh is on holiday 😁

                              1 Reply Last reply Reply Quote 1
                              • M
                                Meta Chuh moderator @andrecool-68
                                last edited by Meta Chuh Jun 4, 2019, 9:55 AM Jun 4, 2019, 9:55 AM

                                @andrecool-68

                                by the way:

                                And how is such a combination of two code variants … does it violate the laws of criminal law?
                                <Command name="Restart" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /c taskkill /f /im notepad++.exe && start notepad++.exe</Command>

                                lol, no, everything is legal 😉

                                your version is correct, mine was wrong, as it keeps the cmd window open.
                                i just have forgotten the start command while typing, sorry 🙏

                                1 Reply Last reply Reply Quote 2
                                • E
                                  Ekopalypse
                                  last edited by Jun 4, 2019, 12:13 PM

                                  Just for completeness, there is also the $(NPP_FULL_FILE_PATH) variable.

                                  A 1 Reply Last reply Jun 4, 2019, 12:20 PM Reply Quote 4
                                  • A
                                    andrecool-68 @Ekopalypse
                                    last edited by Jun 4, 2019, 12:20 PM

                                    @Ekopalypse said:

                                    Just for completeness, there is also the $(NPP_FULL_FILE_PATH) variable.

                                    Please show me an example of your solution code.

                                    E 1 Reply Last reply Jun 4, 2019, 12:57 PM Reply Quote 1
                                    • E
                                      Ekopalypse @andrecool-68
                                      last edited by Jun 4, 2019, 12:57 PM

                                      @andrecool-68

                                      I don’t have one, it was in response to your response about using $(NPP_DIRECTORY),
                                      which would mean you need to concatenate directory and executable wheres $(NPP_FULL_FILE_PATH) is already the complete path.
                                      Did I misunderstood something?

                                      A 1 Reply Last reply Jun 4, 2019, 1:10 PM Reply Quote 2
                                      • A
                                        andrecool-68 @Ekopalypse
                                        last edited by Jun 4, 2019, 1:10 PM

                                        @Ekopalypse I understood))

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