Page 1 of 1

Run an external program on a selection?

Posted: Wed Dec 17, 2003 12:38 am
by Derek K.
It's great that I can run an external program on a saved file and capture the output, but it would be even better if I could run a command on a selection instead of the whole file.

I imagine that behind the scenes TextPad could copy the selected text, place it in a new (temporary) document, save the document, run the program on it, capture the output, delete the temporary file, and paste the captured output over my original selection.

Any way to achieve this effect without performing the steps manually?

Posted: Wed Dec 17, 2003 12:52 am
by Bob Hansen
:idea: Have you considered a macro that would be run after you have made the selection?

Posted: Wed Dec 17, 2003 1:16 am
by Derek K.
Bob Hansen wrote::idea: Have you considered a macro that would be run after you have made the selection?
How would this macro invoke an external program on my selection?

I already have a command-line program that does what I want, and it's much too complicated to express as a macro, so I can't convert my program to a normal macro if that is what you are suggesting. :(

Posted: Wed Dec 17, 2003 2:10 am
by Bob Hansen
I imagine that behind the scenes TextPad could copy the selected text, place it in a new (temporary) document, save the document, run the program on it, capture the output, delete the temporary file, and paste the captured output over my original selection.
I guess I was thinking of something like this:

Macro contents:
1. Highlight selection in Doc1, and copy to clipboard. Do not close.
2. Open new Doc2
3. Paste from clipboard
4. Save Doc2 (not really necessary, will be deleted anyway), do not close.
5. Make sure Doc2 is active document and run tool on Doc2 passing filename as parameter.
6. Capture the output in Output Capture Doc.
7. Select all in Output Capture Doc, and copy to clipboard.
8. Go back to Doc1 and paste from clipboard, replacing section which is still selected.
9. Save modified Doc1.
10. Close Doc1.
11. Close/Delete Output Capture Doc.
12. Close/Delete Doc2.

This sounds to me exactly what you have described, you have defined the answer yourself. You can switch between multiple documents in TextPad when recording a macro. Try doing all macro recording with keystrokes, not with mouse.

Posted: Wed Dec 17, 2003 8:06 am
by MudGuard
Is the external program able to put its result on the clipboard?

THEN it would be quite easy.

You can run your tool, giving $Sel (or $SelLine or $SelWord) as a parameter. Once the tool has finished (i.e. put the result in the clipboard), you simply press Ctrl-V

I'm not sure whether you can call tools from a macro, but if that is the case, you simply record a macro which invokes your tool and then pastes the result.

Posted: Wed Dec 17, 2003 3:55 pm
by Bob Hansen
Thanks MudGuard for the reminder about $Sel. Shoulda known that. :oops:

More info is available in the Help section for "Tool Parameter Macros"

Have you found a solution??

Posted: Mon Dec 22, 2003 4:47 pm
by drefty
The $Sel parameter macro does not work like you might expect. It only works with the first line of a multi-line selection. This is an understandable limitation, because it is difficult to send a multiple-line blurb of text as an argument to a script or program. You need some way of delimiting that blurb of text as a complete stand-alone unit (not a series of multiple arguments).

I tried using an approach that automatically opens and closes a temp file, but this takes numerous cumbersome steps. The clipboard approach works, but your script must be able to get and set the clipboard contents. It is less work to simply use the clipboard to paste text into and out of another program or script and bypassing the 'tool' menu entirely.

What would make work go *much* faster is if there were a way to export the current selection to a temp file and back into the document with a single keystroke.

Posted: Mon Dec 22, 2003 10:24 pm
by Bob Hansen
What would make work go *much* faster is if there were a way to export the current selection to a temp file and back into the document with a single keystroke.
I suggested a macro earlier that I think does that. You can assign a hot key to the macro and be done with one keystroke.