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.
-
IT WORKS!!!
Damn…you have NO idea how happy I am with this.
Thank you so much!
You are my hero!
g
-
Three are a bit much, where one (at most) would have definitely sufficed.
That definitely does look like you though!
We’re glad that you were able to achieve your goal.
You can also UPVOTE @PeterJones to show your appreciation. -
@Alan-Kilborn
Yeah…sorry…This just opens up a whole bunch of automations that will make my work so much easier.
It’s not just these two macro’s, but so much more.
(I tried to edit my reply to take two down, but I can’t anymore)Upvote @PeterJones .
Ow, yes…I absolutely want to do that…but I don’t see how. (yet)
Where do I do that please? -
-
@Alan-Kilborn
Ah, yes…I already did that.
Thanks. -
@PeterJones
As part om my Macro, I want also to set the ‘Language’ automatically to “XML”.
I have tried several versions of the Macro lines.<Action type="2" message="0" wParam="46006" lParam="0" sParam="" /> <!-- Set language to XML -->
and also
<Action type="0" message="46006" wParam="0" lParam="0" sParam="" /> <!-- Set language to XML -->
and several other versions as well.
Up to now, I have been unable to make this work.How can I set the file Language to XML in a Macro please?
-
You need to read and understand https://npp-user-manual.org/docs/config-files/#macros , which describes which commands require which parameters. Type0 and Type1 are for the Scintilla messages only; type2 are for menu commands (whether built-in, or the cheater version for plugins); type3 are search/replace commands. That section of the manual also tells you where you can look to find the possible
message=
values for the scintilla type 0 and 1 actions, and the built-in menu-command-IDs for type 2 actions; it also directs you to the right section of the Searching page for finding out how to use the type=3 search/replace Actions.The Language menu entries are menu commands, so they must be type2.
Randomly trying type=0 for menu commands will not work.I have a macro which, among other things, sets the language for the current document to “Perl”, and the following Action works to accomplish that.
<Action type="2" message="0" wParam="46013" lParam="0" sParam="" />
Thus, your first type2 example should work.
Yes, the following macro correctly changes the Language to XML:
<Macro name="!!XML!!" Ctrl="no" Alt="no" Shift="no" Key="0"> <Action type="2" message="0" wParam="46006" lParam="0" sParam="" /> </Macro>
I do not know why it is not for you: maybe something you are doing after it in your macro is overriding that choice, or maybe you have a bug in your macro so it is not working at all.
My debug recommendation is to start by trying my short
!!XML!!
macro, and verify it works (it should, since it does for me); then start adding your additional commands (before and after) until it stops doing what you expect -
@PeterJones said in Marco's do not record and execute shortcuts.:
type2 are for menu commands (…) the following macro correctly changes the Language to XML:
<Macro name="!!XML!!" Ctrl="no" Alt="no" Shift="no" Key="0"> <Action type="2" message="0" wParam="46006" lParam="0" sParam="" /> </Macro>
Hmm, I can see the part in the Macros page that mentions
type=2
messages, and that excerpt links to the menuCmdID.h source file.IDM_LANG
is defined on line 474, where the base number appears to be 6000, not 4600 or 46000.How did you get 46006 for the XML language?Edit: I now see that line 474 has
IDM+6000
, not simply6000
.IDM
is on line 20, defined as40000
-
@mathlete2 said in Marco's do not record and execute shortcuts.:
How did you get 46006 for the XML language?
plus
plus
= ?
Or, maybe more accurately and easier to understand:
IDM = 40000 IDM_LANG = (IDM + 6000) IDM_LANG_XML = (IDM_LANG + 6) = 46006
-
In the menuCmdID source file, there are multiple comments of the form:
// IMPORTANT: If list above/below is modified, you have to change the value of...
Is it possible for
IDM_LANG
to be defined differently in another setup?Also, when UDLs are created, do they get their own ID too? If so, where can users find this ID number?
-
@mathlete2 said in Marco's do not record and execute shortcuts.:
In the menuCmdID source file, there are multiple comments of the form:
// IMPORTANT: If list above/below is modified, you have to change the value of...
Is it possible for
IDM_LANG
to be defined differently in another setup?If you recompile Notepad++. But stuff like that, Don doesn’t tend to change (or allow others to) – he set it up originally with plenty of space for each group, and I haven’t noticed any changes to those main “header” constants.
However, the “If list above/below” warnings are put before certain “end of list” constants, like the
IDM_FILEMENU_LASTONE
– the point of that comment is to developers/contributors: if you add a new command to the File menu, you have to change the definition of IDM_FILEMENU_LASTONE, because obviously the last one has changed. Usually those comments apply to a very specific pair of X,Y in “if X changed then you have to change the value of Y” – it doesn’t mean “everything below this comment must be changed”, it means “the specific line or two immediately following this comment must be changed”.Also, some are “beginning of sub-list” constants, which may change, but not very often. For an example of such a change, see my final paragraph in this post, about IDM_LANG_USER.
Also, when UDLs are created, do they get their own ID too? If so, where can users find this ID number?
UDLs get dynamically-assigned numbers between 46180 and 46210; the order depends on what order Notepad++ processes the UDL languages in – so it should remain consistent from one run to the next, as long as you don’t change the number of UDL or name of UDL or filename of UDL (the latter two might change the order that they get processed in). And you can use NppUISpy! plugin to read the menu command IDs for those, just like you can for plugin menu actions:
And, as an example of the “beginning of sub-list” style changes, the offset for IDM_LANG_USER changed from 80 to 180 about 8 years ago, when Notepad++ added around 20 new languages – he shifted it by 100, to make sure there was room for future expansion, so that wouldn’t have to change again; in the intervening years it’s up to about 90 builtin languages, so I estimate it would take another 20 years before the number of langauges gets close to the current limit. That constant changing is not something you should worry about anytime soon.
-
@PeterJones
It works now.I started, as you suggested, with this macro and that worked just fine.
<Macro name="!!XML!!" Ctrl="no" Alt="no" Shift="no" Key="0"> <!-- TEST --> <Action type="2" message="0" wParam="46006" lParam="0" sParam="" /> <!-- Set Language to XML --> </Macro>
So…then I played around with the position of the XML-Language setting and it turns out it works when done in the first step.
<Macro name="BASE64 Decode XML" Ctrl="yes" Alt="yes" Shift="yes" Key="106"> <!-- Ctrl/Shift/Num* --> <Action type="2" message="0" wParam="46006" lParam="0" sParam="" /> <!-- Set Language to XML --> <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="22129" lParam="0" sParam="" /> <!-- Base64 Decode --> <Action type="2" message="0" wParam="22217" lParam="0" sParam="" /> <!-- Xml Pretty Print with Attribute Indent --> <Action type="0" message="2013" wParam="0" lParam="0" sParam="" /> <!-- Select All --> <Action type="0" message="2178" wParam="0" lParam="0" sParam="" /> <!-- Copy --> </Macro>
Yes, I’m happy to tell you that this macro does exactly what I want.
Thank you again for you very helpful assistance. -
@Alan-Kilborn
@mathlete2 said in Marco’s do not record and execute shortcuts.:How did you get 46006 for the XML language?
For me it was simply useing the NppUISpy plugin.
-
By the way.
I have noticed that the numbers that can be used in the shortcut keys are not totally equal to the ‘normal’ keycodes and that not all keycodes can be used.
So…I have created a list with keycodes that are valid and I mention the actual keys they represent.
The list is just for the ‘key’ attribute and they can off cause (duh) be combined with Ctrl, Alt, Shift.If a number is not mentioned, that that means that Notepad++ registers this number als “Unlisted” and will not use it.
I have tested the numbers 0 to 255.I hope this list is useful.
Any corrections and/or additions are welcome.Key="8" ==> Backspace Key="9" ==> TAB Key="13" ==> ENTER Key="27" ==> ESC Key="32" ==> Spacebar Key="33" ==> Page up Key="34" ==> Page Down Key="35" ==> End Key="36" ==> Home Key="37" ==> Left Key="38" ==> Up Key="39" ==> Right Key="55" ==> INS Key="46" ==> DEL Key="48" ==> 0 Key="49" ==> 1 Key="50" ==> 2 Key="51" ==> 3 Key="52" ==> 4 Key="53" ==> 5 Key="54" ==> 6 Key="55" ==> 7 Key="56" ==> 8 Key="57" ==> 0 Key="65" ==> A Key="66" ==> B Key="67" ==> C Key="68" ==> D Key="69" ==> E Key="60" ==> F Key="71" ==> G Key="72" ==> H Key="73" ==> I Key="74" ==> J Key="75" ==> K Key="76" ==> L Key="77" ==> M Key="78" ==> N Key="79" ==> O Key="80" ==> P Key="81" ==> Q Key="82" ==> R Key="83" ==> S Key="84" ==> T Key="85" ==> U Key="86" ==> V Key="87" ==> W Key="88" ==> X Key="89" ==> Y Key="90" ==> Z Key="96" ==> Numpad 0 Key="97" ==> Numpad 1 Key="98" ==> Numpad 2 Key="99" ==> Numpad 3 Key="100" ==> Numpad 4 Key="101" ==> Numpad 5 Key="102" ==> Numpad 6 Key="103" ==> Numpad 7 Key="104" ==> Numpad 8 Key="105" ==> Numpad 9 Key="106" ==> Numpad * Key="107" ==> Numpad + Key="109" ==> Numpad - Key="110" ==> Numpad . Key="111" ==> Numpad / Key="112" ==> F1 Key="113" ==> F2 Key="114" ==> F3 Key="115" ==> F4 Key="116" ==> F5 Key="117" ==> F6 Key="118" ==> F7 Key="119" ==> F8 Key="120" ==> F9 Key="121" ==> F10 Key="122" ==> F11 Key="123" ==> F12 Key="186" ==> + Key="187" ==> = Key="188" ==> , Key="189" ==> - Key="190" ==> + Key="191" ==> / Key="192" ==> ~ Key="219" ==> [ Key="222" ==> ' Key="226" ==> <
-
Hello, @rens-duijsens and All,
-
Apparently, you forgot the
Key "40" ==> Down
, in your list -
The key INS is
Key 45
( and not 55 ) -
Some
OEM
keys seem missing too !
Refer this post and the next posts, for further information :
Best Regards,
guy038
-