Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, PHP, and whatever else I happen to feel like hacking at.

Stack windows on top of each other—Thursday, March 18th, 2010

I check in on the Nisus scripting forum every once in a while to see if there are any new tricks I can pick up for managing my online documents (such as my Gods & Monsters gamebooks).

On that forum, member bjast asked “How would I go about writing a macro to automatically stack all of the open windows exactly on top of each other?” Well, it turns out that Nisus macros are designed mainly for working within documents, and its window manipulation is not very extensive. But being a well-designed Mac OS X application, it also has AppleScript support. And there are standard tools for manipulating windows in AppleScript that most applications get automatically by supporting AppleScript.

[toggle code]

  • tell application "Nisus Writer Pro"
    • copy windows to windowList
    • copy the bounds of the first item of windowList to windowBounds
    • repeat with aLowerWindow in the rest of windowList
      • if aLowerWindow is visible then set the bounds of aLowerWindow to windowBounds
    • end repeat
  • end tell

This will grab the “bounds” of the focus window and set all other windows in Nisus Writer Pro to have the same bounds. They’ll all go behind the focus window (which is almost always the top-most window).

This is useful for comparing similar documents. Besides Nisus Writer Pro, it also works in AppleScript Editor, AppleWorks 6, Mail, Microsoft Word X, Safari, Smultron, and TaskPaper1.

There appears to be some sort of oddity or bug in Terminal; it sometimes works in Terminal, and sometimes it positions lower windows horizontally but not vertically.

By only setting the bounds of windows that are “visible” the script ignores hidden windows, obviously, but also docked windows. It does not ignore windows in other spaces if you use Spaces; I couldn’t find a way to distinguish them. However, it will not pull them into the current space, it will just position them in their own space to be where your document is in the current space.

To use this, you need to enable the script menu in your Mac’s menu bar. When you go to AppleScript Editor to paste the script in, check your preferences to make sure that “Show Script menu in menu bar” is checked. Then, whenever you go into an application (such as Nisus) you can choose “Open Scripts Folder” from the script menu in your menu bar. Any script you place in the application’s folder will show in that menu when you use that application.

All roads lead up—Tuesday, March 9th, 2010

When I was in college, I had a lot of fun studying programming and psychology. Neither topic helped me pass my Physics courses. When I realized that Physics was not my future, I chose not to switch to Computer Science because I thought the future of programming was going to be that of mechanics running through the same rote procedures for every car that came in. That it would be reduced from a creative endeavor to gluing things together, though I wouldn’t have worded it that way then.

I was wrong. I was wrong because I didn’t foresee the Internet and web sites. And I ended up working in computer programming anyway, due to a combination of skill and just being in the right place at the right time. A lot of what Mike Taylor is talking about in Whatever happened to programming? is him being in the wrong place. If he were to take a pay cut and work for a small organization, he’d be able to, and probably be required to, do his own programming again, using Python, PHP, or Ruby possibly in combination with HTML and CSS.

One of my first tweets was “The main benefit of JavaScript toolkits is they condense to fifty or so kilobytes what would otherwise take several hundred bytes to do.” It’s true, as Mike writes, that much of what passes for programming today is trying to find the right parameters for some black box toolkit. But it’s also true that what some people seem to want to do using, say, JQuery, are things that don’t need more than a few lines of code if you know how to program. They don’t need to include a 50k file on every page view just to flip between a series of divs.

But I also stand by what I said in Learning to program without BASIC: our expectations were lower then. Programming isn’t fun unless we’re programming something useful, even if it’s only useful for a moment. This means that some things we had fun programming then aren’t going to be fun now, because computers—and frameworks—already do that for us. But, so far at least, there’s always something else to program. There’s always something else to optimize and make more beautiful.

To paraphrase Stanislaw Lem, we’ve climbed to reach the summit, and we’ve discovered that some roads still lead up.

A present for Palm—Wednesday, January 20th, 2010

I was browsing the web, minding my own business, when a twelve-point Macalope strode past. Being from flyover country, my first thought was “dinner on the table”. But since this is the holiday season and it’s possible he’s a distant relation to Rudolph, I offered him a beer and we started talking.

Well, not so much talking. When a creature has a computer screen for a head, their side of the conversation is all blog. It’s not like the Macalope’s got high-res graphics in that MacPlus.

Apparently Computerworld is taking Apple to task because Palm’s software doesn’t work. Some idiocy is so bad it requires fictional creatures to respond; the Macalope, in a springtime editorial, does the job.

  1. Palm is hardly a “little guy”.
  2. It’s hardly Apple’s job to do Palm’s work for them.

But the mythical man/Mac/beast forgot point 3: Apple has already done Palm’s work for them. The Macalope writes:

Just move the files. All you’re really complaining about is losing the playlists. Even in the realm of first-world problems, that rates pretty low.

In fact, even that isn’t a problem no matter what world you’re in (or on). Apple has made all of this data easily available in a standard, easy-to-read (for computers) format. Every iTunes user’s Music folder contains an “iTunes Music Library.xml” file. As you might guess from the extension, this file is simple XML. It continuously updates with not just your music, but also all of your playlists: both the static ones and the dynamic ones.

Why do they do this? So that ungrateful third-parties like Palm can use it to make life easier for iTunes-using music lovers.

So if you want to blame someone for Palm not syncing your iTunes music, blame Palm; Apple’s already done the work, clocked out, and gone home to the little tablet and six tiny iPods.

But why talk about blame? It’s a new year and a new era of bipartisan peace, love, and understanding. In the spirit of the post-holiday season, here’s a present for Palm.