• Login
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.
  • J
    June Wang
    last edited by Mar 24, 2021, 12:57 PM

    I have a ‘filenames.txt’ where each line has the full path to a file, and I want to open all the files in the ‘filenames.txt’ from Python Script Plugin console. And here’s what I’m doing:

    f = open("C:/Users/junew/Desktop/filesnames.txt", "r");
    lines = f.readlines(); 
    for i in lines: 
      notepad.open(i)
    

    When I try lines[0], the output is

    'F:/06__Other_Models/Transport/round1/convert2/58542_\xd0\x9f\xd0\xbe\xd0\xb3\xd1\x80\xd1\x83\xd0\xb7\xd1\x87\xd0\xb8\xd0\xba.mtl\n'
    

    the true path is

    F:/06__Other_Models/Transport/round1/convert2/58542_Погрузчик.mtl
    

    I can open it using notepad.open(‘F:/06__Other_Models/Transport/round1/convert2/58542_Погрузчик.mtl’). But from python console using above code, notepad++ keeps saying ‘File F:/06__Other_Models/Transport/round1/convert2/58542_Погрузчик.mtl doesn’t exist’, I think it has something to do with the non UTF-8 encoded file(names).

    Any idea how to get around it?

    E P 2 Replies Last reply Mar 24, 2021, 1:28 PM Reply Quote 1
    • E
      Ekopalypse @June Wang
      last edited by Mar 24, 2021, 1:28 PM

      @June-Wang

      Which version of PS plugin are you using?

      e79a9cf9-27d0-48d2-a1e1-6fa9c9f43622-image.png

      1 Reply Last reply Reply Quote 1
      • P
        PeterJones @June Wang
        last edited by Mar 24, 2021, 1:32 PM

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

        I can open it using notepad.open(‘F:/06__Other_Models/Transport/round1/convert2/58542_Погрузчик.mtl’). But from python console using above code, notepad++ keeps saying ‘File F:/06__Other_Models/Transport/round1/convert2/58542_Погрузчик.mtl doesn’t exist’, I think it has something to do with the non UTF-8 encoded file(names).

        Actually, \xd0\x9f is the two-byte sequence to represent П in UTF-8. However, it may be that there is an encoding/decoding trick that you need to take in Python to convert the raw UTF-8 bytes read from the file to translate to the valid pathname. I’m not an expert on Python+encoding.

        However, I see one more problem: .mtl\n': because you read the line from a newline-delimited file, you still have a newline in each line. You should really remove the trailing newline sequence before passing it to the notepad.open() command.

        P 1 Reply Last reply Mar 24, 2021, 1:33 PM Reply Quote 0
        • P
          PeterJones @PeterJones
          last edited by Mar 24, 2021, 1:33 PM

          Ah, @Ekopalypse showed it working with the name represented with the \x## notation, so that doesn’t appear to be the problem. I then think it’s the newline sequence at the end of your text line, because the real filename doesn’t end in newline.

          P 1 Reply Last reply Mar 24, 2021, 1:38 PM Reply Quote 1
          • P
            PeterJones @PeterJones
            last edited by PeterJones Mar 24, 2021, 1:38 PM Mar 24, 2021, 1:38 PM

            I confirmed:

            >>> n = 'C:/Users/peter.jones/Downloads/TempData/nppCommunity/\xd0\x9f\xd0\xbe\xd0\xb3\xd1\x80\xd1\x83\xd0\xb7\xd1\x87\xd0\xb8\xd0\xba.mtl'
            >>> notepad.open(n)
            

            opened the file just fine, but

            >>> n = 'C:/Users/peter.jones/Downloads/TempData/nppCommunity/\xd0\x9f\xd0\xbe\xd0\xb3\xd1\x80\xd1\x83\xd0\xb7\xd1\x87\xd0\xb8\xd0\xba.mtl\n'
            >>> notepad.open(n)
            

            shows
            ab58e9fc-7261-40b6-9904-fb867a4e7265-image.png

            Notice the newline between .mtl and the end-quote "

            1 Reply Last reply Reply Quote 2
            • A
              Alan Kilborn
              last edited by Mar 24, 2021, 1:39 PM

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

              notepad.open(i)

              So maybe OP should try:

              notepad.open(i.rstrip())

              J 1 Reply Last reply Mar 24, 2021, 2:07 PM Reply Quote 4
              • J
                June Wang @Alan Kilborn
                last edited by Mar 24, 2021, 2:07 PM

                @Alan-Kilborn said in Python script plugin console to open multiple files:

                notepad.open(i.rstrip())

                That worked ! Thank you all so much! @Ekopalypse @PeterJones

                1 Reply Last reply Reply Quote 3
                • J
                  June Wang
                  last edited by Mar 24, 2021, 8:14 PM

                  @PeterJones @Ekopalypse @Alan-Kilborn
                  Btw, anyone knows how to tell if a file selects ANSI or Character sets ? Right now if a file that has Character sets selected, and a file that has ANSI selected will both return true on the following commands.

                  notepad.runMenuCommand("Encoding", 'Character sets')
                  notepad.runMenuCommand("Encoding", 'ANSI')
                  
                  1 Reply Last reply Reply Quote 0
                  • E
                    Ekopalypse
                    last edited by Ekopalypse Mar 24, 2021, 8:29 PM Mar 24, 2021, 8:29 PM

                    @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++?

                    A J 2 Replies Last reply Mar 24, 2021, 8:45 PM Reply Quote 1
                    • A
                      Alan Kilborn @Ekopalypse
                      last edited by Mar 24, 2021, 8:45 PM

                      @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
                      • E
                        Ekopalypse
                        last edited by Mar 24, 2021, 8:50 PM

                        @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
                        • J
                          June Wang @Ekopalypse
                          last edited by Mar 25, 2021, 6:05 AM

                          @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.

                          E 1 Reply Last reply Mar 25, 2021, 11:33 AM Reply Quote 0
                          • E
                            Ekopalypse @June Wang
                            last edited by Ekopalypse Mar 25, 2021, 11:36 AM Mar 25, 2021, 11:33 AM

                            @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.

                            J 1 Reply Last reply Mar 25, 2021, 2:32 PM Reply Quote 2
                            • E
                              Ekopalypse
                              last edited by Ekopalypse Mar 25, 2021, 11:42 AM Mar 25, 2021, 11:42 AM

                              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
                              • J
                                June Wang @Ekopalypse
                                last edited by June Wang Mar 25, 2021, 2:32 PM Mar 25, 2021, 2:32 PM

                                @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
                                
                                E 1 Reply Last reply Mar 25, 2021, 2:35 PM Reply Quote 0
                                • E
                                  Ekopalypse @June Wang
                                  last edited by Ekopalypse Mar 25, 2021, 2:37 PM Mar 25, 2021, 2:35 PM

                                  @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
                                  • J
                                    June Wang
                                    last edited by Mar 25, 2021, 5:35 PM

                                    @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 :(

                                    P 1 Reply Last reply Mar 25, 2021, 5:50 PM Reply Quote 1
                                    • P
                                      PeterJones @June Wang
                                      last edited by Mar 25, 2021, 5:50 PM

                                      @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.

                                      J 1 Reply Last reply Mar 25, 2021, 6:42 PM Reply Quote 1
                                      • J
                                        June Wang @PeterJones
                                        last edited by Mar 25, 2021, 6:42 PM

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