"AppleScript Makes a Good Broom"

Written and published May 1, 1999

Update: When I first wrote this Bill Briggs was a MacCentral reader, full of ideas (not unusual among my readers). After this column was published so many people wrote in asking for more on AppleScript that I proposed Bill do his own column. And thus, Mac Efficiency 101 had a highly successful spin-off - the AppleScript Primer. These days you can still find Bill's columns at MacCentral, along with mine. And, you can find Bill teaching AppleScript at Macworld Expo too. In fact, you can also find us speaking/teaching together at Macworld Expo; we did Mac Beginnings (Mac 101) in January 2001 and Mac Beginnings (Mac 101 and Mac 102) in July 2001.

First, I have to admit that I am not the author of this article's title. Reader Bill Briggs is. It was the subject line of his email to me several weeks ago when he shared his AppleScript broom with me. I tried his suggestion and find it very useful. (Not to mention fun.) For weeks I've wanted to share this with you.

Admittedly, I should devote one more week to the topic of email files since another 40 or so good responses have come in from another 40 or so great readers. I will pass those tips, many about Netscape mail, on to you next week. It also seems others have had more luck moving Emailer files. I'll look into that and report. Meanwhile, this week I give to you a great, handy AppleScript, thanks to Bill Briggs.

For many of you this may be your first AppleScript creation. Just wait... <g>

So what's Bill's Broom and Why?

As Bill wrote to me, "necessity is often the mother of invention, and last week I had an incident that required a remedy..." It seems Bill has a friend who likes to reorganize Bill's desktop. (Heck, he downright plays pranks!) Of course, we all love our Macs, and being rather intimate with our Macs, like our desktop set up just right -- the way we like it. And, chances are that most of us have a friend like Bill's. (I'd tell you about the friend but I forgot to ask permission to quote him.) Anyway, Bill figured out how to undo the damage in one easy operation. Bill's broom script is handy even if you don't have a mischievous friend. For example, when you switch your monitor's resolution, icons become misplaced. (I happen to do this many times a day for screen shots for my books.) Bill's other reason is similar: he has a PowerBook and says, "If you use, for example, an 800x600 projector attached to a G3 PowerBook, when you get back to your desktop, sometimes your icons have been moved around a lot. This little cleanup device will get you back to your "normal" arrangement. As Bill wrote, "AppleScript and the recordable Finder to the rescue!" The solution, Bill's Broom, is a simple script you create for yourself, using Script Editor. Script Editor is a free utility that comes with your Mac system. You should find it in the AppleScript folder. Since everyone's desktop is different, Bill and I can't write your Broom script for you. It's up to you to create your own. Are you game? We hope you'll give it a go. Here's how.

Making Your Broom

Before you begin, clean up your desktop. File away any lose files and keep only stuff you permanently keep on your desktop. (i.e. partitions, desktop printers, trash, aliases to folders/files you always keep on your desktop, etc.

Now follow these steps:

  1. Set up your desktop exactly the way you like it. Move icons exactly into place as desired.

  2. Launch Script Editor. If a new window doesn't open automatically, choose New from the File menu.

  3. (Optional) Click the arrow next to the word Description and enter a description of the script for future reference.

  4. Click the Record button. The Green light will go on, letting you know your actions are being recorded. Your Apple menu may also flash an icon reminding you that you are recording. Your next steps, until you press Stop, all become part of the script.

  5. Click on the desktop. To have your script clean up every item on your desktop, select them all by using Select all [(Cmd)-A].

  6. With the icons selected, move them en-masse a few pixels, then back to where they were, then release the mouse button. [After you move the icons, sit back and watch as the Script Editor documents the placement of every icon on your desktop, noting its placement in terms of screen coordinates. See the screen shot below.]

  7. Click in the Script Editor window to make it active again.

  8. Click the Stop button. This, of course, ends the recording of your actions. The script window will record the words, "end tell," completing your script.

  9. (Optional) "If you're feeling playful," Bill adds, "click at the end of the last line last line after the words "end tell," press Return, then if you are using OS 8.5/8.5.1 type the words: say "That looks much better" using "Victoria" including the quote marks. (Or, for OS 8.1 or older type: speak "That looks much better" voice "Victoria" including the quote marks.)" [You need Speech turned on in your Speech control panel and the Speech manager extension in the Extensions folder, but if Speech is off it isn't harmful. You can, of course, change the words within the quotes or the name of the voice.]

  10. (Optional) Test your script now by messing up the icons on your desktop, then clicking the Run button. If all goes well, you'll see your icons move back into place.

  11. Use File->Save ((Cmd)-S) to save the script.

  12. Choose Compiled Script or Application from the popup menu. (If you use OSA Menu, as suggested here later, either is fine. I use Application so it is also double-clickable from the Finder. Compiled Script, when not in the OSA Menu, opens the Script Editor. )

  13. Name your script.

  14. Be sure to check Never Show Startup Screen. (If you forget this, each time you run your script you'll get a dialog box asking if you want to run the script.)

  15. Chose the folder into which you want to store your script, then click Save. (I saved mine to the Desktop first, to test it.
    Afterwards, I moved it.)

    Note: Don't do this while you have temporary files on your desktop. If you do, they become part of the script and when you remove them, you'll have errors. Instead, if you want to try this but don't want to remove your lose files from your desktop, choose which icons to clean up and ignore the rest. For example, I would select my hard drive and partition icons, desktop printers, Trash, an alias for my nephews, and a folder I call "File!" The rest of the stuff isn't constant so I wouldn't select it.

    In short, use this alternative step 5: Click on any one of the icons to be cleaned up. Then press the Shift key and click on each of the rest of the items you want cleaned up by your script.

    You can open your script to review or edit it at any time by launching Script Editor, choosing File->Open, then selecting the script from the Open dialog box.

Enjoy Your Broom! From now on, whenever your desktop gets messed up, just run that script and sit back for a moment as AppleScript magically sweeps everything back into place. I even use this script when my desktop isn't "messed up." I have three hard drive icons and just like them closer together than Apple puts them. My broom takes care of that for me. In fact, if I pop an alias of my broom into the Startup items folder (in the System folder) my desktop will be to my perfect liking day after day.

Storing and Accessing Your Broom

There are two ways I recommend you store and use your Broom. (Remember that I always advocate keeping files in a place you'll back up. This is a unique circumstance as it is a utility of sorts, but only you have a copy. Therefore, you want to have a backup unless you want to recreate it again later.) One way is to place it in your Utilities folder, and launch it from an alias you place in the Apple menu. The other, far better, is to use Leonard Rosenthol's OSA Menu. A copy of OSA Menu Lite came with your OS 8.5 install, but get the recommended newer version from Leonard's web site, http://www.lazerware.com/. After installing OSA Menu and restarting, click on your desktop, go to the new OSA Menu by your clock, select Open Finder Scripts folder, and place your Broom or an alias of it in this folder. (Then close the folder.) Then you can run your Broom from the OSA Menu. It will appear in this menu whenever you are in the Finder.

Next Week

More on where to store email files. That'll finish up those data files.

Previous Column • • •
Button that takes you to the index.
• • • Next Column