13.2. Unix Without TerminalAfter using the Terminal for a while, you're bound to get bored. That's because, despite all the window settings you can tweak, Terminal is still a holdover from the days of mainframes. At some point, you'll probably wonder, "Why can't I just run Unix programs straight from AppleScript, without going through an ugly Terminal window?" As it turns out, you can. AppleScript includes a special command, do shell script, for running Unix programs without Terminal's assistance. For instance, if you wanted to run the defaults program from Section 13.1.1, you could use the following commands instead of invoking Terminal: do shell script "defaults write com.apple.dock wvous-floater -bool true" tell application "Dock" quit end tell When you run that script, AppleScript runs the Unix defaults program, silently and in the background. If there's a problem while running the Unix program, AppleScript displays an error dialog box. Otherwise, if there aren't any problems, your script proceeds to restart the Dockand, in doing so, displays the blue Exposé blob. Of course, this background-only do shell script command has its limitations. For example, you can't run pico with do shell script, because you wouldn't get to see a Unix text-editing window. And certain Unix programs, like top, run foreveror at least until you force them to quitand therefore aren't very well suited for using in the middle of an AppleScript.
13.2.1. Getting Program Results BackStill, there are some Unix programs that give you back informationand don't take forever to finish running. The cal program, for instance, presents a nicely formatted calendar of the current month when you run it in Terminal. Now, suppose you wanted to run cal from AppleScript. If you were of the Section 13.1 mindset, your first instinct would be to write a script that asked Terminal to run the cal command for you. However, there's a much simpler way to do it, using do shell script: --Part 1: set theResult to (do shell script "cal") --Part 2: display dialog theResult Here's how the script works:
Now, whenever you want a calendar of the current monthso you can check whether New Year's day falls on a weekend, for examplejust run your script.
|