Section 13.2. Unix Without Terminal


13.2. Unix Without Terminal

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

The Unix top program is meant for checking the status of all your other programskind of like a text-only Activity Viewer (in your Applications Utilities folder). For more information about top, type man top in Terminal.

13.2.1. Getting Program Results Back

Still, 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:

  • Part 1 runs calin the background, of course, because you opted to use do shell script rather than commanding Terminal directly. The result of your Unix program (in this case, a text representation of the current month) gets stored in theResult.

  • Part 2 displays a dialog box with the result of your Unix program (Figure 13-3).

Figure 13-3. The output of the cal program. The reason some of the numbers don't line up quite right is that Terminal uses fixed-width fonts (that is, every letter, number, and space takes up the same amount of room), whereas AppleScript uses varying-width fonts (so spaces appear narrower than letters and numbers).


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.

If you want the calendar for some month besides the current month, just use cal like this:

cal 3 2006

That would get you a calendar for March (month 3) of 2006.



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