Section 14.2. Noting Important Events


14.2. Noting Important Events

If you want a notification when something important in your script happens, the display dialog command (Sidebar 4.2) is always a good choice. In a script like the following, for example, careful use of display dialog can let you figure out how many times a repeat statement is running:

set currentIteration to 1 --How many times the repeat statement has run repeat 10 times     tell application "Finder"         make new Finder window     end tell     display dialog "Made new Finder window #" & currentIteration     set currentIteration to currentIteration + 1 end repeat

Each time that script creates a new Finder window, you'll see a dialog box like the one in Figure 14-2. That way, you can keep track of how many times your repeat statement has run so farand stop your script if you've seen too many Finder windows appear.

Figure 14-2. This dialog box lets you know that your script has just created a seventh Finder window. And that information, along with $130, will buy you a retail copy of Mac OS X.


Of course, there's one big disadvantage to using display dialog: it interrupts your script's progress. You have to stick around while your script is running, so you can click away each dialog box. And if your display dialog command is inside a long repeat statement, you could be spending the rest of the week just clicking OK.

14.2.1. The Event Log

Luckily, you don't have to use bothersome dialog boxes to track your script's progress. AppleScript's log command lets your script take silent note of events while they're happeningand lets you read the notes when the script is done. This is the perfect arrangement if you want to know what your script is doing but don't mind finding out after everything has happened.

Here's how to use the AppleScript log:

  1. Incorporate the log command into your script.

    Anywhere you can use display dialog, you can use log. For example, the following tweaks would modify the previous script to log notifications instead of displaying them in dialog boxes:

    set currentIteration to 1 repeat 10 times     tell application "Finder"         make new Finder window     end tell     log "Made new Finder window #" & currentIteration     set currentIteration to currentIteration + 1 end repeat

  2. Click the Event Log tab at the bottom of the Script Editor window (or press -3).

    By switching to this tab before you click Run, you tell Script Editor, "Please show me everything that gets logged while my script is running."

  3. Click Run, and watch as your Event Log fills up with notifications (Figure 14-3).

Figure 14-3. A typical Event Log. Everything in the lower pane is sequential: the commands at the top were logged before the ones at the bottom.


It may surprise you to see a multicolored log, but each color means something special:

  • Blue text indicates a verbatim AppleScript command. When your script comes upon tell application "Finder" or make new Finder window, for example, AppleScript automatically logs them in blue. That way, when your script is finished, you can look in Event Log and discover the order in which your commands ran.

  • Purple text indicates the result of an AppleScript command. After logging each make new Finder window command, for example, you'll notice that Script Editor puts purple text like "Finder window id 314." That's AppleScript's way of telling you, "I just ran the make new Finder window command, and once it was finished, there was a new Finder window hanging around. (By the way, in case you want to refer to that window in your script, the window's id number is 314.)"

  • Finally, black text (surrounded in parentheses and asterisks) is text that you logged. When you run this example, you'll notice black text like "(*Made new Finder window #7*)", which corresponds exactly to what you'd see in a dialog box like Figure 14-2.

14.2.2. The Event Log History

The Event Log (described in the previous section) is a great tool for seeing how your scripts run, but it's hardly perfect. For one thing, you have to switch to the Event Log before you run your script, or else AppleScript won't log your events. For another thing, you don't have much control of what AppleScript actually logs (commands, results, and so on). Finally, every time you run a script, AppleScript overwrites the script's existing Event Log, making it impossible to see what the script has done in the past.

Luckily, there's a tool that avoids all these problems: the Event Log History. This self-standing window logs the events from all your scripts and lets you browse the logs at your leisure. You can even see the logs from scripts you've already closed.

Before you can pull off any of those tricks, though, you first have to open the Event Log History. You can do this in either of two ways: by choosing Window Event Log History or by pressing Option--L. Whichever way you choose, Script Editor displays a window like Figure 14-4.

Figure 14-4. The Event Log History. Click an entry in the left pane to display that script's log in the right pane. Or, if you just want to browse the left list, double-click the vertical divider bar in the middle of the window to hide the right pane entirely.


14.2.2.1 Navigating logs

At first glance, the Event Log History might seem pretty self-explanatory: the left list shows you the most recently run scripts (and when you ran them), and to read a script's log, you simply click its entry. However, if that's all you do with the Event Log History, you're missing out on a whole lot of tricks.

For one thing, you can Shift- or -click multiple items in the left list, so you can see the logs of several scripts simultaneously. This trick can save you time when, for example, you want to see how the Event Logs of two similar scripts compare to each other. (You can even use this trick to select several occurrences of the same script in the left list, so you can compare how a script ran yesterday to how it ran 5 minutes ago.)

Unfortunately, the Event Log History displays multiple scripts one above the othernot side by sideso it's hard to compare logs directly.

If those features aren't exciting enough, though, you can even narrow down a single log into its various components. Figure 14-5 shows you how.

Figure 14-5. By clicking the flippy triangles in the left pane, you can narrow down the log from an entire script (in this case, the script named Untitled) into its tell statements, individual commands, and finally, the results of those commands. Here, by selecting the make command, you can see the full text of the commandand its resultin the right pane.


14.2.2.2 The toolbar

Just like normal Script Editor windows, the Event Log History comes with its own set of toolbar buttons. You won't find any Run or Debug buttons here, however; instead, you'll see the following:

  • Clear History lets you wipe clean your entire Event Log History, erasing all the entries in the left pane. This trick is useful if you've got dozens of old logs clogging your History and you don't need to read them anymore.

Of course, this Event Log History-purging happens automatically when you quit and restart Script Editor. But, if you'd just like to cap the number of entries in your Event Log (rather than erasing all the entries), visit Script Editor Preferences History and turn on "Maximum entries" for the Event Log History.

  • Show Script finds the script that's selected in the left pane, and opens that script in its own window. This can save you time when a log is confusing, for example, and you want to figure out which commands produced which lines.

    This feature works for scripts you've already closed, too; as long as a script's log remains in the History window, you can reincarnate the script's code by clicking Show Script.

If mousing up to the toolbar to click Show Script is too slow for you, you can simply double-click a script's name in the left pane to bring that script back to life.

  • The Options pop-up menu lets you choose how specific you want your logs to be. The default setting, Log Events and Results, is meant for verbose people: it shows you AppleScript commands, command results, and your own log statements, too. The less talkative setting, Log Events, shows you everything except your commands' results. And the simplest setting, Log Nothing, is perfect if your scripts work so flawlessly that you never need to read their logs.

    Note, however, that no matter what setting you choose, it won't take effect on your existing logs.

You can use all the toolbar tricks from Section 2.1.1.5 with the Event Log History window, too.



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