Community
    • Login

    Running a macro on all the open docs ?.

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    51 Posts 4 Posters 1.1k 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.
    • Robert Or Janet DiebelR
      Robert Or Janet Diebel @Alan Kilborn
      last edited by

      @Alan-Kilborn Sure would like that ability.
      Any plans?
      Any help with work-arounds?

      Thanx for all you do!

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Robert Or Janet Diebel
        last edited by

        @Robert-Or-Janet-Diebel ,

        The following script for the PythonScript plugin runs the same macro (called DoNothing) on all files open in either view of Notepad++.

        You would have to edit the script to call the name of your Macro, as listed in your Macro menu.

        # encoding=utf-8
        """in response to https://community.notepad-plus-plus.org/topic/21663/
        Run a macro on each open file
        """
        from Npp import notepad
        
        # store active tab in each View
        keepBufferID = notepad.getCurrentBufferID()
        docIndexes = [notepad.getCurrentDocIndex(v) for v in range(2)]
        
        for filename, bufferID, index, view in notepad.getFiles():
            notepad.activateBufferID(bufferID)
            notepad.runMenuCommand("Macro", "DoNothing")
        
        # restore active tab in each View
        for v in range(2):
            notepad.activateIndex(v, docIndexes[v])
        notepad.activateBufferID(keepBufferID)
        
        Robert Or Janet DiebelR 2 Replies Last reply Reply Quote 5
        • PeterJonesP PeterJones referenced this topic on
        • Alan KilbornA
          Alan Kilborn
          last edited by

          Peter’s script above is (mostly) fine, but I’ll offer a couple of comments:

          • I like to save/restore the active tab using a Python “context manager”. This is used by wrapping the code block that changes the active tab into a “with … :” block. The advantage of this approach is that the saving and restoring is integrated; you don’t have to remember to do the restoring, it just happens. My context-manager for this restores based upon “name” rather than index, so if the code wrapped by it changes the tab ordering, or closes/opens tabs, the restore can still work (within reason). I will be publishing this context-manager script soon as part of a larger script.

          • The above script won’t work in this scenario: The active tab is cloned and the view 1 cloned tab is active when the script is run – what happens here is that when the script has finished executing, the view 0 clone’s tab is left as the active one. I believe this is actually a bug in Notepad++ and I will be making an official bug report soon about this.

          Robert Or Janet DiebelR Alan KilbornA 2 Replies Last reply Reply Quote 1
          • Robert Or Janet DiebelR
            Robert Or Janet Diebel @PeterJones
            last edited by

            @PeterJones Thanx much. I will try to get this working, but I am not too familiar with Python.

            PeterJonesP 1 Reply Last reply Reply Quote 0
            • Robert Or Janet DiebelR
              Robert Or Janet Diebel @Alan Kilborn
              last edited by

              @Alan-Kilborn Thanx for your input. Much appreciated. Please let me know when your script is available.

              1 Reply Last reply Reply Quote 0
              • Robert Or Janet DiebelR
                Robert Or Janet Diebel @PeterJones
                last edited by

                @PeterJones I have installed PythonScript, loaded four files and changed the MACRO.
                It is not working for me. Could you give me a procedure for running files with PythonScript please?

                PeterJonesP 1 Reply Last reply Reply Quote 0
                • PeterJonesP
                  PeterJones @Robert Or Janet Diebel
                  last edited by

                  @Robert-Or-Janet-Diebel said in Running a macro on all the open docs ?.:

                  Could you give me a procedure for running files with PythonScript please?

                  FAQ

                  1 Reply Last reply Reply Quote 2
                  • PeterJonesP
                    PeterJones @Robert Or Janet Diebel
                    last edited by

                    I will try to get this working, but I am not too familiar with Python.

                    You don’t need to. Literally all you have to do to edit the script is to change the text DoNothing to be the exact name of your Macro.

                    Robert Or Janet DiebelR 2 Replies Last reply Reply Quote 2
                    • Robert Or Janet DiebelR
                      Robert Or Janet Diebel @PeterJones
                      last edited by

                      @PeterJones When I run it a very quick window flashes up with no time to see any output, or errors.
                      When I open The console I try to paste the program, that does not work.

                      PeterJonesP 2 Replies Last reply Reply Quote 0
                      • PeterJonesP
                        PeterJones @Robert Or Janet Diebel
                        last edited by PeterJones

                        @Robert-Or-Janet-Diebel said in Running a macro on all the open docs ?.:

                        When I run it a very quick window flashes up with no time to see any output, or errors.

                        Are you sure the macro works correctly when you run it once on an open file? Does your macro open a windows command?

                        Because the with the script I gave you, the PythonScript plugin itself should not be opening any windows, but should just be running the macro

                        Are you running the script with the PythonScript plugin, or are you trying to run the blah.py file with an external python.exe instance? If the latter, you are doing it wrong, and need to read the FAQ I linked you to again.

                        If this isn’t enough for you to debug what’s going wrong, you need to tell us the exact steps you took to install the script and run the script.

                        Robert Or Janet DiebelR 1 Reply Last reply Reply Quote 1
                        • PeterJonesP
                          PeterJones @Robert Or Janet Diebel
                          last edited by

                          Sorry, I didn’t see this part of the post when I first replied.

                          @Robert-Or-Janet-Diebel said in Running a macro on all the open docs ?.:

                          When I open The console I try to paste the program, that does not work.

                          The FAQ does not say to paste the program into the PythonScript console. Please read the FAQ again.

                          Robert Or Janet DiebelR 1 Reply Last reply Reply Quote 1
                          • Robert Or Janet DiebelR
                            Robert Or Janet Diebel @PeterJones
                            last edited by

                            @PeterJones I have input a MACRO name that works on individual files in Notepad++.
                            I have it in a file and run it from there, but the files do not change when I run this by clicking on the file name in its folder. That is when the window pops up and disappears.
                            I try to run it from the console but cannot paste it in, or type.
                            Sorry, I am missing something here.

                            PeterJonesP 1 Reply Last reply Reply Quote 0
                            • Robert Or Janet DiebelR
                              Robert Or Janet Diebel @PeterJones
                              last edited by

                              @PeterJones OK, Thanx

                              1 Reply Last reply Reply Quote 0
                              • PeterJonesP
                                PeterJones @Robert Or Janet Diebel
                                last edited by

                                @Robert-Or-Janet-Diebel said in Running a macro on all the open docs ?.:

                                when I run this by clicking on the file name in its folder.

                                The FAQ does not say to run the script from Windows Explorer. It says to run it using the Plugins > PythonScript > scripts > scriptname menu command.

                                Have you read the FAQ I linked to? Because it doesn’t appear that you have.

                                Robert Or Janet DiebelR 4 Replies Last reply Reply Quote 1
                                • Robert Or Janet DiebelR
                                  Robert Or Janet Diebel @PeterJones
                                  last edited by

                                  @PeterJones This does not work. Fearing I made a mistake installing, I tried to remove the plugin and re-install. It will not remove. It remains in installed, and not available.

                                  1 Reply Last reply Reply Quote 0
                                  • Robert Or Janet DiebelR
                                    Robert Or Janet Diebel @PeterJones
                                    last edited by

                                    @PeterJones I have read the FAQ

                                    PeterJonesP 1 Reply Last reply Reply Quote 0
                                    • Robert Or Janet DiebelR
                                      Robert Or Janet Diebel @PeterJones
                                      last edited by

                                      @PeterJones My Python:```
                                      Python 3.13.2 (tags/v3.13.2:4f8bb39, Feb 4 2025, 15:23:48) [MSC v.1942 64 bit (AMD64)] on win32

                                      PythonScript is 2.1.0.0
                                      From FAQ I would remove PythonScript 2, and install 3. But it will not remove from the plugin admin.
                                      1 Reply Last reply Reply Quote 0
                                      • Robert Or Janet DiebelR
                                        Robert Or Janet Diebel @PeterJones
                                        last edited by

                                        @PeterJones Now I see this come up in the console:

                                        Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)]
                                        Initialisation took 62ms
                                        Ready.
                                        

                                        I really am lost here.

                                        1 Reply Last reply Reply Quote 0
                                        • PeterJonesP
                                          PeterJones @Robert Or Janet Diebel
                                          last edited by PeterJones

                                          @Robert-Or-Janet-Diebel ,

                                          I really am lost here.

                                          It looks like you uninstalled the PythonScript 3 that you previously had installed manually, and replaced it using PythonScript 2 in the Plugins admin. I would not have recommended that.

                                          To re-install PythonScript 3 again:

                                          1. Close all instances of Notepad++
                                          2. Go to C:\Program Files\Notepad++\Plugins\ (or your Plugins directory for wherever Notepad++ is installed) and delete the PythonScript directory
                                          3. Follow the instructions in the PythonScript 3 section of the installing-pythonscript in the FAQ (the link takes you to the right line of the FAQ, with the text highlighted, in most browsers), making sure to grab PythonScript_Full_3.0.22.0_x64_PluginAdmin.zip

                                          After PythonScript 3 is installed, the PythonScript console should go back to telling you

                                          Python 3.12.8 (tags/v3.12.8:2dc476b, Dec  3 2024, 19:30:04) [MSC v.1942 64 bit (AMD64)]
                                          

                                          Then move on to the step 2 in the FAQ: create a new scropt, and follow those instructions to create the empty version of the script.

                                          Next continue with step 3 of the FAQ: copy the script from my post and paste it in the new file. tTo avoid forgetting it later, at this point, please edit the script to say your macro’s name instead of DoNothing, and then save the script.

                                          Skip step 4 for now, because you don’t want to confuse yourself with trying to do too much.

                                          Then move down to the Usage section of the FAQ, and read that whole section. Once you are done reading that section, you should understand that the last bullet point explains how to run your script if you don’t have a keyboard shortcut (which you shouldn’t, at this point).

                                          Robert Or Janet DiebelR 1 Reply Last reply Reply Quote 1
                                          • Robert Or Janet DiebelR
                                            Robert Or Janet Diebel @PeterJones
                                            last edited by

                                            @PeterJones ![alt text](c4b5dce7-4030-4246-a5d6-378ea3ba34d4-image.png image url)

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