Macro to clear RO and Format XML
When I open XML in Outlook - xml file is always read only. I have to clear read-only attribute and to use XML plugin to format it.
Is that possibly to assign such a combo to a single shortcut.
There are two main difficulties with your request:
- Plugin commands, like Format XML (aka Plugins > XML Tools > Pretty Print or similar in whatever plugin you are using), do not necessarily get a constant menu command ID, and macros must use those menu command IDs. Thus, the macro recorder does not record plugin commands.
- Which “read-only” attribute do you mean? There’s the Windows-style read-only attribute, which sets the flag in the filesystem, which you can clear with Edit > Clear Read-only Flag. And there’s the Notepad++ read-only status, which you can toggle with Edit > Set Read-only.
For the first, if you are sure that the menu command IDs won’t be changing (ie, you are not installing new plugins anymore), then if you can figure out what the menu command ID is, it should stay the same from run to run. For me, the Pretty Print command is ID=
22137, and that stays consistent from run-to-run of my current setup. But I will not guarantee it’s the same for you, or that it will remain consistent if you add or remove plugins. Once you have a command ID, you can manually edit ↗ the
%AppData%\Notepad++\shortcuts.xml🛈 for a normal installation)
For the second, if it’s the system-level read-only flag, then it’s easy, you just have to have the recorded value of the menu ID for the Clear Read-only Flag entry, which is ID=
42033, which you can get by recording the macro. If it’s the Notepad++ read-only flag (ID=
42028), then you cannot get the current state of that flag in a macro, and deciding whether or not to send that command is not possible (no logic in macros, either). If you’re willing to maybe have to run the macro twice, when it happens to not be read-only and you thought it was, then having that
42028in the macro is not a problem.
I was able to manually edit ↗ the macro to be:
<Macro name="TryRO" Ctrl="no" Alt="no" Shift="no" Key="0"> <Action type="2" message="0" wParam="42033" lParam="0" sParam="" /> <Action type="2" message="0" wParam="42028" lParam="0" sParam="" /> <Action type="2" message="0" wParam="22137" lParam="0" sParam="" /> </Macro>
If the file was both windows read-only and Notepad++ read-only, that macro works on the first run. If it was just Notepad++ read-only, it also works. If it happened to be not read-only at all, then the first run set it Notepad++ read-only, and running it a second time was able to make it writeable and then runs Pretty Print correctly.
Once the macro is working for you, you could use Macro > Modify Shortcut, go to
TryRO(or whatever you called the macro) and
If you don’t like all those Macro limitations, and if you have PythonScript plugin installed (or similar), or are willing to install such, you could use:
from Npp import * notepad.menuCommand(MENUCOMMAND.EDIT_CLEARREADONLY) # clears the Windows attribute if editor.getReadOnly(): notepad.menuCommand(MENUCOMMAND.EDIT_SETREADONLY) # toggles the Notepad++ menu, but only if it's needed notepad.runPluginCommand('XML Tools', 'Pretty Print') # runs the plugin command
Once that’s working for you, you can use the PythonScript interface to add it to the Run menu, then use Settings > Shortcut Mapper to assign a keyboard shortcut.
Other scripting plugins will behave similarly.
For me, the Pretty Print command is ID=
Sorry, I forgot, I was going to explain how I found that: I have the plugin NppUISpy installed; using its interface, I was able to find that ID number.
Note: many of the same points regarding plugin commands in macros were brought up in this discussion from Jun 2020.