Community
    • Login

    file associations (file types to open with npp)

    Scheduled Pinned Locked Moved General Discussion
    41 Posts 4 Posters 9.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.
    • patrickdrdP
      patrickdrd
      last edited by

      actually I didn’t,
      this one:
      https://textuploader.com/1awt2/raw
      seems to work fine in windows 7,
      while this one:
      https://textuploader.com/1aw6a/raw
      seems to work in windows 8.1

      I wish they could be merged, can they?

      Eko palypseE 1 Reply Last reply Reply Quote 1
      • Eko palypseE
        Eko palypse @patrickdrd
        last edited by Eko palypse

        @patrickdrd

        what about using a script like this?
        For setting it per machine it must run under administrative privileges .
        The FILE_EXT list would be the place to maintain your file extensions.

        import ctypes
        import ctypes.wintypes as wintypes
        from Npp import notepad, MESSAGEBOXFLAGS
        
        shell32 = ctypes.WinDLL('shell32')
        advapi32 = ctypes.WinDLL('advapi32')
        
        SHCNE_ASSOCCHANGED = 0x08000000
        SHCNF_IDLIST = 0
        
        HKEY_CURRENT_USER = -2147483647
        HKEY_LOCAL_MACHINE = -2147483646
        SUB_KEY = u'Software\Classes\{}'
        KEY_ALL_ACCESS = 0xf003f
        REG_SZ = 1
        ERROR_SUCCESS = 0x0
        
        FILE_EXT = ['.txt', '.xml']
        NPPNAME = ctypes.create_unicode_buffer(u'Notepad++_file')
        
        def main():
            result = notepad.messageBox(('YES for USER only\n\n'
                                         'NO for MACHINE\n\n'
                                         'CANCEL to abort'), 'FileExtension Registration', 3)
        
            if result == MESSAGEBOXFLAGS.RESULTYES:
                _HKEY = HKEY_CURRENT_USER
            elif result == MESSAGEBOXFLAGS.RESULTNO:
                _HKEY = HKEY_LOCAL_MACHINE
            else:
                return
        
            for extension in FILE_EXT:
                _sub_key = SUB_KEY.format(extension)
                try:
                    phkResult = wintypes.HKEY()
                    result = advapi32.RegOpenKeyExW(_HKEY,
                                                    _sub_key,
                                                    0,
                                                    KEY_ALL_ACCESS,
                                                    ctypes.byref(phkResult))
                    if result == ERROR_SUCCESS:
                        result = advapi32.RegSetValueExW(phkResult,
                                                         None,
                                                         0,
                                                         REG_SZ,
                                                         NPPNAME,
                                                         len(NPPNAME) * ctypes.sizeof(wintypes.c_wchar))
                        if result == ERROR_SUCCESS:
                            print(u'key {} successfully set'.format(_sub_key))
                        else:
                            print(u'Aborting as error {} occured'.format(result))
                            break
        
                    else:
                        print('ERROR {} opening key {}'.format(result, _sub_key))
                        break
                except Exception as e:
                    print('ERROR:{}'.format(e))
                finally:
                    advapi32.RegCloseKey(phkResult)
        
            result = notepad.messageBox('Should the system be informed about the changes?', 'Inform System', 4)
            if result == MESSAGEBOXFLAGS.RESULTYES:
                shell32.SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, None, None)
        
        main()
        
        1 Reply Last reply Reply Quote 3
        • patrickdrdP
          patrickdrd
          last edited by patrickdrd

          looks good, but how do I run it?
          this should be a PowerShell script, right?
          can you post a batch file to call it with administrative privileges please?

          also, where is npp in this script?

          1 Reply Last reply Reply Quote 0
          • Eko palypseE
            Eko palypse
            last edited by

            sorry for confusion, it is a python script.
            As I read some of your posts I got the impression that you are using pythonscript plugin.

            1 Reply Last reply Reply Quote 0
            • patrickdrdP
              patrickdrd
              last edited by

              yes, I am, but I’m not that familiar with it, actually I’ve forgotten, so either you send me instructions or I’ll dig older posts

              1 Reply Last reply Reply Quote 1
              • Eko palypseE
                Eko palypse
                last edited by Eko palypse

                If you have the python script plugin installed, just create a new script and copy/paste the posted script.
                Save it. Restart npp as admin if you want to set the extensions on LOCAL_MACHINE key and execute the script. If it should be set under CURRENT_USER a non-admin start of npp is sufficient.

                1 Reply Last reply Reply Quote 3
                • patrickdrdP
                  patrickdrd
                  last edited by

                  ok, thanks a lot, I’ll try it in the morning in both pc’s and I’ll tell you

                  1 Reply Last reply Reply Quote 1
                  • patrickdrdP
                    patrickdrd
                    last edited by

                    it didn’t work

                    1 Reply Last reply Reply Quote 0
                    • patrickdrdP
                      patrickdrd
                      last edited by

                      I tried both my scripts on a windows 2008r2 server and
                      the windows7 script worked, the 8.1 not,
                      strange that the 8.1 works on my home laptop

                      1 Reply Last reply Reply Quote 1
                      • Eko palypseE
                        Eko palypse
                        last edited by

                        Did you get one of the possible errors or the message that key xyz has been successfully set?
                        Note, you have to open the python script plugin console to see the messages.
                        If the message is successfully set then I assume that the machine does not have a proper npp
                        installation, means, the link Notepad++_file is either not set at all or points to a wrong path etc…

                        1 Reply Last reply Reply Quote 1
                        • patrickdrdP
                          patrickdrd
                          last edited by

                          if you had opened my reg file(s),
                          maybe you would have seen that I’m running npp portable in all the machines,
                          that’s why I asked after you posted it “where is npp in this script”,

                          after this post I think that specifying the npp path is needed,
                          in my case it is “D:\Utilities\PortableApps\Notepad++”

                          1 Reply Last reply Reply Quote 0
                          • Eko palypseE
                            Eko palypse
                            last edited by Eko palypse

                            I’ve opened the reg files but I didn’t understand what you trying to ask.
                            So what would you suppose? Making a workflow like

                            A.)
                            Check if Notepad++_file link is found
                            if found, run the script like it is now
                            if not found, open a dialog and asking if such a key should be created?
                            or
                            B.)
                            something else in mind?

                            1 Reply Last reply Reply Quote 1
                            • patrickdrdP
                              patrickdrd
                              last edited by

                              no, can’t it be set inside the script like in a constant variable?

                              1 Reply Last reply Reply Quote 0
                              • Eko palypseE
                                Eko palypse
                                last edited by

                                Of course ;-) - that was what I meant.
                                Doing everything within the script.

                                1 Reply Last reply Reply Quote 1
                                • patrickdrdP
                                  patrickdrd
                                  last edited by

                                  that’s what I meant too, within the script is fine,
                                  running the script once every time a new machine is setup is fine as well of course

                                  1 Reply Last reply Reply Quote 0
                                  • Eko palypseE
                                    Eko palypse
                                    last edited by Eko palypse

                                    Ok, I will try to come up with a solution later this day.
                                    Lunch break if over.

                                    While thinking about the dialog to ask for confirmation about
                                    creating the Notepad++_file link - does it really makes sense?
                                    I mean, if one runs the script the idea is to have the extensions
                                    registered and this means you need to have the key.
                                    I guess silently setting the key is ok.

                                    Other question to everyone reading this,
                                    Does someone know a way to refresh the icon cache?
                                    Currently, if you run the script and, let’s say you want to register
                                    .xml to be opened with npp, you still see the previous icon until
                                    you logon/logoff.
                                    It would be nice if I could force to reload the icon cache so that the
                                    npp icon is shown. I haven’t found any good side describing what
                                    needs to be done yet.

                                    Have to go back to work :-)

                                    1 Reply Last reply Reply Quote 2
                                    • patrickdrdP
                                      patrickdrd
                                      last edited by

                                      don’t worry, I’m not in a hurry,
                                      this thread will be active for a while,
                                      I’ll be waiting for SalviaSage’s reply too

                                      as for the your question, yes, silent is fine,
                                      if I was writing the script,
                                      I would choose machine silently too

                                      1 Reply Last reply Reply Quote 1
                                      • Eko palypseE
                                        Eko palypse
                                        last edited by

                                        Hopefully this does what you are looking for.
                                        The problem with the icon cache reload is still not solved.

                                        import ctypes
                                        import ctypes.wintypes as wintypes
                                        from Npp import notepad, MESSAGEBOXFLAGS
                                        
                                        
                                        # modify this two variables to your needs
                                        # ------------------------------------------------------------------------------------------------------------------------
                                        
                                        NPP_FULL_PATH = u'D:\\PortableApps\\Npp\\notepad++.exe'
                                        FILE_EXT = ['.txt','.xml']
                                        
                                        # ------------------------------------------------------------------------------------------------------------------------
                                        
                                        
                                        shell32 = ctypes.WinDLL('shell32')
                                        advapi32 = ctypes.WinDLL('advapi32')
                                        
                                        SHCNE_ASSOCCHANGED = 0x08000000
                                        SHCNF_IDLIST = 0
                                        
                                        HKEY_CURRENT_USER = -2147483647
                                        HKEY_LOCAL_MACHINE = -2147483646
                                        KEY_ALL_ACCESS = 0xf003f
                                        REG_SZ = 1
                                        ERROR_SUCCESS = 0x0
                                        
                                        SUB_KEY = u'Software\\Classes\\{}'
                                        NPPNAME = u'Notepad++_file'
                                        DOCTYP = u'Notepad++ Document'
                                        NPP_ICON = u'"{}",0'.format(NPP_FULL_PATH)
                                        NPP_COMMAND = u'"{}" "%1"'.format(NPP_FULL_PATH)
                                        
                                        
                                        def set_reg_value(root_key, value):
                                            try:
                                                result = advapi32.RegSetValueExW(root_key,
                                                                                 None,
                                                                                 0,
                                                                                 REG_SZ,
                                                                                 value,
                                                                                 len(value) * ctypes.sizeof(wintypes.c_wchar))
                                        
                                                if result == ERROR_SUCCESS:
                                                    print('successfully set {}'.format(value))
                                                else:
                                                    notepad.messageBox('Aborting as error {} occured'.format(result), 'ERROR', MESSAGEBOXFLAGS.ICONERROR)
                                            except Exception as e:
                                                raise IOError('{}'.format(e))
                                            finally:
                                                advapi32.RegCloseKey(root_key)
                                        
                                        
                                        def create_reg_entries(root_key, sub_key):
                                            try:
                                                phkResult = wintypes.HKEY()
                                                dwDisp = wintypes.DWORD()
                                                result = advapi32.RegCreateKeyExW(root_key,
                                                                                  sub_key,
                                                                                  0,
                                                                                  None,
                                                                                  0,
                                                                                  KEY_ALL_ACCESS,
                                                                                  None,
                                                                                  ctypes.byref(phkResult),
                                                                                  ctypes.byref(dwDisp)
                                                                                  )
                                                if result == ERROR_SUCCESS:
                                                    if dwDisp.value in [1, 2]:
                                                        print '\nKey {} has been {}'.format(sub_key, 'created' if dwDisp == 1 else 'opened')
                                                    else:
                                                        notepad.messageBox('Unexpected disposition value received:{}'.format(dwDisp), 'WARNING', MESSAGEBOXFLAGS.ICONWARNING)
                                            except Exception as e:
                                                advapi32.RegCloseKey(root_key)
                                                raise IOError('{}'.format(e))
                                        
                                            return phkResult
                                        
                                        
                                        def create_npp_link(root_key):
                                            _key = create_reg_entries(root_key, SUB_KEY.format(NPPNAME))
                                            set_reg_value(_key, DOCTYP)
                                            _key = create_reg_entries(root_key, SUB_KEY.format(u'Notepad++_file\\DefaultIcon'))
                                            set_reg_value(_key, NPP_ICON)
                                            _key = create_reg_entries(root_key, SUB_KEY.format(u'Notepad++_file\\shell\\open\\command'))
                                            set_reg_value(_key, NPP_COMMAND)
                                            return True
                                        
                                        
                                        def register_extensions(root_key):
                                            for extension in FILE_EXT:
                                                _key = create_reg_entries(root_key, SUB_KEY.format(extension))
                                                set_reg_value(_key, NPPNAME)
                                            return True
                                        
                                        
                                        def main():
                                            result = notepad.messageBox(('YES for USER only\n\n'
                                                                         'NO for MACHINE\n\n'
                                                                         'CANCEL to abort'), 'FileExtension Registration', 3)
                                        
                                            if result == MESSAGEBOXFLAGS.RESULTYES:
                                                _HKEY = HKEY_CURRENT_USER
                                            elif result == MESSAGEBOXFLAGS.RESULTNO:
                                                _HKEY = HKEY_LOCAL_MACHINE
                                            else:
                                                return
                                        
                                            if create_npp_link(_HKEY):
                                                if register_extensions(_HKEY):
                                                    result = notepad.messageBox('Should the system be informed about the changes?', 'Inform System', 4)
                                                    if result == MESSAGEBOXFLAGS.RESULTYES:
                                                        shell32.SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, None, None)
                                            else:
                                                notepad.messageBox('Unable to open or create Notepad++_file key', 'ERROR', MESSAGEBOXFLAGS.ICONERROR)
                                        
                                        main()
                                        
                                        1 Reply Last reply Reply Quote 0
                                        • patrickdrdP
                                          patrickdrd
                                          last edited by

                                          no, it doesn’t,
                                          e.g. I deleted already registered txt file with

                                          [-HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.txt]
                                          [-HKEY_LOCAL_MACHINE\SOFTWARE\Classes\txt]
                                          [-HKEY_CURRENT_USER\SOFTWARE\Classes\.txt]
                                          [-HKEY_CURRENT_USER\SOFTWARE\Classes\txt]
                                          [-HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice]
                                          [-HKEY_CURRENT_USER\Software\Microsoft\Windows\Roaming\OpenWith\FileExts\.txt\UserChoice]
                                          

                                          and tried yours and no error was shown but when tried to open .txt file it asked what I wanted to open it with

                                          1 Reply Last reply Reply Quote 0
                                          • patrickdrdP
                                            patrickdrd
                                            last edited by

                                            I think I fixed it to work in windows 8.1 and windows 7:

                                            https://textuploader.com/1awwn/raw
                                            

                                            I found out that some files need the “hash” in registry which is awkward,
                                            because I don’t know if it will work in another pc,
                                            but I don’t have another 8.1,
                                            so if anyone wants to test it

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