Community
    • Login

    Run command on file open

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    6 Posts 3 Posters 2.6k 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.
    • quzarQ
      quzar
      last edited by

      I tried searching for this but my search-foo is weak. Every time I open an XML file with NPP, I hit ctrl+alt+shift+b to ‘pretty print’ reformat it via the XML tools plugin. Is there any way to designate internal/plugin commands to be run automatically when a file is opened?

      Perhaps this specific instance could also be achieved via a user style extension.

      Thank you!

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

        @quzar

        You can make this work if you install the python script plugin.
        Once installed, goto Plugins->Python Script->New Script
        and name the new script startup.py
        It is important to name it like that because the python script plugin
        checks if this file exists and in case it does, it executes it.
        The content of the script should be like this

        from Npp import notepad, NOTIFICATION, LANGTYPE
        
        def BufferActivated(args):
            if notepad.getLangType() == LANGTYPE.XML:
                notepad.runPluginCommand('XML Tools','Pretty print (XML only - with line breaks)')
        
        notepad.callback(BufferActivated, [NOTIFICATION.BUFFERACTIVATED])
        

        This means, whenever a document gets activated, the BufferActivated function gets called
        and checks if the current document has the language type xml set and if so, tries to run
        the XML plugin function pretty print …

        One last thing, goto Plugins->Python Script->Configuration… and change the
        Initialisation from LAZY to ATSTARTUP

        Restart npp - done

        Eko

        Alan KilbornA 1 Reply Last reply Reply Quote 4
        • Alan KilbornA
          Alan Kilborn @Eko palypse
          last edited by Alan Kilborn

          @Eko-palypse

          I’m far from expert on this, but isn’t your solution going to run every time a tab is changed by the user instead of just on starting up? Could this run into a lot of time for a big XML file? A big time wouldn’t be really noticable on npp startup, but everytime switching a tab it might be.

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

            @Alan-Kilborn

            You are correct this runs when the buffer has been activated, always.
            Means it might run even it isn’t necessary to run as it has been formatted already.

            There is one issue here, as far as I see/understood from pythonscript examples/documents/forum
            and this is that you cannot modify a buffer as long
            as it isn’t part of one of the two existing editor objects and the XML plugin can
            modify the current buffer only anyways.

            There are workarounds here but without knowing what @quzar exactly wants to do
            I can’t say for sure which one would fit best(?). For example we could add an property
            to each document or using an internal list containing the bufferids of the
            documents which have been already formatted or …

            Eko

            1 Reply Last reply Reply Quote 0
            • quzarQ
              quzar
              last edited by

              @Eko-palypse

              Thank you, I had seen some other forum responses saying to use a python script for on-open execution of instructions, but they did not provide instructions.

              The re-applying at every modification/tab switch might be a bit onerous. Perhaps a temporary variable to have this only happen once per buffer? After the initial pretty-fication, it shouldn’t have to be done again.

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

                @quzar

                so something like this would do it once as long as the already_formatted property
                does not get changed to empty string.

                from Npp import notepad, editor, NOTIFICATION, LANGTYPE
                
                def BufferActivated(args):
                    if notepad.getLangType() == LANGTYPE.XML and editor.getProperty('already_formatted') == '':
                        notepad.runPluginCommand('XML Tools','Pretty print (XML only - with line breaks)')
                        editor.setProperty('already_formatted', '1')
                
                notepad.callback(BufferActivated, [NOTIFICATION.BUFFERACTIVATED])
                

                Eko

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