Menu

runMenuCommand in other languages than englisch

Help
mirko
2016-01-18
2016-02-19
  • mirko

    mirko - 2016-01-18

    Hello,
    i programmed a script to check xml files.
    As long as the language is set to English everything works fine, after switching to german nothing works. Also, the change in German menu items takes no effect. E.G. notepad.runMenuCommand("Zeilenoperationen", "Leerzeilen (auch mit Whitespace) löschen") instead of notepad.runMenuCommand("Line Operations", "Remove Empty Lines (Containing Blank characters)")

    notepad.runPluginCommand("XML Tools", "Pretty Print (XML only)") works in german, i think because there is no german translation.

    import os;
    import sys;
    filePathSrc="c:\Test"
    for root, dirs, files in os.walk(filePathSrc):
    for fn in files:
    if fn[-4:] == '.xml':
    notepad.open(root + "\" + fn)
    notepad.runMenuCommand("Line Operations", "Remove Empty Lines (Containing Blank characters)")
    notepad.runMenuCommand("Blank Operations", "Trim Trailing Space")
    notepad.runPluginCommand("XML Tools", "Pretty Print (XML only)")
    notepad.save()
    notepad.close()

    Does somebody know a special work around to solve this problem?

     
  • CFrank

    CFrank - 2016-01-20

    May I ask you why you want to go this way?
    Why not using a pure python script, without using npp at all, instead?
    I guess this would be much faster and you have more control over it.

    Cheers
    Claudia

     
  • mirko

    mirko - 2016-02-19

    Hy Claudia,
    thank you for your answer. in my case, this was the fastest way. I'am not so good in python.
    So i have to find some hours in the next time to improve my phyton knowledge.

    Thank you

     
  • CFrank

    CFrank - 2016-02-19

    Hi Mirko,

    the easieast way to see if a menucommand or plugin command works is to use the console.
    Plugins->Python Script->Show Console

    Then put the command into it (copy it from nativlang.xml to get the proper spelling
    but you need to remove the accelerator chars) and run it.

    When it run successful it returns true, if it fails it returns false.

    >>> notepad.runMenuCommand("Zeilenoperationen", "Leerzeilen (auch mit Whitespace) löschen")
    True
    >>> notepad.runMenuCommand("Nicht druckbare Zeichen", "Leerzeichen und Tabulatoren am Zeilenende löschen")
    True
    >>> notepad.runPluginCommand("XML Tools", "Pretty Print (XML only)")
    True
    >>> notepad.runPluginCommand("XML Tools", "Pretty Print (XML only2)")
    False
    

    In regards to a pure python solution I would do somethig like

    from xml.dom.minidom import parse
    from os import walk
    from StringIO import StringIO
    
    filePathSrc = r'C:\Test'
    for root, dirs, files in walk(filePathSrc):
        for fn in files:
            # if fn[-4:] == '.xml':
            if fn == 'test1.xml':
                with open(root + '\\' + fn, 'r+') as f:
    
                    xml = parse(f)                                  
    
                    version = ' version="{}"'.format(xml.version) if xml.version != None else ''
                    encoding = ' encoding="{}"'.format(xml.encoding) if xml.encoding != None else ''
                    standalone = ' standalone="{}"'.format(xml.standalone) if xml.standalone != None else ''
    
                    pretty_xml = xml.toprettyxml("    ")
                    lines = StringIO(pretty_xml)
                    lines = [ x for x in lines if x.strip() != ""]
    
                    if version != '':
                        lines[0] = '<?xml{}{}{} ?>'.format(version, encoding, standalone)
                    else:
                        lines.pop(0)
    
                    prettier_xml = "".join(lines)
                    f.seek(0)
                    f.truncate(0)
                    f.write(prettier_xml)
    

    To test it you need to have a test1.xml, once you're fine with it comment/delete the line

            if fn == 'test1.xml':
    

    and uncomment

            # if fn[-4:] == '.xml':
    

    Cheers
    Claudia

     

    Last edit: CFrank 2016-02-19

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.