Automate/Schedule a Macro
-
Hi,
I’ve created a macro that I would like to run at a scheduled interval, e.g. every 15 min.
Is there a way to schedule a macro to run?
Thanks,
Pat -
There isn’t a way to do that natively.
However, there may be a way using one of the scripting plugins, like PythonScript (or the others listed below). The PythonScript help lists the various notification signals that you can trap for a callback, but I didn’t see any that appeared to be timer-based – though I may have missed one.
As an alternative, I am sure that Python or Lua (the programming languages, independent of the PythonScript or LuaScript plugins) have some manner of setting and handling timer-based alarms. During your startup (for PythonScript, use the
startup.py
), set an alarm; when the alarm is handled, have it usenotepad.runMenuCommand()
(or equivalent) to call the macro, and then have it schedule itself (set the alarm) again. I don’t know enough Python (or Lua) to work up an example for you, sorry.-----
Scripting Plugins (Links)
- PythonScript
- PythonScript HOME
- PythonScript DOWNLOAD
- HELP =
Plugins > Python Script > Context-Help
- Getting Started with Python
- LuaScript
- jN: JavaScript for Notepad++
- NppExec
- PythonScript
-
I will have to dig through PythonScript when I get some time.
In the mean time, I use Windows and someone suggested using Windows Task Scheduler. Any idea what file or script I would have it call to run the macro?
-
you cannot use task scheduler to start a specific macro within notepad++(npp).
Task scheduler can only be used to start programs like npp.
May I ask you what exactly you try to do with the macro? Maybe there is a “better” way doing it.Eko
-
macro that I would like to run at a scheduled interval
This Pythonscript code shows one way of doing it:
import threading def timer_callback(): print('hello world every 3 seconds!') # or, for example, notepad.runMenuCommand(menuName, menuOption) threading.Timer(3.0, timer_callback).start() timer_callback()
-
@Scott-Sumner said:
def timer_callback():
print(‘hello world every 3 seconds!’) # or, for example, notepad.runMenuCommand(menuName, menuOption)
threading.Timer(3.0, timer_callback).start()timer_callback()
Hi Scott,
I tried your script as follows:
def timer_callback():
notepad.runMenuCommand(Macro, my-macro)
threading.Timer(3.0, timer_callback).start()timer_callback()
I am getting “NameError: name ‘notepad’ is not defined.”
Any thoughts on how to resolve that?
-
Hi, @patrick-coleman, @scott-sumner and All,
Just a piece of information :
When you want to execute a Windows program or batch script, on specific intervals, the Microsoft
schtasks
utility is quite powerful ;-))For instance, the line, below, typed in a console windows, would create a task, named
Test
which executes thetest.bat
batch, located in\D:
every minute :schtasks /create /sc minute /mo 1 /tn "Test" /tr D:\test.bat
For help on this command, which replaces, the old
at
command, type inschtasks /?
To list all the scheduled tasks, simply type
schtasks
and validTo delete the
Test
task, type in the commandschtasks /delete /tn test
Here is, below, the detail of the
schtasks
command ( Sorry, it’s written in French on my system, of course ! )SCHTASKS /paramètre [arguments] Description : Permet à un administrateur de créer, supprimer, effectuer des requêtes, modifier, exécuter et mettre fin à des tâches planifiées sur un système local ou distant. Remplace AT.exe. Liste de paramètres : /Create Crée une nouvelle tâche planifiée. /Delete Supprime les tâches planifiées. /Query Affiche toutes les tâches planifiées. /Change Modifie les propriétés d'une tâche planifiée. /Run Exécute la tâche planifiée immédiatement. /End Arrête la tâche planifiée actuellement en cours d'exécution. /? Affiche cet écran d'aide. Exemples : SCHTASKS SCHTASKS /? SCHTASKS /Run /? SCHTASKS /End /? SCHTASKS /Create /? SCHTASKS /Delete /? SCHTASKS /Query /? SCHTASKS /Change /?
Well, now the deal is how to connect that stuff with the action of running a macro from within N++ ;-)) Not tried, yet !
Best Regards,
guy038
-
@guy038 said:
Well, now the deal is how to connect that stuff with the action of running a macro from within N++ ;-)) Not tried, yet !
Yea…good luck. I’ll look forward to seeing the solution on THAT one. :-)
-
@Patrick-Coleman said:
I am getting “NameError: name ‘notepad’ is not defined.”
You have to understand Python/Pythonscript a bit for this one. The short story is you probably need this at the top of the script:
from Npp import notepad
For my setup, I put that line in my
startup.py
(which runs upon Notepad++/Pythonscript-plugin startup, hence its name), so I don’t have to put that in my individual script files (but you can do it that way). -
@Scott-Sumner said:
I’ll look forward to seeing the solution on THAT one. :-)
Seems it’s time for another nice hackish (though unnecessary) solution. Here we go. :-)
- Download and install my NppUISpy plugin. Follow the instructions for its manual installation.
- Download the tool NirCmd (scroll down to the end of the text to find the download links) and unpack the zip file to a directory where you have write access.
- Start Notepad++, go to Plugins menu and launch the NppUISpy plugin by clicking on its Spy! menu entry.
- In the left pane of the plugin’s UI go to the tree node of the Macro menu and open it by clicking on the small
+
sign to its left. Find the entry of the macro you want to start and remember the related number in the Command Id column. Please note: The tree items respond to mouse clicks and make Notepad++ to execute the related command. Maybe you prefer to click only on the tree lines at the left and the small+
signs to prevent that. The toolbar button items in the right pane respond to mouse clicks as well. - Start a console window and navigate to the directory where you have unpacked NirCmd.
- Type the following command:
nircmdc win postmsg ititle "Notepad++" 2072 0 XXXXX
where XXXXX is the number you remembered in step 4.
The command of step 6 can be put into a batch file which in turn can be executed by a scheduled task.
-
I knew it was going to get wild and crazy like that. :-)
It’s fine though as there is a Notepad++ tie in, so we are not getting into “off-topic” land…
Curious, though, why did you say “(though unnecessary)”…?
-
@Scott-Sumner said:
Curious, though, why did you say “(though unnecessary)”…?
Because you have already posted a (I guess) working Python solution.
-
I see. Well, we don’t know yet that the Pythonscript-based solution will work for the OP, so it is still nice to have other options. BTW, I like the looks of your “spy” plugin! (see…without the alternative solution path being presented, I never would have learned about that plugin!)
-
Does the “spy” plugin take a large amount of time to initialize when it starts up? I added it to my setup and it seems to have some odd interactions with some of my Pythonscript stuff that runs on startup. I haven’t investigated further yet but the first thing I noticed is that it seems to take longer now for my Notepad++ to get to a “ready for editing” state.
-
Hmm, the plugin itself does nearly nothing when it is loaded, it only adds its menu entries to the Notepad++ UI. Only when you click on the Spy! menu entry it examines the main menu and the toolbar of Notepad++ and fills its trees. All these data gets thrown away when you close the dialog.
But it is a Delphi application and as such it incorporates the needed parts of the VCL (Visual Component Library), the abstraction layer of Delphi for the Win32 API. In this context it loads a noticeable amount of Windows DLL files. Maybe this causes the increased load time.
I have installed about to 45 plugins, thus my Notepad++ needs some time to start up anyway and I was not aware of an increased load time after adding the new plugin.