wzeller Posted November 13, 2009 Report Share Posted November 13, 2009 Is it possible to stream a variable into the standard input of an external program and have it's standard output streamed into another variable? (Instead of writing a file, using that file as the standard input, having stdout write to another file, and then reading that file back.) I often find myself copying text to the clipboard, launching Excel, pasting it at A1, highlighting the column, clicking the sort button, cutting back to the clipboard, closing Excel, then pasting someplace. This morning I was in the middle of it and thinking my usual thought: Using Excel to sort a short list of text is like swatting a fly with a Buick. Then it occurred to me that I can't be the only one, and surely somebody has written a utility to simply sort the clipboard contents. To my surprise, a quick Googling only brought up one program and that one, if running, will ALWAYS sort the clipboard every time you copy to it. That would be bad. Then I realized I've got the world's coolest macro program! Somebody surely has written a macro for this. So I came to the Macro Express site and looked through the samples and the "Shared Macros" and, (once again) to my surprise, found that there doesn't seem to be one. Some forum hunting turned up a post on using the Windows sort command to sort a file into another file based on the third field of the input file. That got me rolling, and actually much of my task was just to simplify the example provided there. The result is this attached (and nicely-working) macro: CopySorted.mexIt checks for the existence of previous work files and deletes them, copies selected text to the clipboard, copies the clipboard to an input file, calls sort.exe via cmd using the input file as stdin and sending stdout to another file, waits for the output file to be ready, copies it back into the clipboard, and deletes the two work files. To use, I just highlight some text and hit Ctl-Shft-A to Alphabetize, then paste it wherever I want it. (An earlier version ended with a Paste, replacing the selected text, but I realized you don't always want it pasted into the same place you copied it from, so I removed the Paste.) It works fine, but seems very kludgy. Having to write and read and delete files for something as simple as sorting a string that's already in memory is, again, like swatting a fly with a Buick. (Okay, compared to using Excel for it, it's probably more like swatting a fly with a Schwinn.) Doing it without files would speed it up a LOT. It would get rid of over half the code, not to mention get rid of disk access delays. Surely there's some way to stream a variable directly into the stdin of sort.exe and have the stdout streamed back to another variable. ...? I'm sure others would find this macro useful, so I plan to post it to the shared macros area (where it might be especially helpful for newbies like me), but before I do I'd like it to be as streamlined as possible. Any thoughts? Wayne Quote Link to comment Share on other sites More sharing options...
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.