Python & Notepad++
PythonScript is a Notepad++ plugin that provides a Python interpreter to edit Notepad++ documents.
You install PythonScript in Notepad++ like this:
Use "New Script" to create a new Python script:
As an example, I will create a template substitution script, something that I use often. You provide a substitution template as input, and then each line of the open document is substituted according to the given template.
First we create the script substitute.py:
This is the template substitution script I developed:
def Substitute(contents, lineNumber, totalLines):
contents = contents.rstrip('\n\r')
if contents != '':
editor.replaceLine(lineNumber, template.replace(token, contents))
token = notepad.prompt('Provide a token', 'Substitute token', '%%')
template = notepad.prompt('Provide a template', 'Substitute template', '')
if token != None and template != None:
editor.forEachLine(Substitute)
You can paste it into Notepad++:
I will now demonstrate the script on a new document I created in Notepad++: the list of today's top 10 scanning IP addresses:
For each IP address, I want to generate a command that I will then execute.
The script can now be invoked to be executed on this open document like this:
The first line of Python script substitute.py to be executed, is line 6 (token = notepad.prompt...). It prompts the user for a token string (default %%), this is a string that, when used in the template string, will be replaced by each line in the open document
Line 7 prompts the user for a template string:
When the user has not cancelled answering the prompts (tested in line 8), line 9 (editor.forEachLine(Substitute)) is executed: it runs function Substitute on each line of the document:
Then I can copy/paste all these generated commands into a cmd.exe console:
This example is a bit contrived, as you could also use a for loop in the scripting shell to achieve the same result.
I also use this Python script often when I'm programming. Say that I want to hardcode this list of scanning IP addresses in a Python script. I can quickly create a Python list as follows:
And then I add the variable assignment statemnt and create a list:
Didier Stevens
Senior handler
blog.DidierStevens.com
Comments