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.
    • 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
                  • patrickdrdP
                    patrickdrd
                    last edited by

                    ok I’ve found it,
                    https://stackoverflow.com/questions/17946282/whats-the-hash-in-hkcu-software-microsoft-windows-currentversion-explorer-filee

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

                      :-( - although I can see why securing it might be a good choice. Too bad - at least I learnt something new. :-)

                      1 Reply Last reply Reply Quote 0
                      • PeterJonesP
                        PeterJones
                        last edited by

                        With Windows 10, at least, the good old fashioned HKCR\.ext (Default)=SomeAction and HKCR\SomeAction\command (default)=notepad++ "%1" still works for me, and I thought if it worked in Win7 and works in Win10, it would work in Win8. But I could be wrong.

                        An example of me recently verifying that the old-style assocation works can be found on superuser. maybe it’s because the .0-.9 extensions were ones that weren’t already being interfered with using the Classes/FilesExts assocation.

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

                          But, afaik, you shouldn’t use HKEY_CLASSES_ROOT as this, itself, isn’t a persistent key.
                          It’s just another view of keys stored in HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER.
                          I’m unsure what kind of issue would possibly rise if one corrupts HKCR.

                          1 Reply Last reply Reply Quote 1
                          • PeterJonesP
                            PeterJones
                            last edited by PeterJones

                            It’s worked with HKCR on every windows (I believe since NT; definitely XP, 7, and 10) for me, and I’ve never had problems with it on any machine at work or at home, where I’ve used this method for years. When I write the one in HKCR, it’s always correctly been copied to the appropriate HKLM/HKCU locations without difficulty.

                            (edit: that doesn’t mean it’s the “right” way to do it; I’ve just never had a problem with it.)

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

                              Seems one of those things WE do which wasn’t initially planned by MS :-)
                              But if you replace it under HKCR, is it set in HKLM or HKCU?
                              If it is HKLM, then isn’t it a security issue??

                              1 Reply Last reply Reply Quote 2
                              • PeterJonesP
                                PeterJones
                                last edited by

                                You are right: if I write to HKCR, it duplicates to HKLM, so that is an issue (of course, if you don’t have permission to write to HKLM, it won’t do that write).

                                I just tried an experiment where I created HKCU\Software\Classes\.eko2 (.eko was my write-to-HKCR experiment, so wanted a new extension), and pointed it to my existing txtfile type; when I do that and refresh environment, it associates .eko2 file extension with txtfile, and thus opens with Notepad++ when I double-click… When I look back through the registry, the .eko2 key exists in HKCU\Software\Classes where I put it, and also exists as HKCR\.eko2, but does not exist as HKLM\Software\Classes\.eko2, so I believe that writing to HKCU\Software\Classes\ is safer.

                                As another experiment, I created HKCU\Software\Classes\EkoThreeType with (default) = Eko palypse type 3, subkey shell, subkey Notepad++ (default)=notepad++... then HKCU\Software\Classes\.eko3 (default)=EkoThreeType. It also properly associated.

                                [HKEY_CURRENT_USER\Software\Classes\EkoThreeType]
                                @="Eko palypse type 3"
                                
                                [HKEY_CURRENT_USER\Software\Classes\EkoThreeType\shell]
                                @="Notepad++"
                                
                                [HKEY_CURRENT_USER\Software\Classes\EkoThreeType\shell\Notepad++]
                                @="Eko3 Edit With Notepad++"
                                
                                [HKEY_CURRENT_USER\Software\Classes\EkoThreeType\shell\Notepad++\command]
                                @="\"c:\\usr\\local\\scripts\\notepad++.bat\" \"%1\""
                                
                                [HKEY_CURRENT_USER\Software\Classes\.eko3]
                                @="EkoThreeType"
                                

                                So, even using HKCU\Software\Classes, I can associate a file extension and new filetype for myself.

                                -----
                                PS: for refresh, i use nircmd shellrefresh and nircmd sysrefresh, using the NirSoft nircmd utility. This avoids having to reboot or log in/out for every change.

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

                                  well that example with .eko3 worked for me too (in 8.1),
                                  but when I try the same with .txt, e.g.:

                                  [HKEY_CURRENT_USER\Software\Classes\.txt]
                                  @="txt"
                                  [HKEY_CURRENT_USER\SOFTWARE\Classes\txt\DefaultIcon]
                                  @="\"D:\\Utilities\\PortableApps\\Notepad++\\notepad++.exe\",0"
                                  [HKEY_CURRENT_USER\Software\Classes\txt]
                                  @="Text Document"
                                  [HKEY_CURRENT_USER\Software\Classes\txt\shell]
                                  @="Notepad++"
                                  [HKEY_CURRENT_USER\Software\Classes\txt\shell\Notepad++]
                                  @="Eko3 Edit With Notepad++"
                                  [HKEY_CURRENT_USER\Software\Classes\txt\shell\Notepad++\command]
                                  @="\"D:\\Utilities\\PortableApps\\Notepad++\\notepad++.exe\" \"%1\""
                                  

                                  it doesn’t work

                                  can you try the same @PeterJones?

                                  1 Reply Last reply Reply Quote 0
                                  • PeterJonesP
                                    PeterJones
                                    last edited by

                                    So, based on my experiments, the following worked for me:

                                    • If there already is a HKLM\Software\Classes\.txt, you have to delete it using Admin privileges
                                    • If there already is a HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt, delete it (shouldn’t require Admin, because it’s Current User)
                                      • I also removed any *.txt from HKCU\Software\Microsoft\Windows\CurrentVersion\ApplicationAssociationToasts\ – I don’t know if that’s required (I don’t think so), but I had already done it, so…
                                    • Then run your .reg file shown, which will set up the HKCU\Software\Classes\.txt
                                    • Then do a refresh (reboot, or logout/login, or nircmd shellrefresh && nircmd sysrefresh)
                                    • When I did all that, my old association with .txt was gone, and it shows up using the one from your .reg sequence (I actually used “DRD Text Document” as the name and “DRD Edit With Notepad++” to disambiguate from my other naming scheme)

                                    If I then re-instate the HKLM\Software\Classes\.txt assocation (Admin regedit) and remove the HKCU\Software\Classes\.txt assoc (normal user regedit) and refresh, it goes back to using my old assocation.

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

                                      no, I didn’t mean to try my method, I know that the reg works but it contains this hash,
                                      if you found a working way of setting the .txt association without the hash, please post

                                      1 Reply Last reply Reply Quote 1
                                      • PeterJonesP
                                        PeterJones
                                        last edited by PeterJones

                                        @patrickdrd ,

                                        The reg file I was talking about was

                                        [HKEY_CURRENT_USER\Software\Classes\.txt]
                                        @="txt"
                                        [HKEY_CURRENT_USER\SOFTWARE\Classes\txt\DefaultIcon]
                                        @="\"D:\\Utilities\\PortableApps\\Notepad++\\notepad++.exe\",0"
                                        [HKEY_CURRENT_USER\Software\Classes\txt]
                                        @="Text Document"
                                        [HKEY_CURRENT_USER\Software\Classes\txt\shell]
                                        @="Notepad++"
                                        [HKEY_CURRENT_USER\Software\Classes\txt\shell\Notepad++]
                                        @="Eko3 Edit With Notepad++"
                                        [HKEY_CURRENT_USER\Software\Classes\txt\shell\Notepad++\command]
                                        @="\"D:\\Utilities\\PortableApps\\Notepad++\\notepad++.exe\" \"%1\""
                                        

                                        … which does not contain a hash. Using that file, and the steps I described above, the association worked just fine.

                                        (edit: well, okay, almost that file. I had to add the registry header line, and I converted the path to notepad++.exe to work for my setup.)

                                        1 Reply Last reply Reply Quote 2
                                        • PeterJonesP
                                          PeterJones
                                          last edited by

                                          Further details:

                                          The hash you are mentioning, from your original post:

                                          [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice]
                                          "ProgId"="txt_auto_file"
                                          "Hash"="q4kw7ShKh2M="
                                          

                                          Is part of the HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt that I told you to delete – it will delete the .txt and everything under it, including your hash.

                                          While quoting that piece from your original post, I see what may have been going on from the start: You are telling windows that the current user has chosen the program ID (ProgID) of “txt_auto_file”. But then you are defining the details of the file type, not under txt_auto_file, but under txt – so you were telling Windows that the user had chosen a different association for .txt than you were defining, so windows was still using your old “txt_auto_file” rather than the new “txt”.

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

                                            well I’ve just tried this:
                                            [-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]
                                            [-HKEY_CURRENT_USER\Software\Classes.txt]
                                            [-HKEY_CURRENT_USER\Software\Classes\txt]

                                            [HKEY_CURRENT_USER\Software\Classes\.txt]
                                            @="txt"
                                            [HKEY_CURRENT_USER\SOFTWARE\Classes\txt\DefaultIcon]
                                            @="\"D:\\Utilities\\PortableApps\\Notepad++\\notepad++.exe\",0"
                                            [HKEY_CURRENT_USER\Software\Classes\txt]
                                            @="Text Document"
                                            [HKEY_CURRENT_USER\Software\Classes\txt\shell]
                                            @="Notepad++"
                                            [HKEY_CURRENT_USER\Software\Classes\txt\shell\Notepad++]
                                            @="Edit With Notepad++"
                                            [HKEY_CURRENT_USER\Software\Classes\txt\shell\Notepad++\command]
                                            @="\"D:\\Utilities\\PortableApps\\Notepad++\\notepad++.exe\" \"%1\""
                                            

                                            as you can see I’m deleting everything before the operation, so there is no txt_auto_file or anything, however that doesnt work still

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