Notepadpp的Python Script插件

简介

这个插件除了运行通常的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没打开,不可见。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值