• Login
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.
  • C
    Claudia Frank @Alguem Meugla
    last edited by Claudia Frank Jul 18, 2017, 7:16 PM Jul 18, 2017, 7:16 PM

    @Alguem-Meugla

    either install python script plugin via plugin manager or by using the msi package .
    I would suggest to use the msi package as it has been reported that installation via plugin manager
    fails from time to time.

    Once installed, goto Plugins->Python Script->New Script and name it startup.py .
    and press Save button.
    Copy the posted script into the newly created document, save it and restart npp.

    Cheers
    Claudia

    1 Reply Last reply Reply Quote 1
    • C
      Claudia Frank @Alguem Meugla
      last edited by Jul 18, 2017, 7:27 PM

      @Alguem-Meugla

      sorry - I forgot one step.

      before you restart npp, goto
      Plugins->Python Script->Configuration
      and instead of Initialisation: LAZY choose ATSTARTUP.

      Cheers
      Claudia

      A 1 Reply Last reply Jul 18, 2017, 8:01 PM Reply Quote 1
      • A
        Alguem Meugla @Claudia Frank
        last edited by Alguem Meugla Jul 18, 2017, 8:02 PM Jul 18, 2017, 8:01 PM

        @Claudia-Frank
        I copied the script to Plugins>Python Script>Scripts>(Ctrl+)startup and saved as administrator.
        I also did what you said on Configuration.
        But it is doing nothing when I start my file with “NPP_COLLAPSE_IT” on a line with a -. I then changed the script and added a line “print(‘startup.py done!’)” on the end trying to see if Plugins>Python Script>Show Console showed it, but nothing appears there other than Ready (and some more things above it).

        C 1 Reply Last reply Jul 18, 2017, 8:06 PM Reply Quote 1
        • C
          Claudia Frank @Alguem Meugla
          last edited by Jul 18, 2017, 8:06 PM

          @Alguem-Meugla

          I don’t understand what you mean by

          I copied the script to Plugins>Python Script>Scripts>(Ctrl+)startup and saved as administrator.

          you should have done

          goto Plugins->Python Script->New Script and name it startup.py
          and press Save button.
          Copy the posted script into the newly created document, save it and restart npp.

          Depending on your installation, the script directory is different,
          by using the way I mentioned you make sure that the correct directory is used.

          Cheers
          Claudia

          A 1 Reply Last reply Jul 18, 2017, 8:10 PM Reply Quote 0
          • A
            Alguem Meugla @Claudia Frank
            last edited by Jul 18, 2017, 8:10 PM

            @Claudia-Frank said:

            you should have done

            goto Plugins->Python Script->New Script and name it startup.py
            and press Save button.
            Copy the posted script into the newly created document, save it and restart npp.

            Depending on your installation, the script directory is different,
            by using the way I mentioned you make sure that the correct directory is used.

            When I go on New Script it puts me on Documents. Should I create the file there or in a place I want? I had created a folder there and put the new file named startup.py with the script inside it, but wasn’t working, so I replaced the standard startup.py with the new one. Didn’t work.

            C 1 Reply Last reply Jul 18, 2017, 8:16 PM Reply Quote 0
            • C
              Claudia Frank @Alguem Meugla
              last edited by Jul 18, 2017, 8:16 PM

              @Alguem-Meugla

              yes, you should save it in the default directory, the one which
              get opened by using New Script.

              The standard startup.py is used do the general setups and initialization.
              I do not recommend to modify this file as long as you know what you do.
              I hope you do have a copy of the old one and you can revert your changes.
              If not, a reinstall of the python script plugin is needed I guess.

              Cheers
              Claudia

              A 1 Reply Last reply Jul 19, 2017, 7:30 PM Reply Quote 0
              • A
                Alguem Meugla @Claudia Frank
                last edited by Alguem Meugla Jul 19, 2017, 7:33 PM Jul 19, 2017, 7:30 PM

                @Claudia-Frank said:

                yes, you should save it in the default directory, the one which
                get opened by using New Script.

                I think that there is not a place which any plugin/program can trust. When I use New Script it opens to me the directory I left the dialog on the last time, MyDocuments\Python Notepad++ (the folder I created).

                After reinstalling the plugin and having the default startip.py back, I tried having a copy of the new startup.py on MyDocuments, another on MyDocuments\Python Notepad++, and another on %APPDATA%\Notepad++\plugins\config\PythonScript\scripts (the plugin’s Help said it was the User directory). All those copys should also print some words after your script. After restarting N++ it doesn’t print anything neither collapse the line it should (https://image.prntscr.com/image/Wa9pMODERIm7Kq7cnHhZAg.png ).

                I also tried having one copy on the same folder as the Samples folder and the default startup.py (inside a folder I created so it didn’t have problems with 2 startup.py ’s). After restarting N++ nothing happened, but it was appearing on the Scripts list so I clicked it and: no collapse and the print worked (this means the script is not collapsing for some reason, maybe because it’s not happening at startup?).

                Thank you for the help and time you are giving to this.

                C 1 Reply Last reply Jul 19, 2017, 8:00 PM Reply Quote 0
                • C
                  Claudia Frank @Alguem Meugla
                  last edited by Jul 19, 2017, 8:00 PM

                  @Alguem-Meugla

                  Can you post your debug->info (available under ? menu?

                  In addition, what do you see if you run the following code

                  notepad.getVersion()

                  in the python script console, like here

                  Cheers
                  Claudia

                  A 1 Reply Last reply Jul 19, 2017, 8:54 PM Reply Quote 0
                  • A
                    Alguem Meugla @Claudia Frank
                    last edited by Jul 19, 2017, 8:54 PM

                    @Claudia-Frank said:

                    Can you post your debug->info (available under ? menu?

                    Here it is:
                    Notepad++ v7.4.2 (32-bit)
                    Build time : Jun 18 2017 - 23:34:19
                    Path : C:\Program Files (x86)\Notepad++\notepad++.exe
                    Admin mode : OFF
                    Local Conf mode : OFF
                    OS : Windows 10 (64-bit)
                    Plugins : JSMinNPP.dll mimeTools.dll NppConverter.dll NppExec.dll NppExport.dll NppFTP.dll NppTextFX.dll PluginManager.dll PythonScript.dll ReloadButton.dll

                    In addition, what do you see if you run the following code

                    notepad.getVersion()

                    in the python script console, like here

                    It returns:
                    (7, 4, 2)

                    Definitely version 7.4.2.

                    C 1 Reply Last reply Jul 19, 2017, 9:12 PM Reply Quote 0
                    • C
                      Claudia Frank @Alguem Meugla
                      last edited by Claudia Frank Jul 19, 2017, 9:14 PM Jul 19, 2017, 9:12 PM

                      @Alguem-Meugla

                      Ok, the command was to see if the python script plugin works in general and
                      the debug info shows, that two directories are of interest in regards to automated startup.

                      The standard startup.py , the one which is automatically created during installation
                      should be located under C:\Program Files (x86)\Notepad++\plugins\PythonScript\scripts\

                      The user startup.py , the one you should have created should be located under
                      %APPDATA%\Notepad++\plugins\config\PythonScript\scripts

                      Concerning the print statement - a standard python script installation has has the following
                      entry in the standard startup.py

                      sys.stdout = editor

                      This means, that a print … will not be send to the console but to the currently active document.
                      In order to change this behavior you need to modify it like this

                      sys.stdout = console

                      and restart npp. Now a print statement would be send to console.

                      In addition, there should be a file %APPDATA%\Notepad++\plugins\config\PythonScriptStartup.cnf.

                      Can you post its content?

                      Cheers
                      Claudia

                      A 1 Reply Last reply Jul 19, 2017, 9:27 PM Reply Quote 0
                      • A
                        Alguem Meugla @Claudia Frank
                        last edited by Alguem Meugla Jul 19, 2017, 9:28 PM Jul 19, 2017, 9:27 PM

                        @Claudia-Frank said:

                        Concerning the print statement - a standard python script installation has has the following
                        entry in the standard startup.py

                        sys.stdout = editor

                        This means, that a print … will not be send to the console but to the currently active document.
                        In order to change this behavior you need to modify it like this

                        sys.stdout = console

                        and restart npp. Now a print statement would be send to console.

                        I don’t know python (i only learnt the first lessons of it on codecademy), so I didn’t know if print was supposed to write on the console or in the file. First I thought it was on the console so I was always opening it to see if there was something. But when I tested the script on the same folder as the Samples and the default startup.py I found it was printing in the file. Not a problem. I have always checked in both places for the print output.

                        In addition, there should be a file %APPDATA%\Notepad++\plugins\config\PythonScriptStartup.cnf.

                        Can you post its content?

                        SETTING/PREFERINSTALLEDPYTHON/0 and on the next line
SETTING/STARTUP/ATSTARTUP

                        C 1 Reply Last reply Jul 19, 2017, 9:41 PM Reply Quote 0
                        • C
                          Claudia Frank @Alguem Meugla
                          last edited by Claudia Frank Jul 19, 2017, 9:41 PM Jul 19, 2017, 9:41 PM

                          @Alguem-Meugla

                          Looks ok - do you see two startup.py if you
                          goto Plugins->Python Script->Scripts ?

                          One should have the addition (User).

                          If you press the CTRL key while clicking onto the user startup.py it should open as a document.

                          If this is the case, add the following line under the bufferactivated callback.

                          console.write('callback_FOLD_BUFFERACTIVATED\n')
                          

                          Should look like

                          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
                          

                          Restart npp.

                          Now, every time you switch a tab you should see the line

                          callback_FOLD_BUFFERACTIVATED
                          

                          written to the console - to prove if the script runs at all.

                          Cheers
                          Claudia

                          A 1 Reply Last reply Jul 20, 2017, 5:35 PM Reply Quote 0
                          • A
                            Alguem Meugla @Claudia Frank
                            last edited by Jul 20, 2017, 5:35 PM

                            @Claudia-Frank said:

                            Looks ok - do you see two startup.py if you
                            goto Plugins->Python Script->Scripts ?

                            There’s only one at Plugins->Python Script->Scripts, the default startup.py . I can only add the new startup.py if I create a new folder like Samples or I put it inside the Samples folder:
                            image

                            I also have one new startup.py at %APPDATA%\Notepad++\plugins\config\PythonScript\scripts\startup.py and more at MyDocuments…
                            Should it appear there as a second startup.py because of this?

                            Restart npp.

                            Now, every time you switch a tab you should see the line

                            callback_FOLD_BUFFERACTIVATED
                            written to the console - to prove if the script runs at all.

                            The console outputs nothing when I switch tab, neither when I click the script (the blue startup.py above in the image).

                            C 1 Reply Last reply Jul 20, 2017, 6:20 PM Reply Quote 0
                            • C
                              Claudia Frank @Alguem Meugla
                              last edited by Claudia Frank Jul 20, 2017, 6:22 PM Jul 20, 2017, 6:20 PM

                              @Alguem-Meugla

                              Should it appear there as a second startup.py because of this?

                              Yes, because your installation shows

                              Local Conf mode : OFF

                              this means, that notepad++ configuration files and plugin directories are located under

                              %APPDATA%\Notepad++

                              therefore, if you use Plugins->Python Script->New Script the script gets created in

                              %APPDATA%\Notepad++\plugins\config\PythonScript\scripts

                              As there is no startup.py in this directory per default you can create it and it should appear
                              in the same level as the standard startup.py but with the addition of (User) (done by Python Script plugin).

                              Please enter the following commands in the python console and post the ouput

                              import sys
                              print '\n'.join(sys.path)
                              

                              Cheers
                              Claudia

                              A 1 Reply Last reply Jul 21, 2017, 6:30 PM Reply Quote 0
                              • A
                                Alguem Meugla @Claudia Frank
                                last edited by Jul 21, 2017, 6:30 PM

                                @Claudia-Frank said:

                                this means, that notepad++ configuration files and plugin directories are located under

                                %APPDATA%\Notepad++

                                therefore, if you use Plugins->Python Script->New Script the script gets created in

                                %APPDATA%\Notepad++\plugins\config\PythonScript\scripts

                                As there is no startup.py in this directory per default you can create it and it should appear
                                in the same level as the standard startup.py but with the addition of (User) (done by Python Script plugin).

                                New Script takes me to the last place I left the dialog on:
                                gif not avaiable

                                And I don’t have a startup.py (User) on the Scripts menu, while there’s one at %APPDATA%\Notepad++\plugins\config\PythonScript\scripts:
                                image not avaiable

                                Please enter the following commands in the python console and post the ouput

                                import sys
                                print '\n'.join(sys.path)
                                

                                C:\Program Files (x86)\Notepad++\plugins\PythonScript\lib
                                C:\Users\Alguem\AppData\Roaming\Notepad++\plugins\Config\PythonScript\lib
                                C:\Program Files (x86)\Notepad++\plugins\PythonScript\scripts
                                C:\Users\Alguem\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts
                                C:\Program Files (x86)\Notepad++\plugins\PythonScript\lib\lib-tk
                                C:\Program Files (x86)\Notepad++\python27.zip
                                C:\Program Files (x86)\Notepad++\DLLs
                                C:\Program Files (x86)\Notepad++\lib
                                C:\Program Files (x86)\Notepad++\lib\plat-win
                                C:\Program Files (x86)\Notepad++\lib\lib-tk
                                C:\Program Files (x86)\Notepad++

                                C 1 Reply Last reply Jul 21, 2017, 10:40 PM Reply Quote 0
                                • C
                                  Claudia Frank @Alguem Meugla
                                  last edited by Jul 21, 2017, 10:40 PM

                                  @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
                                  • A
                                    Alguem Meugla
                                    last edited by Jul 22, 2017, 5:49 PM

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

                                    C 1 Reply Last reply Jul 22, 2017, 6:23 PM Reply Quote 0
                                    • C
                                      Claudia Frank @Alguem Meugla
                                      last edited by Jul 22, 2017, 6:23 PM

                                      @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

                                      A 2 Replies Last reply Jul 22, 2017, 11:45 PM Reply Quote 0
                                      • A
                                        Alguem Meugla @Claudia Frank
                                        last edited by Jul 22, 2017, 11:45 PM

                                        @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
                                        • A
                                          Alguem Meugla @Claudia Frank
                                          last edited by Jul 23, 2017, 12:08 AM

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

                                          C 1 Reply Last reply Jul 23, 2017, 12:28 AM Reply Quote 0
                                          28 out of 44
                                          • First post
                                            28/44
                                            Last post
                                          The Community of users of the Notepad++ text editor.
                                          Powered by NodeBB | Contributors