Marco's do not record and execute shortcuts.
-
I have searched the forum, but I can not find any topic that solves my problem.
I have installed the plugin “MIME Tools”.
In the “MIME Tools” menu there is for instance “Base64 Encode with Padding” to which I connected the shortcut “Ctrl/Shift/NumPad +”
To the “Base64 Decode” I connected the shortcut “Ctrl/Shift/NumPad -”.Both shortcuts work file.
I select all data, press “Ctrl/Shift/NumPad +” and I see the data is converted to Base64.
I select all data again, press “Ctrl/Shift/NumPad -” and my original data appers again.Now I want to create two marco’s.
Macro1: Select all data --> Press “Ctrl/Shift/NumPad +” --> Select all data --> Copy
Macro 2: Select all data --> Press “Ctrl/Shift/NumPad -” -->Press “Alt/Ctrl/Shift/B” --> Select all data --> CopyWhen I do this manually, all works fine.
When I record a macro, all works fine.
When I play back the macro, only the select aal happens.How do I make a macro that does what I need please?
-
Background
Plugin commands (even for the default plugins like MIME Tools) are not macro-recordable. However, with a bit of effort on your part, they can be macro-playable (with some limitations).
The reason that they aren’t macro-recordable is that macros make use of the menu command ID for storing which command to use. For built-in menu commands, those commands all have a fixed ID, so from one run of Notepad++ to the next, they will always keep the same menu command ID. Since the plugins available and taking up menu space can be different from run-to-run in Notepad++, the application actually assigns the menu command IDs for each plugin dynamically. Thus, if you install or upgrade a plugin such that the number of plugin menu commands change, the next time you run Notepad++, other plugins will possibly get different command IDs compared to the previous run (depending on what order Notepad++ processes each of the plugins).
Due to this dynamic menu command ID, Notepad++ would not be able to guarantee that a command ID it records for the macro would work for the same command the next time Notepad++ is run, and thus does not record macro commands.
There are two workarounds:
-
Use a scripting plugin (like PythonScript) instead of Macros to do your automation (because scripting plugins usually have helper commands that can search through the menu to be able to run even plugin menu commands). This adds overhead, and you might have to learn a new programming language, but it gives you a lot more power.
-
Cheat the macro system. This will be described below, but has the downside that you have to manually edit the macro XML, and you might have to tweak it if you install or upgrade plugins.
Cheat the Macro System
As I said earlier, the macros are dynamic, depending on the order that Notepad++ processes the plugins and how many menu commands each plugin uses. However, for any stretch of time where you don’t install any new plugins or upgrade existing plugins, Notepad++ will process your plugins in the same order, so they will get the menu command IDs for each run.
So to cheat the macro, you can snoop to find the current menu command ID for each action, then manually edit the macro’s XML, then restart Notepad++, and the macro should work until the next time you install or update a plugin.
-
To snoop the menu command ID, I recommend the NppUISpy plugin. So install that plugin, then run it’s Spy! command (either from the menu or toolbar icon).
- For example, with my current plugins, Spy! tells me that Base64 Encode with Padding is Command Id =
22064
- For example, with my current plugins, Spy! tells me that Base64 Encode with Padding is Command Id =
-
Open
%AppData%\Notepad++\shortcuts.xml
and edit your recorded macro.It would start something like: WRONG: see replies
That is the Select All command twice and the copy (skipping the plugin command between the two Select All. (My macro is called<Macro name="RecordedMacro" Ctrl="no" Alt="no" Shift="no" Key="0"> <Action type="2" message="0" wParam="42007" lParam="0" sParam="0" /> <Action type="2" message="0" wParam="42007" lParam="0" sParam="0" /> <Action type="2" message="0" wParam="42002" lParam="0" sParam="0" /> </Macro>
RecordedMacro
and has no shortcut; your name and shortcut will be different than mine.)You would need to edit it to be like: WRONG: see replies
which puts the<Macro name="RecordedMacro" Ctrl="no" Alt="no" Shift="no" Key="0"> <Action type="2" message="0" wParam="42007" lParam="0" sParam="0" /> <Action type="2" message="0" wParam="22064" lParam="0" sParam="0" /> <Action type="2" message="0" wParam="42007" lParam="0" sParam="0" /> <Action type="2" message="0" wParam="42002" lParam="0" sParam="0" /> </Macro>
22064
that I found with NppUISpy in between the two Select All instances- Save the
shortcuts.xml
and restart Notepad++
At this point, running your macro should do the select all, the conversion, the select all, and the copy.
You would similarly edit the second macro. (You could do both the edits at once; this was just a single example to show you what needs to be done.)
The next time you installed or upgraded a plugin, you would need to re-run Spy!, and check whether the command ID has changed; if it has, edit
shortcuts.xml
again, and replace the22064
with the new number.Useful Reference
—
update: since this has come up before, so I know it’s a common desire (I’ve done it myself sometimes), I took my answer from here, tweaked it, and added it as a SubFAQ in the FAQ: Automating Notepad++ page. So thanks to you, our FAQ has gotten better. :-) -
-
@PeterJones
Thank you so much for your quick response.
I am trying to make this work.I recorded two marco’s.
One for encode and one for decode.
Both have shortcuts,
In the shortcuts.xml this resulted in this: (I put the comments only in this post. It’s not in the file)<Macro name="BASE64 Decode" Ctrl="yes" Alt="yes" Shift="yes" Key="109"> <Action type="0" message="2316" wParam="0" lParam="0" sParam="" /> <!--Home--> <Action type="0" message="2013" wParam="0" lParam="0" sParam="" /> <!--Select All--> <Action type="0" message="2013" wParam="0" lParam="0" sParam="" /> <!--Select All--> <Action type="0" message="2178" wParam="0" lParam="0" sParam="" /> <!--Copy--> </Macro> <Macro name="BASE64 Encode with Padding" Ctrl="yes" Alt="yes" Shift="yes" Key="107"> <Action type="0" message="2316" wParam="0" lParam="0" sParam="" /> <!--Home--> <Action type="0" message="2013" wParam="0" lParam="0" sParam="" /> <!--Select All--> <Action type="0" message="2013" wParam="0" lParam="0" sParam="" /> <!--Select All--> <Action type="0" message="2178" wParam="0" lParam="0" sParam="" /> <!--Copy--> </Macro>
Spy tells me:
- Plugin Tools
==> MIME Tools
====> Base64 Encode with padding
Command ID = 22126 - Plugin Tools
==> MIME Tools
====> Base64 Decode
Command ID = 22129 - Plugin Tools
==> XML Tools
====>Pretty print
Command ID = 22216
So…I modified the macros to: (Again…the comments are not in the shortcuts.xml)
<Macro name="BASE64 Decode" Ctrl="yes" Alt="yes" Shift="yes" Key="109"> <Action type="0" message="2316" wParam="0" lParam="0" sParam="" /> <!--Home--> <Action type="0" message="2013" wParam="0" lParam="0" sParam="" /> <!--Select All--> <Action type="0" message="22129" wParam="0" lParam="0" sParam="" /><!--Mime Decode--> <Action type="0" message="22216" wParam="0" lParam="0" sParam="" /><!--XML Pretty print--> <Action type="0" message="2013" wParam="0" lParam="0" sParam="" /> <!--Select All--> <Action type="0" message="2178" wParam="0" lParam="0" sParam="" /> <!--Copy--> </Macro> <Macro name="BASE64 Encode with Padding" Ctrl="yes" Alt="yes" Shift="yes" Key="107"> <Action type="0" message="2316" wParam="0" lParam="0" sParam="" /> <!--Home--> <Action type="0" message="2013" wParam="0" lParam="0" sParam="" /> <!--Select All--> <Action type="0" message="22126" wParam="0" lParam="0" sParam="" /><!--Mime Encode with padding--> <Action type="0" message="2013" wParam="0" lParam="0" sParam="" /> <!--Select All--> <Action type="0" message="2178" wParam="0" lParam="0" sParam="" /> <!--Copy--> </Macro>
Result?
In both cases: Only the “Home”, “Select All” and the “Copy” happens…nothing else.What am I doing wrong?
(I did test with ‘type=“2”’ but then nothing works.) - Plugin Tools
-
Sorry, you are right. That’s what I get for not testing before publishing code.
<Macro name="DuijsensEdited" Ctrl="no" Alt="no" Shift="no" Key="0"> <Action type="0" message="2316" wParam="0" lParam="0" sParam="" /> <!--Home--> <Action type="0" message="2013" wParam="0" lParam="0" sParam="" /> <!--Select All--> <Action type="2" message="0" wParam="22064" lParam="0" sParam="" /> <!--Mime Encode with padding--> <Action type="0" message="2013" wParam="0" lParam="0" sParam="" /> <!--Select All--> <Action type="0" message="2178" wParam="0" lParam="0" sParam="" /> <!--Copy--> </Macro>
The select/copy are scintilla commands, and so need type=“0” with the scintilla command number in the message=“####” – I had mistakenly handcrafted them assuming they would use Notepad++'s command IDs. But the Plugin commands are menu commands, and they need to be type=“2” with the menu command ID (from Spy!) going in the wParam=“###” … (You can, of course, use your name and keyboard shortcut information rather than mine.)
The decode and the pretty print in your second macro will also both need to be type=“2” with the number in wParam
Sorry for the confusion.