Section 6.3. The Ever-Useful every Keyword


6.3. The Ever-Useful every Keyword

As 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 Programs

Seeing 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:

  1. In Script Editor, open System Events's dictionary (File Open Dictionary, or Shift--O).

    Page Sidebar 3.4 explains the magic behind AppleScript dictionaries.

  2. Click the Processes Suite flippy triangle, and then click the Classes flippy triangle that appears underneath.

    You get a list of many of System Events's AppleScript nouns (or, in geek-speak, AppleScript classes) that you can use in your own scripts.

  3. Select the entry for process (Figure 6-3).

    A process is just System Events terminology for a "running program." Therefore, by getting a list of processes from System Events, you can discover all the programs running on your Mac.

Figure 6-3. A process is System Events' nerdy term for a running programwhether that program is visible or not. Using the every keyword, you can access a list of all the processes that are running at a particular time.


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).

Figure 6-4. If you select a program that has an icon in the Dock (like Safari), the program comes to the front when you click OK. On the other hand, if you select an invisible program (like, say, iCalAlarmScheduler), nothing happens when you click OKor, in a few rare cases, Script Editor may crash. That's because invisible programs, by their very definition, can't be activated.


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.



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net