Python script plugin console to open multiple files
-
@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?
-
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.
-
@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.
-
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.
-
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 onlyASSUME
the encoding, it has no chance to be 100 sure this is the correct one. -
@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 outputsNpp.BUFFERENCODING.COOKIE
On ANSI selected file, it outputs
Npp.BUFFERENCODING.ENC8BIT
-
If you use the code I posted, you can distinguish between
ANSI and CharacterSet by checking for ANSI, with another elif branchelif encoding == 'ANSI' ...
But why would you want to do that??
-
@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 :( -
@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.
-
This post is deleted!