6.3. The Ever-Useful every KeywordAs you've seen on Sidebar 4.3, the every keyword is an extremely powerful tool for obtaining a list of items. You can use the Finder to get every file of the folder "Applications", or use TextEdit to get the name of every document. No matter how you use it, the every keyword is a huge timesaver. Moreover, when you use the every keyword, you're guaranteed to get a list as the result. That makes every a perfect companion to use with the choose from list command; the combination of these two commands lets you obtain a list and then present the list onscreen. 6.3.1. An Example: Displaying a List of Running ProgramsSeeing a list of all your running programs is enormously helpful, because it lets you check what invisible programs are working in the backgroundand, in some cases, hogging your computer's processing power. You could, of course, use the Activity Monitor program (stored in your Applications Utilities folder) for this task. But you could also do it with AppleScript, and that's a lot more fun and educational. To write such a script, you have to command Mac OS X's hidden System Events program, which monitors all the other programs running on with your Mac. Proceed like this:
You can see, in this dictionary, that all processes have a name property. Using that, you can display the name of every running program in a dialog box: tell application "System Events" set allApps to the name of every process --allApps is a list end tell choose from list allApps This four-line script is enough to show you every running program on your Maceven much of the system-level stuff that you probably didn't know existed. Still, you can't do anything with that informationother than brag to your friends about all the hidden programs you have, perhaps. It would be much more useful if you could double-click a program's name in the list to activate it. And, with a few script modifications, you can: tell application "System Events" set allApps to the name of every process --allApps is a list end tell set chosenApp to (choose from list allApps) --chosenApp is also a list tell application (chosenApp as string) --Convert chosenApp to a string activate end tell These additional lines take whichever program you selected in the choose from list dialog box, and send that program the activate command. With a quick double-click, therefore, you can bring any open program to the front (Figure 6-4).
There's only one problem with your current script: if you click Cancel in the dialog box, AppleScript continues running the script. (That's different from the display dialog command, where clicking Cancel immediately halts your script.) By the time AppleScript hits the tell application (chosenApp as string) line, it's pretty confused: how is your script supposed to know which program to activate if you clicked Cancel in the dialog box? The fact is, AppleScript doesn't know. If you click Cancel when this script is running, AppleScript considers the result of the dialog box to be falseand that's why you get a dialog box asking you to locate the program named "false" (which, of course, you can't, because there is no such program). This is a bug in your scriptbut, luckily, you can fix it by adding a simple if statement: tell application "System Events" set allApps to the name of every process --allApps is a list end tell set chosenApp to (choose from list allApps) --chosenApp is also a list if chosenApp is not false then --In other words, if you didn't click Cancel tell application (chosenApp as string) activate end tell end if Now, if you click the Cancel button, your script skips over the if statement completely, thereby eliminating the bug. |