Community
    • Login

    Is it possible to run text through an external program?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 2 Posters 3.0k Views
    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.
    • bcosellB
      bcosell
      last edited by

      I guess this is a bit of how TextFX works and I suppose I’m asking “can I write my own TextFX-like filters” and have n++ send the selected text through the program and replace the text with its output

      1 Reply Last reply Reply Quote 1
      • PeterJonesP
        PeterJones
        last edited by

        The literal answer to your question is “yes.” I am assuming, however, that you’d also like to know how. :-)

        Using the NppExec plugin, you can grab the selected text and pass it through another program, and then grab the results. Here is one where I had NppExec grab the entire contents of the given window, save it to a temporary file, pass it through gpg to decrypt it, and paste the contents back into that same NPP window.

        cls
        npp_save
        cmd.exe /c exit %RANDOM%														// cannot access $(SYS.RANDOM) directly through NppExec, but can tell cmd.exe to return it as a value
        set tempfile = $(SYS.TEMP)\NppGpgFile_$(EXITCODE).tmp							// create random tempfile name
        set ascfile = $(SYS.TEMP)\NppGpgFile_$(EXITCODE).asc							// create associated ascfile name
        sci_sendmsg SCI_SELECTALL														// select all
        sel_saveto $(ascfile) :a														// save selection to the ascfile	(use :a to save as ansi, to prevent unicode prefix ÿþ getting embedded)
        gpg.exe --output "$(tempfile)" --decrypt "$(ascfile)"							// decrypt
        sel_loadfrom $(tempfile)														// replace selection with results
        sci_sendmsg SCI_DOCUMENTSTART													// deselect
        rm -rf "$(tempfile)" "$(ascfile)"												// cleanup temp files
        npp_save
        

        Since you want to make use of what you’ve already selected, you wouldn’t need (for eample) the sci_sendmsg SCI_SELECTALL, because you would want to leave the selection alone. So, based on your description that it’s already selected, here’s something that should give you a starting place:

        cmd.exe /c exit %RANDOM%														// cannot access $(SYS.RANDOM) directly through NppExec, but can tell cmd.exe to return it as a value
        set destfile = $(SYS.TEMP)\NppFilterDestinatino_$(EXITCODE).tmp					// create random destfile name
        set sourcefile = $(SYS.TEMP)\NppFilterSource_$(EXITCODE).tmp					// create associated sourcefile name
        sel_saveto "$(sourcefile)" :a													// save selection to the sourcefile	(use :a to save as ansi, to prevent unicode prefix ÿþ getting embedded)
        c:\path\to\program.exe --output "$(destfile)" --source "$(sourcefile)"			// this is the program you're filtering it through; this will work if your program can take input and output filenames at the command prompt
        // however, if your program only uses STDIN and STDOUT, then do something like:
        // type "$(sourcefile)" | c:\path\to\program.exe > "$(destfile)"				// this uses windows pipes and redirection so your program.exe takes STDIN and outputs to STDOUT, but NppExec can interface with the files
        sel_loadfrom "$(destfile)"														// replace selection with results
        rm -rf "$(destfile)" "$(sourcefile)"											// cleanup temp files
        

        I left in the random file naming for the tempfiles, because it should keep collisions from occurring. I included options for either command-line arguments to your program.exe, or using windows type, along with an input pipe and output redirection, so that your program could use STDIN->STDOUT.

        NppExec comes with pretty thorough documentation, but if you have any questions about this outline, feel free to ask.

        bcosellB 1 Reply Last reply Reply Quote 4
        • bcosellB
          bcosell @PeterJones
          last edited by

          @PeterJones Perfect… thanks very much!!

          1 Reply Last reply Reply Quote 1
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors