Can User Defined Command Save Current File First?



  • I have a user defined command that launches my browser & displays the current file. As I quickly found out, it doesn’t automatically save the file before launching the browser.

    It would be a huge time saver if I could make NP++ save the file automatically before launching the browser. It would also ensure I don’t forget to save the file before examining the results of the changes I just made.

    Is there a way to do this?



  • The user defined commands just launch external processes, which you can pass arguments to (like the filename). They do not affect the Notepad++ GUI environment (save, etc) at all.

    However, you can hand-craft a macro to do it. (I tried recording the macro, but the View > View Current File In > ___ doesn’t seem to be recordable in v7.8.2, even though it’s playable).

            <Macro name="SaveFileLaunchInChrome" Ctrl="no" Alt="no" Shift="no" Key="0">
                <Action type="2" message="0" wParam="41006" lParam="0" sParam="" />
                <Action type="2" message="0" wParam="44101" lParam="0" sParam="" />
            </Macro>
    

    To implement that macro, follow the instructions for Editing Configuration Files to edit %AppData%\Shortcuts.xml, and paste those four lines inside the <Macros> ... </Macros> section, next to one of the other macros.

    To understand the macro: the "41006" translates to File > Save. The ["44101" translates to View > View Current File In > Chrome](https://github.com/notepad-plus-plus/notepad-plus-plus/blob/master/PowerEditor/src/menuCmdID.h#L354-L357. Firefox would be "44100", MS Edge to "44102", and MS Internet Explorer to "44103" – just replace the "44101" in the second <Action to change which browser. If you want a browser not supported by the View Current File In…, you will have to use the NppExec route, described below.

    If that doesn’t work for your purposes (you have a different browser, or you want a more complicated sequence), use a plugin like NppExec, you could have a script like:

    npp_save
    "c:\program files\path\to\browser.exe" "$(FULL_CURRENT_PATH)"
    

    That script can then be assigned a keyboard shortcut via the NppExec interface.



  • @PeterJones said in Can User Defined Command Save Current File First?:

    doesn’t seem to be recordable in v7.8.2

    submitted issue#7851



  • @PeterJones I’m unfamiliar with the NppExec interface you suggested for assigning a keyboard shortcut. I Googled NppExec, but all the hits I saw were forum questions that assumed the user was already familiar with it.

    Is there a tutorial or something on how NppExec works & how to use it? Is it built into the default NP++ installation?



  • Like I said, I’d recommend trying the macro first, unless you have to use a different browser.

    NppExec can be installed using the Plugins > Plugins Admin interface. It comes with reasonable helpfiles – but to do what I suggested just takes the following flow:

    1. Install NppExec and restart Notepad++
    2. Plugins > NppExec > Execute …
    3. Paste in the script I showed above
    4. Hit Save…
    5. Give it a name, like SaveAndLaunchInBrowser, Save
    6. OK to run immediately, or Cancel if you’re not ready to run the script yet (don’t worry, it’s saved)
    7. Plugins > NppExec > Advanced Options…
      • Item Name = Save and Launch in Browser,
      • Associated Script = SaveAndLaunchInBrowser
      • Add/Modify, which will place an entry in the upper-left Menu Items List
      • Enable Place to the Macros submenu checkbox
    8. Exit and restart Notepad++
    9. Macro menu will now have a Save and Launch in Browser menu item.
    10. To assign a keyboard shortcut,
      • Macro > Modify Shortcut / Delete Macro…,
      • Go into the Plugin Commands tab
      • Filter by Launch
      • click on the row that says Save and Launch in Browser
      • click Modify, choose your keyboard shortcut, and OK
      • Close to exit dialog

    That keyboard shortcut will now run that whole NppExec script.



  • @PeterJones Yay! Got it work without having to mess with NppExec interface. For the benefit of anyone who wants to do this with Firefox & Windows 10, the macro below is what I finally settled on. I used a keyboard shortcut of Ctrl-Shift-Enter (your mileage may vary on other systems, especially the virtual key number).

    Thanks for help and patience, Peter. If you’d like to see the the Web site I’m using it for, it’s at http://www.hymntime.com/tch. I’m editing several hundred files, so this will be a tremendous help!

    <Macro name="Save File and Launch in Firefox" Ctrl="yes" Alt="no" Shift="yes" Key="13">
       <Action type="2" message="0" wParam="41006" lParam="0" sParam="" />
       <Action type="2" message="0" wParam="44100" lParam="0" sParam="" />
    </Macro>
    

Log in to reply