Community
    • Login

    SaveAs event - get new file Name

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    3 Posts 3 Posters 213 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.
    • Florian JochheimF
      Florian Jochheim
      last edited by

      Hi all,

      I am currently working on a plugin that triggers certain actions when files are opened/saved with a specific fileEnding. For this, I have made a switch in beNotified checking for NPPN_FILEOPENED or NPPN_FILEBEFORESAVE and then I use NPPM_GETFULLPATHFROMBUFFERID to get the full path of the file and check if the path/extension match certain criteria that trigger my action.

      I have noticed though, that when I use saveAs in notepad++, then NPPM_GETFULLPATHFROMBUFFERID in the NPPN_FILEBEFORESAVE will return the old filename, but not the one that was chosen in the saveAs menu. Is there anyway I can get ahold of that?

      As an example, I want to trigger my action wehn saving a file with the extension .special. When I open a file log.txt and select saveAs and enter log.special, then I still get the Filename log.txt from NPPN_FILEBEFORESAVE, hence my action will not trigger

      CoisesC 1 Reply Last reply Reply Quote 0
      • CoisesC
        Coises @Florian Jochheim
        last edited by

        @Florian-Jochheim said in SaveAs event - get new file Name:

        I have noticed though, that when I use saveAs in notepad++, then NPPM_GETFULLPATHFROMBUFFERID in the NPPN_FILEBEFORESAVE will return the old filename, but not the one that was chosen in the saveAs menu. Is there anyway I can get ahold of that?

        Looking at the code, it appears that Notepad++ updates the file name in the buffer after a successful save; and doing my best to follow the processing for IDM_FILE_SAVEAS, I don’t see any messages sent that would help. You could use NPPN_FILESAVED to be notified after the fact and then check if the name was changed from what you captured in NPPN_FILEBEFORESAVE.

        If what you need to do is to intercept processing before the save, probably the best you can do is create your own Save As… command, display and process the dialog to get the filename yourself, and then use NPPM_SAVECURRENTFILEAS to perform the requested action. You could, if you were brave, use SetWindowSubclass to subclass the main Notepad++ window, intercept IDM_FILE_SAVEAS, and call your own routine instead.

        1 Reply Last reply Reply Quote 4
        • Mark OlsonM
          Mark Olson
          last edited by Mark Olson

          As an example, I want to trigger my action wehn saving a file with the extension .special. When I open a file log.txt and select saveAs and enter log.special, then I still get the Filename log.txt from NPPN_FILEBEFORESAVE, hence my action will not trigger

          As @Coises said, NPPN_FILESAVED should be used to listen for when a file is finished saving. As the name implies, NPPN_FILEBEFORESAVE fires before the file is saved, so you should not be too surprised that you get the old name of the buffer when you respond to this notification.

          To respond to a file being renamed, use NPPN_FILEBEFORERENAME, NPPN_FILERENAMED and NPPN_FILERENAMECANCEL.

          You need a global variable, the name of a file that is being renamed. Let’s call it fileToRename.

          1. When you receive an scNotification with code NPPN_FILEBEFORERENAME, set fileToRename, to the name of the buffer to be renamed (use NPPM_GETFULLPATHFROMBUFFERID with WPARAM = scNotification->Header.idFrom)
          2. When you receive an scNotification with code NPPN_FILERENAMECANCEL, set fileToRename to NULL, because the file rename operation was cancelled.
          3. When you receive an scNotification with code NPPN_FILERENAMED, you can set fileToRename to the name of the file after being renamed (use NPPM_GETFULLPATHFROMBUFFERID with WPARAM = scNotification->Header.idFrom)

          If you don’t care about what the name of the file was before after it was renamed, you can also only listen to NPPN_FILERENAMED.

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