Jump to content
Macro Express Forums

Sorting the clipboard


Recommended Posts

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

Link to comment
Share on other sites

Yeah, use arrays.

...................................................

You probably saw my convo about sorting and after trying a few different things I now exclusively use arrays and sort them to avoid creating files and such. And in fact I’ve had a rainy day task for some time to create a macro that does exactly what you say.

 

BTW you might be interested in a utility called ClipMate. It has the sort capability.

 

How I plan on making my clipboard sorter:

  1. User copies to clipboard
  2. Copy clipboard to variable
  3. Split variable into an array on end of line (CRLF (Carrige Return – Line Feed)
  4. Move array one element at a time to effect an insert sort
  5. Generate new output variable with each element delimited with CRLF
  6. Copy output variable to clipboard
  7. User pastes where desired

 

The sort routine is actually pretty simple. The master loop is to plow thru every element in the array generated in step 3. Within that loop you plow thru every element of the result array. The first time one obviously sticks the value in element 1. Subsequent iterations you start at 1 and increment thru each time comparing it your value is greater than or equal to the current one being evaluated. If one is found move all the elements higher up in the array up one to make room and insert this element at the current position plus 1. And just chew thru it until done. It actually works surprisingly fast. Hmmm... Sure would be fun to do this one quick. Is it raining out yet?

Link to comment
Share on other sites

I'd thought about using a bubble sort like that, but I haven't actually written a real bubble sort since I was in highschool. It's amazing how quickly you become dependent on functions like sort() when they're built right into the languages you use. I sort arrays all the time - without actually having written a function for it in over 20 years (yeah, highschool was a long time ago). When I thought about doing it this morning I quickly discarded the idea because I wasn't confident I'd even remember how it goes, much less how to make it work in Macro Express scripting. (Back then, I think I wrote it in Turbo Pascal.)

 

Maybe I discarded it too quickly. It's always good for the brain to go back and study the fundamentals.

 

If you decide today is rainy enough for you, then I'll gladly and happily skip the work because I'm not sure it's raining quite that hard here (especially since I already put together something that works). But if I do get a chance to go at it some time soon I'll be sure to post my results here.

 

(Actually, I'm looking forward to the challenge now. If only I could justify shoving the rest of my work aside for an hour or two.)

 

wayne

 

(Oh, and yes: It was your post about sorting that got the ball rolling. So thanks!)

Link to comment
Share on other sites

I’ll take a crack at it today.

...........................................

As soon as I get 3 tasks completed I can spare some time. And I already have the routine in use in other places so it makes more sense if I do it. Also I wanted to try a tweak on my routine that should make it twice as fast.

 

Ha! The last language I learned was Pascal. It wasn’t even turbo’d yet. Was running on a Digital VAX VMS 8600. Funny I can still remember the name to this day.

 

Interesting you call it a bubble sort too. I think Paul called it that as well. But I think it’s an insert sort. As I recall a bubble sort went thru each pair and simple swapped their position if not in order. It would then do that iteratively until it got a clean pass. They were really bad because if one had a large value at he beginning it would take nearly the number of elements in iterations to move ti to the end. However the downside to an insert sort is that you need two arrays. I always wanted to do some sort of binary sort but I could never get my head around how to do it. And I think it might be complex in MEP.

Link to comment
Share on other sites

That's awesome.

 

After I posted my reply I thought to myself, "Wait, that's not really a bubble sort." I went to Wikipedia and looked up a few things and decided you were talking about an insert sort. :)

 

One listed there that seemed more efficient and, actually, simpler was a "select sort" where you take the minimum value in the array and move it to the first position, then the minimum value in the array other than the first position and move THAT to the first position, then minimum value other than in the first two positions and move THAT to first... blah blah blah. But there's no function in Macro Express to return the key/value of the minimum value. Building a function to do that is an option - but by the time that function runs, a bubble or insert sort would have been done already.

 

It's unfortunate that the scripting language for Macro Express doesn't have more in-built array functions. (Obviously, a sort function would be handy but that's not what I'm thinking of.) It would be nice to have functions that return the key/value of the maximum and minimum values stored in the array, a function to insert a value (reassigning keys of values subsequent to the insertion point), a function to remove a value with the same reassignment of keys for subsequent values, and a combination of those last two that lets you move a value within the array (read, remove, insert).

 

With just those functions, array handling in Macro Express would instantly become far more powerful.

Link to comment
Share on other sites

We might be pushing too hard.

............................................

May I suggest you use the reply button in the message you are responding to instead of the “Add Reply” button at the bottom. Some of us use Outline view and it helps keep things organized in threads.

 

I considered a couple different sort methods and like you fund that we would have to build functions to support many of them which negated them. Also the insert sort is easy to conceptualize and unless the list gets really big it works OK. However I have considered and we have talked about using a binary search on the results array but I haven’t written or had need for one yet.

 

I encourage you to submit feature requests to ISS. In fact I have already requested a sort feature. MEP is built with Delphi and a lot of the functions we see are just passing thru to programmatic routines and such so if it exists in Delphi it might be fairly easy for them to give us access to some more. However I really do think we’re asking MEP to do too much. It’s designed to automate simple tasks for non-programmer users with some programming capability. When it gets industrial strength some of my things really should be written in a compiled program. Something I’m slowly learning to do.

Link to comment
Share on other sites

May I suggest you use the reply button in the message you are responding to instead of the “Add Reply” button at the bottom.

 

Oops. Didn't know there was a difference. UI-design fail. So noted for future. :)

 

I certainly agree that MEP is best suited for the simple stuff, but with just a few array functions it would be able to do a lot more of the things people might imagine. Sorting is certainly a good example. Sorting functions are so ubiquitous these days that even people new to the web know that they can often click on column headers to sort tables. And the other array functions I mention are in many ways just subfunctions of sorting.

 

But I'll follow your cue and submit a feature request for a simple array-sorting function. I can't imagine that it doesn't exist in Delphi, so hopefully you're right that it would be a simple add.

 

w

Link to comment
Share on other sites

Another crack.

...........................................

I wrote a macro this morning that logged files with a sort feature that is exactly what we need for the clipboard copy. I plan on making it a sample macro on my website for all to see how I sort an array. I had tried a different insert position but i was an idiot and it took me awhile to figure out why it wasn't working properly.

 

I think I'll add a prompt to ask the user if they would like to eliminate duplicate as well.

 

You would like this one. I wrote a list disorganizer macro awhile back for a client. It takes a column of unsorted cells from Excel and a block of cells from Excel and disorganizes them to match the original list and add the new ones on the end. Evidently they have some spreadsheets which can not be resorted but they receive new data that is and this solves the problem. Seems perverse to write a macro to go that way.

Link to comment
Share on other sites

  • 7 months later...

I've implemented 3 sorting algorithms for Macro Express Pro - Bubble Sort, Gnome Sort, and Insertion Sort.

 

You can search the forum for those terms. However, their performance is poor if you need to sort more than 200 items. I believe it's a limitation of MEP and not my coding ;-)

 

BTW, your use of Windows' sort.exe already works very well. It is faster than my code by many orders of magnitude. Anyway, I only wrote the scripts to improve my understanding of sorting algorithms.

 

Using sort.exe may not be "elegant" but it certainly works and it is fast. Plus, sort.exe is installed with every copy of Windows (I believe it dates back to DOS days!).

 

-Lemming

 

I'd thought about using a bubble sort like that, but I haven't actually written a real bubble sort since I was in highschool. It's amazing how quickly you become dependent on functions like sort() when they're built right into the languages you use. I sort arrays all the time - without actually having written a function for it in over 20 years (yeah, highschool was a long time ago). When I thought about doing it this morning I quickly discarded the idea because I wasn't confident I'd even remember how it goes, much less how to make it work in Macro Express scripting. (Back then, I think I wrote it in Turbo Pascal.)

 

Maybe I discarded it too quickly. It's always good for the brain to go back and study the fundamentals.

 

If you decide today is rainy enough for you, then I'll gladly and happily skip the work because I'm not sure it's raining quite that hard here (especially since I already put together something that works). But if I do get a chance to go at it some time soon I'll be sure to post my results here.

 

(Actually, I'm looking forward to the challenge now. If only I could justify shoving the rest of my work aside for an hour or two.)

 

wayne

 

(Oh, and yes: It was your post about sorting that got the ball rolling. So thanks!)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...