PythonScript to process -pluginMessage="..." command-line option
-
Inspired by the request for running a S&R or Macro from the command line in “Running Notepad++ from a bat file”, I started working on a proof-of-concept script to at least find the bare minimum Script to extract the string from the message or initial command line
The following is a script for PythonScript 3 (uses Python 3 syntax), though I am sure it could be translated to PythonScript2 if needed.
pluginNotification26689.py
:# encoding=utf-8 """in response to https://community.notepad-plus-plus.org/topic/26689/ Example processing of NPPN_CMDLINEPLUGINMSG notification """ from Npp import notepad, console, NOTIFICATION import ctypes import sys def usePluginMessageString(s): console.write(f"TODO: do something more interesting with -pluginMessage=\"{s}\"\n") def getStringFromNotification(args): code = args['code'] if 'code' in args else None idFrom = args['idFrom'] if 'idFrom' in args else None hwndFrom = args['hwndFrom'] if 'hwndFrom' in args else None #console.write(f"notification(code:{code}, idFrom:{idFrom}, hwndFrom:{hwndFrom}) received\n") if idFrom is None: return s = ctypes.wstring_at(idFrom) #console.write(f"\tAdditional Info: str=\"{s}\"\n") usePluginMessageString(s) def getStringFromCommandLine(): for token in sys.argv: if len(token)>15 and token[0:15]=="-pluginMessage=": s = token[15:] #console.write(f"TODO: process {token} => \"{s}\"\n") usePluginMessageString(s) notepad.callback(getStringFromNotification, [NOTIFICATION.CMDLINEPLUGINMSG]) console.write("Registered getStringFromNotification callback for CMDLINEPLUGINMSG\n") getStringFromCommandLine()
This can be run ATSTARTUP in user
startup.py
using###################### # process -pluginMessage="" from the command-line import pluginNotification26689 ######################
when it’s first run, it checks the existing command-line (because Notepad++ does not forward the NPPN_CMDLINEPLUGINMSG to plugins if the initial run has the command-line argument, but only if the command-line is run while N++ is already open)
The one shortcoming I’ve seen is that N++ apparently only forwards the message if there’s a filename specified on the command line… if it’s just
notepad++ -pluginMessage="message string"
, the notification never gets sent. If someone else can confirm that, I might put in a feature request to have that message be sent even if there isn’t a filename specified. If the behavior is different for you, or you know another way to trigger thatmessagenotification if no filename is sent, let me know.