Community
    • Login

    Is there a way/plugin that makes notepad++ run code on a file when we open it?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    44 Posts 3 Posters 35.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.
    • Claudia FrankC
      Claudia Frank @Alguem Meugla
      last edited by

      @Alguem-Meugla

      ok, so the reason why it doesn’t work is that python script plugin
      doesn’t find the startup.py but why…

      Another thing which is strange, whenever I use New Script
      it redirects to the user scripts directory, regardless if I created
      script in another subdirectory, totally different directory or not.
      Maybe this is a coincident because it can’t find the user script directory.

      In addition,

      C:\Users\Alguem\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts

      how could this be, mine is pointing to

      C:\users\linus\Application Data\Notepad++\plugins\Config\PythonScript\scripts

      Has this changed with windows 10??
      May I ask how you installed python script?
      Using plugin manager or the msi package I pointed to?
      Which python script version did you install?
      Did you somehow redirect your %APPDATA% environment variable?

      When you press CTRL+R, the windows run command should open, and type
      %APPDATA% and press enter, which directory gets opened and does it contain
      a subdirectory notepad++??

      Cheers
      Claudia

      1 Reply Last reply Reply Quote 0
      • Alguem MeuglaA
        Alguem Meugla
        last edited by

        @Claudia-Frank said:

        In addition,

        C:\Users\Alguem\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts

        how could this be, mine is pointing to

        C:\users\linus\Application Data\Notepad++\plugins\Config\PythonScript\scripts

        Has this changed with windows 10??

        Isn’t Application Data the same as AppData?
        And %APPDATA% always redirected me to \AppData\Roaming.
        What’s your windows version?

        May I ask how you installed python script?
        Using plugin manager or the msi package I pointed to?

        I used Plugin Manager both times, before and after I replaced the default startup.py, when I had to reinstall it.
        I will try reinstalling it using that package. Plugin manager usually works right and is very simple for me.

        Which python script version did you install?

        It says 1.0.8.

        When you press CTRL+R, the windows run command should open, and type
        %APPDATA% and press enter, which directory gets opened and does it contain
        a subdirectory notepad++??

        CTRL+R reloads the page…
        I can use WINDOWSKEY+R instead, which opens the Run window, or I can use the search menu on Windows 10. Typing %APPDATA% both opens …\AppData\Roaming\.

        Claudia FrankC 1 Reply Last reply Reply Quote 0
        • Claudia FrankC
          Claudia Frank @Alguem Meugla
          last edited by

          @Alguem-Meugla

          What’s your windows version?

          Since a couple of month I use linux exclusively.
          Npp is started via windows emulation software WINE.
          But in the past I was using Windows XP and Windows 7 and I thought
          %APPDATA% redirected to “Application Data” like within wine,
          maybe I have false memory.

          Yes, 1.0.8 is the version I use as well.
          As mentioned earlier, don’t replace the default startup.py as it contains
          needed code for plugin script itself.

          If you can’t get a user startup.py to work, append the posted script
          to the end of the default startup.py - then it has to work - I hope.

          Cheers
          Claudia

          Alguem MeuglaA 2 Replies Last reply Reply Quote 0
          • Alguem MeuglaA
            Alguem Meugla @Claudia Frank
            last edited by

            @Claudia-Frank
            I tried to reinstall Python Script using the package you linked above, and New Script still opens the same folder and the script still doesn’t work at startup.
            One thing to note is that when I go to Python Script configuration it shows startup.py as a User script:

            Adding it to Menu items makes it show at Plugins>Python Script> startup.py.

            1 Reply Last reply Reply Quote 0
            • Alguem MeuglaA
              Alguem Meugla @Claudia Frank
              last edited by

              @Claudia-Frank
              I appended the script (below) to the end of the default startup.py

              import os
              
              # ---------------------------- configuration area -----------------------------
              
              FILE_EXTENSION_LIST = ['.html','.js']
              COLLPASE_LINE_IDENTIFIER = 'NPP_COLLAPSE_IT'
              COLLPASE_FROM_TO_IDENTIFIER = 'NPP_COLLAPSE\((\d+,\d+)\)'
              
              # -----------------------------------------------------------------------------
              
              
              def scan(number_of_lines):
              
                  def find_line_identifiers(): # NPP_COLLAPSE_IT
                      matches = []
                      editor.research('\\b{0}\\b'.format(COLLPASE_LINE_IDENTIFIER), lambda m: matches.append(m.span()), 0)   
                      return matches
              
                  def find_from_to_line_identifiers(): # NPP_COLLAPSE(19,41)
                      matches = []
                      editor.research(COLLPASE_FROM_TO_IDENTIFIER, lambda m: matches.append(m.span(1)), 0)   
                      return matches
                      
                  for match in find_line_identifiers():
                      _line = editor.lineFromPosition(match[0])
                      if editor.getFoldLevel(_line) & FOLDLEVEL.HEADERFLAG:
                          editor.foldLine(_line,0)
              
                  for match in find_from_to_line_identifiers():
                      start_line, end_line = map(lambda x: int(x)-1, editor.getTextRange(*match).split(','))
                      while True:
                          if start_line <= end_line:
                              if editor.getFoldLevel(start_line) & FOLDLEVEL.HEADERFLAG:
                                  editor.foldLine(start_line,0)
                                  start_line = editor.getLastChild(start_line,-1) + 1
                              else:
                                  start_line += 1
                          else:
                              break 
              
              should_be_folded = False
              def callback_FOLD_BUFFERACTIVATED(args):
                  console.write('callback_FOLD_BUFFERACTIVATED\n')
                  global should_be_folded
                  _state = editor.getProperty('AUTO_FOLDED')
                  if _state == '':
                      filename, extension = os.path.splitext(notepad.getBufferFilename(args['bufferID']))
                      if extension in FILE_EXTENSION_LIST:
                          editor.setProperty('AUTO_FOLDED', '1')
                          should_be_folded = True
                      else:
                          editor.setProperty('AUTO_FOLDED', '-1')
                          should_be_folded = False
                  else:
                      should_be_folded = False
              
              def callback_FOLD_UPDATEUI(args):
                  global should_be_folded
                  if should_be_folded:
                      scan(editor.getLineCount())
                      should_be_folded = False
              
              notepad.callback(callback_FOLD_BUFFERACTIVATED, [NOTIFICATION.BUFFERACTIVATED])
              editor.callback(callback_FOLD_UPDATEUI, [SCINTILLANOTIFICATION.UPDATEUI])
              
              console.write('hello'))
              

              Note the last line which I added. When I open my html file the console output is:

              helloPython 2.7.6-notepad++ r2 (default, Apr 21 2014, 19:26:54) [MSC v.1600 32 bit (Intel)]
              Initialisation took 204ms
              Ready.

              and the lines don’t collapse. When I open another html file, it ouputs callback_FOLD_BUFFERACTIVATED and its lines are collapsed. Switching back to the first file, the same happens (working!!!). But it still doesn’t work right at startup.

              Claudia FrankC 1 Reply Last reply Reply Quote 0
              • Claudia FrankC
                Claudia Frank @Alguem Meugla
                last edited by Claudia Frank

                @Alguem-Meugla

                ok, logic error, change the line

                should_be_folded = False
                

                to

                should_be_folded = True
                

                It is the line which is directly before

                def callback_FOLD_BUFFERACTIVATED(args):
                

                But only this line.

                But I’m still confused that your user startup.py doesn’t work but at least you
                have it working now.

                Cheers
                Claudia

                Alguem MeuglaA 1 Reply Last reply Reply Quote 1
                • Alguem MeuglaA
                  Alguem Meugla @Claudia Frank
                  last edited by

                  @Claudia-Frank
                  Thank you a lot for your help.
                  Could it also be possible to put inside the brackets NPP_COLLAPSE(19,41) how many numbers I ask it for? eg: NPP_COLLAPSE(19,22,28,35,43,54,60,67,89,139,220,…). It actually only works for 2 line numbers. It’s ok if you don’t want to do it, I can keep using NPP_COLLAPSE_IT everywhere. Thank you!

                  Claudia FrankC 1 Reply Last reply Reply Quote 0
                  • Claudia FrankC
                    Claudia Frank @Alguem Meugla
                    last edited by

                    @Alguem-Meugla

                    Yes, slightly different regex and for loop can do this but we need to define the cases.

                    Is a list ALWAYS a pair of fold from line to line triggers?
                    Or is a list with more than two entries ALWAYS fold this line for each item
                    and a list with 2 entries a from … to …
                    Or …

                    Btw, may last post about logic error was not a logic issue.
                    Looks like the initial start of npp doesn’t trigger a bufferactivated callback
                    or python script hasn’t been started at that time to catch this notification.
                    I already changed that and will be included in next version.

                    Cheers
                    Claudia

                    Alguem MeuglaA 1 Reply Last reply Reply Quote 0
                    • Alguem MeuglaA
                      Alguem Meugla @Claudia Frank
                      last edited by

                      @Claudia-Frank
                      Oh I though NPP_COLLAPSE(19,41) was going only to collapse lines 19 and 41, but instead it collapse lines 19 to 41… Thank you anyway. This script is very helpful.

                      Claudia FrankC 1 Reply Last reply Reply Quote 0
                      • Claudia FrankC
                        Claudia Frank @Alguem Meugla
                        last edited by

                        @Alguem-Meugla

                        just for clarification, you are happy? No further modification needed?

                        Cheers
                        Claudia

                        Alguem MeuglaA 1 Reply Last reply Reply Quote 0
                        • Alguem MeuglaA
                          Alguem Meugla @Claudia Frank
                          last edited by

                          @Claudia-Frank said:

                          just for clarification, you are happy? No further modification needed?

                          Yeah, it looks good enough. Ty.

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