Is it possible to run text through an external program?
-
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
-
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_saveSince 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 filesI 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.
-
@PeterJones Perfect… thanks very much!!
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