• Login
Community
  • Login

Running a macro on all the open docs ?.

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
51 Posts 4 Posters 1.2k 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.
  • A
    Alex Sunny 0
    last edited by Aug 14, 2021, 2:59 PM

    Hello,

    Is there a way to run a macro on all open documents? Currently I have to open each document to run a macro which is a pain. I’ve tried multi-selecting the tabs but that doesn’t work.

    thanks
    jackyjoy

    A 1 Reply Last reply Aug 14, 2021, 5:55 PM Reply Quote 0
    • A
      Alan Kilborn @Alex Sunny 0
      last edited by Aug 14, 2021, 5:55 PM

      @Alex-Sunny-0

      Short answer is No, but the longer answer is that you could probably write a PythonScript or maybe a NppExec script that could turn the answer into a Yes (but that’s a longer answer).

      If you have interest in that, post back.

      R 1 Reply Last reply Apr 2, 2025, 7:51 PM Reply Quote 1
      • R
        Robert Or Janet Diebel @Alan Kilborn
        last edited by Apr 2, 2025, 7:51 PM

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

        Thanx for all you do!

        P 1 Reply Last reply Apr 2, 2025, 8:35 PM Reply Quote 0
        • P
          PeterJones @Robert Or Janet Diebel
          last edited by Apr 2, 2025, 8:35 PM

          @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)
          
          R 2 Replies Last reply Apr 5, 2025, 11:52 AM Reply Quote 5
          • P PeterJones referenced this topic on Apr 2, 2025, 8:35 PM
          • A
            Alan Kilborn
            last edited by Apr 4, 2025, 11:26 AM

            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.

            R A 2 Replies Last reply Apr 5, 2025, 11:56 AM Reply Quote 1
            • R
              Robert Or Janet Diebel @PeterJones
              last edited by Apr 5, 2025, 11:52 AM

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

              P 1 Reply Last reply Apr 5, 2025, 1:52 PM Reply Quote 0
              • R
                Robert Or Janet Diebel @Alan Kilborn
                last edited by Apr 5, 2025, 11:56 AM

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

                1 Reply Last reply Reply Quote 0
                • R
                  Robert Or Janet Diebel @PeterJones
                  last edited by Apr 5, 2025, 12:20 PM

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

                  P 1 Reply Last reply Apr 5, 2025, 1:51 PM Reply Quote 0
                  • P
                    PeterJones @Robert Or Janet Diebel
                    last edited by Apr 5, 2025, 1:51 PM

                    @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
                    • P
                      PeterJones @Robert Or Janet Diebel
                      last edited by Apr 5, 2025, 1:52 PM

                      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.

                      R 2 Replies Last reply Apr 5, 2025, 6:00 PM Reply Quote 2
                      • R
                        Robert Or Janet Diebel @PeterJones
                        last edited by Apr 5, 2025, 6:00 PM

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

                        P 2 Replies Last reply Apr 5, 2025, 6:03 PM Reply Quote 0
                        • P
                          PeterJones @Robert Or Janet Diebel
                          last edited by PeterJones Apr 5, 2025, 6:03 PM Apr 5, 2025, 6:03 PM

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

                          R 1 Reply Last reply Apr 6, 2025, 7:16 PM Reply Quote 1
                          • P
                            PeterJones @Robert Or Janet Diebel
                            last edited by Apr 5, 2025, 6:04 PM

                            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.

                            R 1 Reply Last reply Apr 5, 2025, 6:09 PM Reply Quote 1
                            • R
                              Robert Or Janet Diebel @PeterJones
                              last edited by Apr 5, 2025, 6:07 PM

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

                              P 1 Reply Last reply Apr 5, 2025, 6:20 PM Reply Quote 0
                              • R
                                Robert Or Janet Diebel @PeterJones
                                last edited by Apr 5, 2025, 6:09 PM

                                @PeterJones OK, Thanx

                                1 Reply Last reply Reply Quote 0
                                • P
                                  PeterJones @Robert Or Janet Diebel
                                  last edited by Apr 5, 2025, 6:20 PM

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

                                  R 4 Replies Last reply Apr 5, 2025, 7:28 PM Reply Quote 1
                                  • R
                                    Robert Or Janet Diebel @PeterJones
                                    last edited by Apr 5, 2025, 7:28 PM

                                    @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
                                    • R
                                      Robert Or Janet Diebel @PeterJones
                                      last edited by Apr 5, 2025, 7:29 PM

                                      @PeterJones I have read the FAQ

                                      P 1 Reply Last reply Apr 5, 2025, 8:38 PM Reply Quote 0
                                      • R
                                        Robert Or Janet Diebel @PeterJones
                                        last edited by Apr 5, 2025, 7:50 PM

                                        @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
                                        • R
                                          Robert Or Janet Diebel @PeterJones
                                          last edited by Apr 5, 2025, 8:02 PM

                                          @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
                                          • First post
                                            Last post
                                          The Community of users of the Notepad++ text editor.
                                          Powered by NodeBB | Contributors