Community
    • Login

    PythonScript to process -pluginMessage="..." command-line option

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    4 Posts 2 Posters 1.2k Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • PeterJonesP Offline
      PeterJones
      last edited by PeterJones

      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 that message notification if no filename is sent, let me know.

      Alan KilbornA 1 Reply Last reply Reply Quote 4
      • Alan KilbornA Offline
        Alan Kilborn @PeterJones
        last edited by

        @PeterJones said:

        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.

        Isn’t this because, without a file on the command line, there’s nothing for Notepad++ itself to do when such a thing happens? (Well, OK, it could (and does) activate Notepad++ in such a situation.)


        If someone else can confirm that

        I can.


        I might put in a feature request to have that message be sent even if there isn’t a filename specified.

        I’d encourage this.

        PeterJonesP 1 Reply Last reply Reply Quote 0
        • PeterJonesP Offline
          PeterJones @Alan Kilborn
          last edited by

          @Alan-Kilborn said in PythonScript to process -pluginMessage="..." command-line option:

          I’d encourage this.

          Issue #16273

          1 Reply Last reply Reply Quote 2
          • Alan KilbornA Offline
            Alan Kilborn
            last edited by Alan Kilborn

            It appears that the “shortcoming” has been FIXED and should appear in the next Notepad++ release.
            Nice.

            1 Reply Last reply Reply Quote 2

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors