Automate/Schedule a 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
schtasksutility is quite powerful ;-))For instance, the line, below, typed in a console windows, would create a task, named
Testwhich executes thetest.batbatch, located in\D:every minute :schtasks /create /sc minute /mo 1 /tn "Test" /tr D:\test.batFor help on this command, which replaces, the old
atcommand, type inschtasks /?To list all the scheduled tasks, simply type
schtasksand validTo delete the
Testtask, type in the commandschtasks /delete /tn test
Here is, below, the detail of the
schtaskscommand ( 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 notepadFor 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 XXXXXwhere 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.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login