How Can I Tile the Windows on the Desktop?


Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a script that opens nine different command windows in order to monitor performance. How can I tile those windows horizontally after they’ve all been opened?

— AM

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AM. You know, one of the Scripting Guys might not be much when it comes to scripting, but he is a master of one thing: he has a near-monopoly on totally useless knowledge. For example, he knows all nine verses of My Darlin’ Clementine; heck, he can sing O Come All Ye Faithful in Latin. He knows the first – and last – names of all the castaways on the original Gilligan’s Island; he even knows Paul McCartney’s first name. (And, no, it’s not Paul.) Every day this Scripting Guy comes to work hoping someone will ask him who came up with the story of Rudolph the Red-Nosed Reindeer. Every day he goes home disappointed.

But today is different. Many, many years ago this same Scripting Guy learned how to tile windows using a script. Not being particularly imaginative, he couldn’t think of a scenario in which this might prove useful. Therefore, he decided he’d just wait until someone asked him how to tile windows using a script. He’s been waiting a long, long time.

AM, you’ve at last made this Scripting Guy one happy camper. You want to know how to horizontally tile windows? All you had to do was ask:

Set objShell = CreateObject(“Shell.Application”)

Good point: after all these years you’d expect something more dramatic than just two lines of code. But that’s all it takes. You simply create an instance of the Shell.Application object and then call the TileHorizontally method. If you want to tile your windows vertically, you can use this code:

Set objShell = CreateObject(“Shell.Application”)

And if you want to cascade your windows, use this code:

Set objShell = CreateObject(“Shell.Application”)

Pretty easy but, then again, no one said you had to be Professor Roy Hinkley in order to tile windows. (Yes, Roy Hinkley was the Professor on Gilligan’s Island.)

Two minor caveats here. First, these methods work only on the local computer; that’s because you can’t instantiate the Shell object on remote machines. But unless you were planning on having some fun by tiling and re-tiling the windows on someone else’s computer that shouldn’t make much difference.

In addition, we should point out that this script will tile all the windows on the computer, not just the nine command windows you started with your script. But that’s true even if you tile windows manually (by right-clicking the Taskbar and then clicking Tile Windows Horizontally or Tile Windows Vertically). Something you’ll just have to live with.

Just to show you what this might look like in real life, we’ve created a script that opens nine command windows and then tiles the desktop. Note that we added a Wscript.Sleep statement and paused the script for 5 seconds (5,000 milliseconds) before tiling the windows; we did that to make sure all nine command windows were open and visible onscreen before we tiled them.

Here’s what the code looks like:

Set objWSHShell = CreateObject(“Wscript.Shell”)

For i = 1 to 9 objWSHShell.Run “%comspec% /k” Next

Wscript.Sleep 5000

Set objShell = CreateObject(“Shell.Application”) objShell.TileHorizontally

And here’s what the desktop looks like after running the script:

Windows Desktop

We’ve waited a long time to see a picture like that.

Oh, and one more thing: it’s James. James Paul McCartney. Thanks for asking!


Discussion is closed.

Feedback usabilla icon