Run MIME tools decoder on multiple files
-
I want to decode URIs that appear on hundreds of files.
I learned that the MIME tools plugin can do it
(Here: https://stackoverflow.com/questions/26147645/how-to-url-encode-decode-selected-text-in-notepad )But it seems I need to to focus on each file, select all text, then run it. Can you teach me how to really do it?
-
decode URIs that appear on hundreds of files.
Most of the plugins, including MIME Tools, are written to act on one file (the active window). The Find In Files dialog is about the only place that’s easy to do multi-file operations. In general, Text Editors focus on the editing of a single file.
That said, it would be possible to code up a script to do it: there are tools like the plugins PythonScript or LuaScript, or my Perl module(*) which allows external control of Notepad++, all of which could use the
runPluginCommand
or equivalent operation to call the MIME Tools > URL Decode menu entry. The script could easily toggle through all the open files in Notepad++, or even do the file matching itself, and then individually open each file in Notepad++, do the fixes, and then save/close the file (to prevent having hundreds of files open simultaneously).But at that point, any of those languages (Python, Lua, Perl) have core or standard modules/libraries which can do the URL-decoding internally, without having to pester Notepad++ itself to run a plugin to do that, so it could be written in a Notepad++ -agnostic manner, without having to customize the logic and commands to use Notepad++ at all. It’s really more of a programming exercise than something specific to Notepad++. And rather than re-inventing the wheel, this StackExchange question has answers showing how to perform your task on a given file in python2, python3, sed, bash+xxd, php, perl, and awk – the question was asked from the Linux perspective, but all of those could be done in Windows as well.
We’re not actually a code-writing service, and as I said above, this is really a coding problem, not a Notepad++ -specific question. However, since it can be done “inside” Notepad++ to some extent, if I have another chunk of time a little later today, I might come back and try to do the PerlScript(*) and/or PythonScript solution
*: Footnote = The Perl module has a perlish name, officially (Win32::Mechanize::NotepadPlusPlus); but has been given the PerlScript nickname to give it balance to the PythonScript and LuaScript plugins. I am more fluent in Perl than Python; unfortunately, until I am able to bundle everything into a plugin package, it’s probably easier for standard Notepad++ users (ie, those who don’t already have Perl installed on their Windows machines) to just install PythonScript plugin to do it.
-
@PeterJones said in Run MIME tools decoder on multiple files:
if I have another chunk of time a little later today, I might come back and try
Unfortunately (for me), trying the PerlScript version found a bug or two in the runPluginsCommand implementation, which will take time to debug and fix.
Fortunately (for you), I switched to PythonScript, partially to give you a solution, but honestly mostly to verify that it was truly a bug in PerlScript, not in the automation messages for running plugin commands.
- Install PythonScript plugin
- Plugins > Python Script > New Script: save as
19148-url-decode-all-files.py
- Populate with the contents below
- Run Plugins > Python Script > Scripts > 19148-url-decode-all-files
It will cycle through all files open in Notepad++, select All, run the URL Decode, then put the cursor back where it was. In the end, it will activate the same buffer that was originally active. Any files that were changed will be in a “changed state”, and you will have to individually check each file to make sure it’s met your standards, and then Save it. (if you are brave, you can Save All, but see “caveat emptor”, below.
# encoding=utf-8 """in response to https://notepad-plus-plus.org/community/topic/19148/ """ from Npp import * from time import sleep bufferID = notepad.getCurrentBufferID() for t in notepad.getFiles(): notepad.activateIndex(t[3], t[2]) sleep(0.5) editor.beginUndoAction() keep_pos = editor.getCurrentPos() editor.selectAll() sleep(0.5) notepad.runPluginCommand('MIME Tools', 'URL Decode') sleep(1) editor.setEmptySelection(keep_pos) editor.endUndoAction() sleep(0.5) notepad.activateBufferID(bufferID) """ https://example/search?encode%3Dblah!!!! https://example/search?encode%3Dblah!!!! https://example/search?encode%3Dblah!!!! other stuff "%55" hi https://example/search?encode%3Dblah%20%21%22%23 https://example/search?encode%3Dblah%20%21%22%23+ABC ## _caveat emptor_ ## This script seemed to work for me, based on my understanding of your issue. I make no guarantees or warranties as to the functionality for you. You are responsible to save and backup all data before and after running this script. """
caveat emptor
This script seemed to work for me, based on my understanding of your issue. I make no guarantees or warranties as to the functionality for you. You are responsible to save and backup all data before and after running this script.
-
Curious about the
sleep
function calls.
Did you find these necessary?
I’ve done a lot of P.S. and the only time I’ve used such calls is when I have a need to do something in a separate thread (which is a really rare thing). -
@Alan-Kilborn said in Run MIME tools decoder on multiple files:
Curious about the
sleep
function calls.
Did you find these necessary?No. They were a holdover from my debugging (I like to be able to see what it’s doing as it goes through each step of the process)
So @trespda , it’s safe to comment out or delete those
sleep()
commands. -
@PeterJones
I definitely didn’t expect fresh code as an answer. Thanks a lot!I spent the rest of yesterday trying to find a solution independent of npp, even in the form of crude character-to-character regex replace. But not knowing the basics of any language, I got nowhere. Today I will test your script.
I was surprised that npp isn’t built to pass commands to all files, indeed because of the priceless find dialog.
-
@PeterJones My test against a few dozen open files went well. I still got problems with certain characters, but the script run fine. Thanks again.