Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write macro for Notepad++?

I would like to write a macro for Notepad++ which should replace char1, char2, char3 with char4, char5, char6, respectively.

like image 661
Igor Drincic Avatar asked Dec 12 '08 10:12

Igor Drincic


People also ask

How do I create a macro in WordPad?

Now bring WordPad to the foreground (click on it in the taskbar). Press CTRL+SHIFT+T to see the address typed into WordPad. We will first change the CTRL+SHIFT+T macro to become a Program Specific macro.

How can I see the macro code in Notepad ++?

Go to %appdata%\Notepad++ folder. The macro definitions are held in shortcuts. xml inside the <Macros> tag. You can copy the whole file, or copy the tag and paste it into shortcuts.


1 Answers

Macros in Notepad++ are just a bunch of encoded operations: you start recording, operate on the buffer, perhaps activating menus, stop recording then play the macro.
After investigation, I found out they are saved in the file shortcuts.xml in the Macros section. For example, I have there:

<Macro name="Trim Trailing and save" Ctrl="no" Alt="yes" Shift="yes" Key="83">     <Action type="1" message="2170" wParam="0" lParam="0" sParam=" " />     <Action type="1" message="2170" wParam="0" lParam="0" sParam=" " />     <Action type="1" message="2170" wParam="0" lParam="0" sParam=" " />     <Action type="0" message="2327" wParam="0" lParam="0" sParam="" />     <Action type="0" message="2327" wParam="0" lParam="0" sParam="" />     <Action type="2" message="0" wParam="42024" lParam="0" sParam="" />     <Action type="2" message="0" wParam="41006" lParam="0" sParam="" /> </Macro> 

I haven't looked at the source, but from the look, I would say we have messages sent to Scintilla (the editing component, perhaps type 0 and 1), and to Notepad++ itself (probably activating menu items).
I don't think it will record actions in dialogs (like search/replace).

Looking at Scintilla.iface file, we can see that 2170 is the code of ReplaceSel (ie. insert string is nothing is selected), 2327 is Tab command, and Resource Hacker (just have it handy...) shows that 42024 is "Trim Trailing Space" menu item and 41006 is "Save".
I guess action type 0 is for Scintilla commands with numerical params, type 1 is for commands with string parameter, 2 is for Notepad++ commands.

Problem: Scintilla doesn't have a "Replace all" command: it is the task of the client to do the iteration, with or without confirmation, etc.
Another problem: it seems type 1 action is limited to 1 char (I edited manually, when exiting N++ it was truncated).
I tried some tricks, but I fear such task is beyond the macro capabilities.

Maybe that's where SciTE with its Lua scripting ability (or Programmer's Notepad which seems to be scriptable with Python) has an edge... :-)

[EDIT] Looks like I got the above macro from this thread or a similar place... :-) I guess the first lines are unnecessary (side effect or recording) but they were good examples of macro code anyway.

like image 147
PhiLho Avatar answered Sep 30 '22 12:09

PhiLho