简介
这个插件除了运行通常的Python基本之外,还可以用Python脚本来自动化Notepad++的一些操作,从而可以简化某些工作。
这方面的例子是很多的,如https://sourceforge.net/p/npppythonscript/discussion/1188886/thread/d632d91e/。
安装
直接在Notepad++上自动安装会提示dll方面的一些问题。
为此,可以直接下载插件的msi文件并安装。
或者先下载Python Script插件的zip文件(PythonScript_Full_1.0.8.0.zip),然后解压缩。解压缩后的目录结构和notepad++安装目录结构一致,因此把解压缩后的文件拷贝到notepad++对应目录下即可。
Full中有个chm文件,方便查阅。
runMenuCommand与menuCommand
如果用runMenuCommand(),则需要在安装Notepad++时,选择English:
notepad.runMenuCommand(“Encoding”, “Convert to ANSI”)
对于中文语言,即便填充中文的菜单名称,也可能有问题。详情参考本文一开始给出的链接。
对于Notepad++的标准菜单,推荐menuCommand()调用方式。其中参数的值,可以从PythonScript.chm中索引MENUCOMMAND找到。
notepad.menuCommand(MENUCOMMAND.FORMAT_CONV2_ANSI)
stdout
在plugins\PythonScript\scripts\startup.py的最后,定义了:
# This sets the stdout to be the currently active document, so print "hello world",
# will insert "hello world" at the current cursor position of the current document
sys.stdout = editor
即标准输出设置为编辑器,也就是Notepad++当前打开的文件窗口中;进一步地,就是把print出来的数据写到当前打开的文件上面。
为了避免非预期的写文件,可以将标准输出设置为控制台,如同startup.py一开始设置stderr一样,即改为:
# This sets the stdout to be the currently active document, so print "hello world",
# will insert "hello world" at the current cursor position of the current document
#sys.stdout = editor
sys.stdout = console
console
打开控制台窗口的方法:Plugins->Python Script->Show Console
补充一点,在没有打开console的时候,如果运行的脚本有语法错误,则试图运行时什么也看不到,很是莫名其妙。就在于错误输出在console,而console没打开,不可见。