Running a macro on all the open docs ?.
-
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 -
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.
-
@Alan-Kilborn Sure would like that ability.
Any plans?
Any help with work-arounds?Thanx for all you do!
-
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)
-
P PeterJones referenced this topic on
-
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.
-
-
@PeterJones Thanx much. I will try to get this working, but I am not too familiar with Python.
-
@Alan-Kilborn Thanx for your input. Much appreciated. Please let me know when your script is available.
-
@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? -
@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?
-
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. -
@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. -
@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 externalpython.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.
-
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.
-
@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. -
@PeterJones OK, Thanx
-
@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.
-
@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.
-
@PeterJones I have read the FAQ
-
@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 win32PythonScript is 2.1.0.0 From FAQ I would remove PythonScript 2, and install 3. But it will not remove from the plugin admin.
-
@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.