Scripting Windows Apps
Stevey's Drunken Blog Rants™
I've played around occasionally with doing win32/ole automation on my Windows system using Ruby, and I'm starting to see lots of potential uses for it.
As one example, a few months back I had 30-odd MS-Word documents that were all in more or less the same format, and I wanted to convert them to a particular XML format. Just yucky data-munging. The XML-export options for Word all resulted in really gross XML output. I could have written a complicated XSLT transfomer, but I really just wanted to do it with a script.
I wrote a quick Ruby script that did the job. Extracting the information from the Word document was by far the most trivial part of it; the rest was just XML munging using Ruby's built-in XML support (REXML).
I could have done this in Perl with the Perl/Win32 extensions, but I'm disliking Perl more and more these days; it's worth another blog entry at some point as to why. Suffice it to say that Ruby makes life a lot easier, even if you have far less experience with Ruby than with Perl.
You can also do Win32 scripting with Visual Basic, but unless it's improved a lot since I last looked at it, it's not a very powerful language, comparatively speaking.
You can do Win32 scripting with Python, and I even had it set up at one point, although the installation was nontrivial. (Ruby just comes with the win32 support as part of the standard distribution, so there simply was no installation.) But for the most part, Ruby does things more cleanly and consistently than Python, and it has all of the nice Perl features that make Perl (in some ways) such a joy to use, e.g. Perl's built-in regex syntax, and string interpolation.
So when it comes down to it, Ruby's just my preference for scripting.
Just for kickers, last night I wrote a script that will fetch the subscribers of any two Amazon email lists, and show you who's only on one of the two lists, or who's on both lists.
Because of the Isaac authentication requirement, using a straight http request is nontrivial. You can do it in various ways, but none of them seemed like they'd be as easy as scripting Internet Exploder to get the pages for me, since I already authenticate in IE every morning.
It turns out to be surprisingly fast - there's essentially no latency from Ruby or IE; the latency is all in fetching the list members on the server side (once a list has been fetched and cached, the script runs in about a second.)
To use this technique, you just need five lines of code:
# required for win32 support
And now you have the HTML for the page in the "html" variable, so you can parse it or munge it with regexes or whatever.
The polling line is a bit silly, but works. If you hate it, there's an option to give your script with a Windows event queue and register for event-based notifications from IE. It didn't look very hard either, but in this case seemed like overkill.
Here's my quick-and-dirty Ruby script that gives you the diffs between two Amazon mailing lists, or with the "-s" option, shows you who's subscribed to both of the lists:
(confidential info deleted)
Pretty useful. And the script is tiny, even with error handling, usage printing, comments, etc. So it's easy to modify.
Here's an even simpler one called "whosin" that just shows you who's on the specified mailing list, so you don't have to fire up a browser window and wait for the page to load and render.
The 'whosin' script:
Notice that I've got a fair amount of copy & paste going on between this and 'email-diff'. Fair 'nuff. Following the Law of Threes, if I wind up doing one more of these scripts, I'll throw the common email-list-lookup stuff into a module.
Modules are as easy to make and use in Ruby as they are in Perl -- easier, in fact. And they're more powerful, if you care to take advantage of Ruby's mixin facility, which is a bit like multiple inheritance without most of the headaches. And you don't need to put that stupid "1;" that Perl requires at the end. Good Ole Perl. Sigh.
Anyway, now I have this little cheesy framework for doing all sorts of intranet-scraping tools. If I want a command-line Amazon phone tool on Windows, it's a 30-minute scripting job (if that). If I absolutely had to have a 'daily diff'... well... Anyway, there are lots of potential uses for intranet scraping.
I haven't really tried yet, but I can think of two potential uses for scripting on Windows. Good ones, that is.
The first is writing a script, or set of scripts, that sets up a Windows box from scratch, configuring it to my liking. Whenever I get a new Windows system, I spend a LOT of time configuring it, usually a full day. I install the Microsoft PowerTools TweakUI stuff. I change registry entries to swap the Caps-lock and Control keys on my keyboard (a must for Emacs power users). I change the backgrounds, and the color schemes, and configure the taskbar, and start menu. I download and install the same old software, and then I configure THAT, too.
It's no wonder I only upgrade my Windows systems once every year or two, if I can help it.
However, I could automate the majority of that with a Ruby/Win32 script. All I'd need to do on a new box is visit www.cygwin.com to download/install Cygwin (which is almost a 1-click process, or close to it), copy my script into my /bin dir, fire it up, and go away for a while. Joy.
The second big use I can see, although it's not as well-defined yet, is scripting Outlook, and *possibly* Exchange as well.
For instance, I might be able to write myself a little admin script that can automatically accept meeting invites that meet certain criteria, provided the script can access my inbox and calendar.
Or I might be able to do better automated manipulation and cleanup of my Outlook folders than what the GUI gives me. Or... who knows? All I know is that Outlook doesn't do a bunch of stuff I'd like it to do, and scripting might help alleviate that.
We all (hopefully) know how useful scripting can be on our Linux boxes. You can (and should!) automate away any tedious typing tasks; not only does it free you up to do more interesting and creative stuff, AND help prevent the ol' carpal tunnel syndrome, it also gives you practice at doing a kind of coding that you might not otherwise get a lot of exposure to -- and that can help improve your regular day-to-day coding.
But I'm not sure many Amazon folks (or Linux users in general) have had the Win32-scripting side of things really "sink in" yet. It's only just barely starting to sink in for me. But I have a feeling once I start thinking this way, I'll spot all sorts of automation tasks that I'd never thought to automate before.
Try it out! If you're dead-set on using Perl, the Perl/Win32 SDK is free, and you can use either Cygwin Perl or the native ActiveState Perl port. I'd personally recommend Cygwin, but the reasons for that go way beyond the scope of this blog entry.
If you want to learn more about Ruby, or just discuss it with other Amazon folks, hop on the ruby mailing list. The main website for Ruby is http://www.ruby-lang.org.. It has an HTML-ized (in color!) version of the Programming Ruby book by the Pragmatic Programmer guys, Dave Thomas and Andrew Hunt. Great book, great intro to Ruby, well worth a look.
(Published Sep 8th 2004)
p.s. -- here are some URLs for learning more about how to do Windows automation with Ruby.
First, there's this project called "web testing with Ruby": http://www.clabs.org/wtr/ -- it's a set of Ruby modules specifically designed for driving Internet Explorer programmatically using the techniques from my blog post. The idea is to be able to QA web applications by simulating end user behavior from automated tests.
Ruby makes this so dang easy, it makes me wish I were a QA test engineer.
Next, the Ruby Application Archive (RAA, sort of like CPAN for Ruby) has http://raa.ruby-lang.org/project/ruby-win32/ -- contains some convenient wrappers for Win32 processes, setting registry entries, and other goodies. Here's a search string that shows a bunch of Win32/Ruby libraries:http://raa.ruby-lang.org/cat.rhtml?category_major=Library;category_minor=Win32
The win32ole library that comes "stock" with Ruby is a fairly low-level interface. It's the one I've been using in my scripts. It basically gives you access to any Win32 API call, or any OLE interface exposed by Windows or a Windows application. But that doesn't mean it's super convenient. These libraries on RAA look like they'll be a huge help, providing clean wrappers for things like the Windows event loop, concurrency constructs, processes, registry, etc.
I'll post again on this topic next time I get a chance to do something with Win32/Ruby. I've been way too busy lately, unfortunately. Soon, I hope.
Posted by: Steve Yegge at September 18, 2004 07:30 PM