Section 13.1. Terminal


13.1. Terminal

To interact with Mac OS X's Unix core, you use the Terminal program, located in your Applications Utilities folder. When you launch that program, you see a pretty meager window, with a line of text and a cursor for you to enter programs' names (Figure 13-1).

Figure 13-1. A Terminal window. To change the window's coloring, visit Terminal Window Settings, choose Color from the pop-up menu at the top, and pick out any colors you want for the window's text and background.


This line of text is known as a command promptor, in casual geeky conversation, just a prompt. You type the name of a program here and press Enter (or Return) to run that program. When a new prompt appears, Unix is ready to accept a new program's name.

Depending on your Mac's name, your user name, and various other settings, your prompt may look different than the one shown here.

If you wanted to run the uptime program, for example, you'd type this:

Adam-Goldsteins-Computer:~ goldfish$ uptime

Once you pressed Enter, you'd see a result like this:

11:27  up 27 days, 17:50, 1 user, load averages: 0.50 0.45 0.43

That information tells you: what time it is; how long your computer's been running without restarting; how many users are on your computer right now; and how hard your processor is working (on a scale from 0 to 2).

Of course, if you want to check that information oftensay, so you can brag to your friends that your Mac's been running for a month straightit's kind of annoying to enter uptime in Terminal every time you want to check your Mac's status. Luckily, Terminal supports a special AppleScript command, do script, that can greatly automate the process of running Unix programs. Here, for example, is a script to run uptime from AppleScript:

tell application "Terminal"     activate     do script "uptime" --Here's where you specify the Unix program end tell

When you run that AppleScript, Terminal comes forward and runs the Unix uptime command for you.

By default, the do script command creates a whole new Terminal window to run your Unix program. If you'd rather run uptime in the current Terminal window, though, simply modify your script like this:

tell application "Terminal" activate do script "uptime" in the front window end tell

13.1.1. Changing System Settings

There are far more sophisticated programs you can run in Terminal, too. For instance, you can display a floating blue Exposé button (Figure 13-2) by running the following command in Terminal:

defaults write com.apple.dock wvous-floater -bool true

Figure 13-2. The Exposé blob. Click it once to display all the windows of the current program (the equivalent of pressing F10 on your keyboard), or Option-click the blob to display all the windows in all visible programs (the equivalent of F9 on your keyboard).


You won't see the blob, however, until you quit and restart the Dock. You can do that in one of two ways:

  • Use Applications Log out and log back in. Or, if that's too dainty for you, restart your entire Mac.

Once the Dock restarts, your blob appears onscreen, ready for action.

The defaults Unix command is meant for tweaking all sorts of system settings. For more information about defaults, run this in Terminal:

man defaults

Incidentally, the man program gives you help with other Unix programs. For more about the man command, run man man.

Of course, you can package your blob-creating Unix command in an AppleScriptso you can share the blobby convenience with all your friends, for example:

tell application "Terminal"     activate     do script "defaults write com.apple.dock wvous-floater -bool true" end tell tell application "Dock"     quit --Restart the Dock end tell

If you get bored with the Exposé blob, you can turn it off by running that same script, just replacing true with false.And if this example doesn't satisfy your thirst for system tweaks, examine a site like www.macosxhints.com for dozens more uses of the Unix defaults command.

13.1.2. Unix Text Editing

If you come from the graphical world of Mac OS X, the idea of a Unix text editor probably strikes you as pretty odd. In a Unix text editor, you can't use the menu bar or mouse to edit text, you can't drag-and-drop sentences to different places, and perhaps worst of all, you can't spell check your writing. Why on earth would people subject themselves to this?

Power, that's why. For people who spend their days writing computer programs, Web pages, and the like, Unix text editors offer all sorts of power-user features: simple looks, extreme customizability, and tons of keyboard shortcuts, for example. Even non-geeks might want to use Unix text editors on occasionso they can edit system files, for example.

Unfortunately, there's no simple way to open a text file from the graphical side of Mac OS X in the Unix side. The best you can do is type the name of the text editor in Terminal, followed by the path to file you want to open, like this:

pico ~/Desktop/CheeseDoodles.txt

That, of course, would open your Home Desktop CheeseDoodles.txt file in the Unix text editor named pico.

In Unix, the ~ symbol simply means, "in my Home folder." Additionally, forward slashes (/) are used to separate folder names. (In the Unix world, folders are also known as directories.)

Luckily, you can use AppleScript to simplify the process of opening a file in pico (or any other Unix text editor, for that matter). That way, you can edit your text files the old-fashioned wayin the Terminalwhich many power users prefer for its total reliance on the keyboard (and total avoidance of the mouse). This script gets you started:

--Part 1: on run     display dialog "Drag files to my icon to open them in pico." end run --Part 2: on open draggedItems     --Part 3:     repeat with currentItem in draggedItems         --Part 4:         set unixPath to the POSIX path of currentItem         --Part 5:         set unixString to ("pico " & unixPath)         --Part 6:         tell application "Terminal"             activate             do script unixString         end tell     end repeat end open

As you can tell from the on open line in part 2, this script is a droplet, so it works when you drag files onto the script's icon. See Section 7.3.3.1 for instructions on how to save your script as a droplet.

Here's how the script works:

  • Part 1 tells you to drag files onto the script's Finder icon to open them in Terminal. Of course, part 1 only runs if you don't drag files onto the script's icon in the first place.

  • Part 2 runs if you do drag files onto the script's icon. This part puts a list of the files you dragged into draggedItems.

  • Part 3 starts a repeat loop so the script can process every file you dropped. The file currently being operated on goes in currentItem.

  • Part 4 gets the POSIX path of the current filebasically, its Unix-compatible path.

  • Part 5 creates a new variable, unixString, to hold the command you're going to send to Terminal. For example, if you dragged your Documents Quesadillas.txt file onto the script's icon, unixString would hold "pico /Documents/Quesadillas.txt" after this part of the script.

  • Part 6 brings Terminal forward and runs your Unix program, pico. Since you don't specify what window to run the program in ([click here]), Terminal automatically spawns a new window for each file you dragged.

Although pico is the easiest Unix text editor to use, there are plenty of others that pack more features (vi and emacs, for example, are popular among programmers).If you'd rather make your AppleScript open text files in a different Unix program, simply replace the word pico with vi, emacs, or the name of whatever text editor you'd like to use. And if you're confused by all the choices of Unix text editors, visit www.reallylinux.com/docs/editors/editor.shtml for some guidance.

Now, if you ever feel the need to open some text files in Terminal, just drag the files to your script's icon. For easy access, you can even store your script in the Dock, so pico is never more than a file-drag away.



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