Community
    • Login

    Python script plugin console to open multiple files

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    19 Posts 4 Posters 1.3k 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.
    • EkopalypseE
      Ekopalypse
      last edited by Ekopalypse

      @June-Wang

      These commands are used to call menu items and just return true if a menu item was found.
      Do you want to find out which encoding has been assigned by notepad++?

      Alan KilbornA June WangJ 2 Replies Last reply Reply Quote 1
      • Alan KilbornA
        Alan Kilborn @Ekopalypse
        last edited by

        @Ekopalypse said in Python script plugin console to open multiple files:

        Do you want to find out which encoding has been assigned by notepad++?

        Is this one going to end HERE?

        1 Reply Last reply Reply Quote 1
        • EkopalypseE
          Ekopalypse
          last edited by

          @Alan-Kilborn

          Eventually, we could find a solution to read the value from the status bar if that is really what is being looked for, but it must be clear that this is just an estimate from Npp and in the worst case it is just wrong.

          1 Reply Last reply Reply Quote 3
          • June WangJ
            June Wang @Ekopalypse
            last edited by

            @Ekopalypse said in Python script plugin console to open multiple files:

            @June-Wang
            Do you want to find out which encoding has been assigned by notepad++?

            Kind of, I don’t know much about encoding methods. Just trying to check if Encoding->Character sets selected in a file, continue; if NTF-8 selected, close; else, convert to NTF-8, save N close. I just read the other thread mentioned above, and put together this code.

            if notepad.runMenuCommand("Encoding", "Character sets") and notepad.getEncoding() =='Npp.BUFFERENCODING.COOKIE':
                  continue
            elseif notepad.runMenuCommand("Encoding", "NTF-8"):
               notepad.close()
            else:
               notepad.runMenuCommand("Encoding", "Convert to UTF-8")
               notepad.save()
               notepad.close()
            

            Thought it should work. However, for a file that has Character sets selected, when I do notepad.getEncoding() from console, it returns Npp.BUFFERENCODING.COOKIE. But when I do a compare notepad.getEncoding() ==‘Npp.BUFFERENCODING.COOKIE’, it returns false.

            EkopalypseE 1 Reply Last reply Reply Quote 0
            • EkopalypseE
              Ekopalypse @June Wang
              last edited by Ekopalypse

              @June-Wang

              I guess this one should do what you are looking for.
              As for ANSI and CharacterSet, they are basically the same thing, they are the 8bit encoding.
              ANSI is what the system is currently configured with and CharacterSet can be used if you want to display a different 8bit encoding.
              For example, if you have a German setup, ANSI is equal to Windows-1252,
              but if you get a Russian text, you may need to select Windows-1251 to see the Cyrillic symbols.

              import ctypes
              
              from ctypes.wintypes import HWND, UINT, LPARAM, WPARAM, LPCWSTR
              
              user32 = ctypes.WinDLL('user32')
              
              LRESULT = LPARAM
              SendMessage = user32.SendMessageW
              SendMessage.argtypes = [HWND, UINT, WPARAM, LPARAM]
              SendMessage.restype  = LRESULT
              
              FindWindow = user32.FindWindowW
              FindWindow.argtypes = [LPCWSTR, LPCWSTR]
              FindWindow.restype  = HWND
              
              FindWindowEx = user32.FindWindowExW
              FindWindowEx.restype = HWND
              FindWindowEx.argtypes = [HWND, HWND, LPCWSTR, LPCWSTR]
              
              WM_USER             = 1024
              SB_GETTEXTW         = WM_USER+13
              SB_GETTEXTLENGTHW   = WM_USER+12
              
              
              def get_assumed_encoding(statusbar_hwnd):
                  buffer_length = user32.SendMessageW(statusbar_hwnd, SB_GETTEXTLENGTHW, 4, 0)
                  buffer = ctypes.create_unicode_buffer(buffer_length+1)
                  user32.SendMessageW(statusbar_hwnd, SB_GETTEXTW, 4, ctypes.addressof(buffer))
                  return buffer.value
              
              def main():
                  npp_hwnd = FindWindow(u'Notepad++', None)
                  statusbar_hwnd = FindWindowEx(npp_hwnd, None, u"msctls_statusbar32", None)
              
                  # for each file in ... do
                  encoding = get_assumed_encoding(statusbar_hwnd)
                  if encoding == 'UTF-8':
                      notepad.close()
                  elif encoding in ['UTF-8-BOM', 'UCS-2 BE BOM', 'UCS-2 LE BOM']:
                      # what to do here??
                      pass
                  else: # ANSI and Character set
                      notepad.runMenuCommand("Encoding", "Convert to UTF-8")
                      notepad.save()
                      notepad.close()
                      
              main()
              

              UPDATE: A word of warning, Npp can only ACCEPT the coding, it has no chance to be 100 sure this is the correct one.

              June WangJ 1 Reply Last reply Reply Quote 2
              • EkopalypseE
                Ekopalypse
                last edited by Ekopalypse

                this
                UPDATE: A word of warning, Npp can only ACCEPT the coding, it has no chance to be 100 sure this is the correct one.
                should be
                UPDATE: A word of warning, Npp can only ASSUME the encoding, it has no chance to be 100 sure this is the correct one.

                1 Reply Last reply Reply Quote 2
                • June WangJ
                  June Wang @Ekopalypse
                  last edited by June Wang

                  @Ekopalypse said in Python script plugin console to open multiple files:

                  As for ANSI and CharacterSet, they are basically the same thing, they are the 8bit encoding.

                  else: # ANSI and Character set
                      notepad.runMenuCommand("Encoding", "Convert to UTF-8")
                      notepad.save()
                      notepad.close()
                  

                  Thank you. So there’s no way to tell if a file is Character sets selected or not? But notepad.getEncoding() outputs differently tho.
                  On Character sets selected file, it outputs

                  Npp.BUFFERENCODING.COOKIE
                  

                  On ANSI selected file, it outputs

                  Npp.BUFFERENCODING.ENC8BIT
                  
                  EkopalypseE 1 Reply Last reply Reply Quote 0
                  • EkopalypseE
                    Ekopalypse @June Wang
                    last edited by Ekopalypse

                    @June-Wang

                    If you use the code I posted, you can distinguish between
                    ANSI and CharacterSet by checking for ANSI, with another elif branch

                    elif encoding == 'ANSI'
                    ...
                    

                    But why would you want to do that??

                    1 Reply Last reply Reply Quote 1
                    • June WangJ
                      June Wang
                      last edited by

                      @Ekopalypse
                      Nice, that worked!
                      The software I used to output my current data is unable to output unicode data when it comes to foreign language, sometimes it’s ANSI in Npp++, sometimes it’s Character sets selected, but, i.e. Хромм_слабe turns out to be 孚把抉技技_扼抖忘忌e . Now I need to import those data to another software, but it only accepts UTF-8 encoded. So I have to convert ANSI to UTF-8, and manually fix the ones that have Character sets selected, otherwise they show up as boxes. Not fun :(

                      PeterJonesP 1 Reply Last reply Reply Quote 1
                      • PeterJonesP
                        PeterJones @June Wang
                        last edited by

                        @June-Wang said in Python script plugin console to open multiple files:

                        The software I used to output my current data is unable to output unicode data

                        Unicode was invented in 1991 ↗, and UTF-8 encoding was invented in 1992 ↗. Software developers have had 30 years to adapt. And most software I’ve seen from the last decade or so knows how to use UTF-8. Any software still under active development should have figured out UTF-8 by now; if they haven’t, and if you (or a company you work for) are a paying customer, I would start making frequent requests to find out what their schedule for upgrading from pre-1990’s technology is.

                        That said, I’m glad that Notepad++ (plus plugins) is able to help you overcome this extreme deficiency in output ability of this other software. Good luck.

                        June WangJ 1 Reply Last reply Reply Quote 1
                        • June WangJ
                          June Wang @PeterJones
                          last edited by

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