Acme Technologies Logo
spacer image

Mac OS X Tips - Command Line Utilities

Get More out of OS X

Mac OS X has a lot of very useful utilities available from the shell (/Applications/Utilities/Terminal). Below you'll find a collection of cool things you can do in the Unix shell on Mac OS X.

screencapture

The screencapture utility allows you to capture a window, a selection or the entire screen/monitor when executed. You can see the available options by typing screencapture in Terminal and pressing the Return key.

usage: screencapture [-icmwsWx] [file] -i capture screen interactively, by selection or window control key - causes screen shot to go to clipboard space key - toggle between mouse selection and window selection modes escape key - cancels interactive screen shot -c force screen capture to go to the clipboard -m only capture the main monitor, undefined if -i is set -w only allow window selection mode -s only allow mouse selection mode -W start interaction in window selection mode -x do not play sounds -S in window capture mode, capture the screen not the window file where to save the screen capture

For example: typing screencapture -c -i -W will change the mouse pointer to a camera icon and as you move the pointer over the visible windows, each window will highlight. Clicking one time in a highlighted window takes snapshot of the window, and because we specified -c as an option, it places the image on the clipboard.

If you remove the -W option from that line, the mouse pointer becomes a cross-hair and you can click and drag over a region of the screen, e.g., screencapture -c -i

pbcopy, pbpaste - Manipulate Clipboard

pbcopy and pbpaste allow you to get and set the clipboard from the command line. pbcopy takes the standard input and places it in the specified paste- board. If no pasteboard is specified, the general pasteboard will be used by default. The input is placed in the pasteboard as ASCII data unless it begins with the Encapsulated PostScript (EPS) file header or the Rich Text Format (RTF) file header, in which case it is placed in the pasteboard as one of those data types.

pbpaste removes the data from the pasteboard and writes it to the standard output. It normally looks first for ASCII data in the pasteboard and writes that to the standard output; if no ASCII data is in the pasteboard it looks for Encapsulated PostScript; if no EPS if present it looks for Rich Text. If none of those types is present in the pasteboard, pbpaste produces no output.

Both commands support the following options:

OPTIONS -pboard {general | ruler | find | font} specifies which pasteboard to copy to or paste from. If no pasteboard is given, the general pasteboard will be used by default. -Prefer {ascii | rtf | ps} tells pbpaste what type of data to look for in the pasteboard first. As stated above, pbpaste normally looks first for ASCII data; however, by specifying -Prefer ps you can tell pbpaste to look first for Encapsulated PostScript. If you specify -Prefer rtf, pbpaste looks first for Rich Text format. In any case, pbpaste looks for the other formats if the preferred one is not found.

So let's try out these commands. Since pbcopy reads from standard input, we'll use the echo command to feed some text to pbcopy.

$ echo 'Fluffy would taste good in a \ cat bisque.' | pbcopy $ echo `pbpaste` Fluffy would taste good in a cat bisque. $

There are two things to notice in those lines. In the first line we're using the echo command which simply writes to standard output whatever you give it as arguments. This output is "piped" (think of it as being passed to the next command) via the vertical bar operator to pbcopy. The second line which demonstrates pbpaste uses the back-tick operator which looks like a single quote character, but is actually a back-leaning quote (mostly likely it's the character on the same key as the tilde ~ character on your keyboard, up in the top left corner of your keyboard (on US-keyboards).

The back-tick operator is shorthand syntax for executing a command and routing the output of that command to standard input. In other words, the echo command gets its input from executing pbpaste

in the next example I'll copy the contents of a file (assumes a file named "example.php$quot; in current directory):

$ cat example.php | pbcopy

Now the contents of the file is available for pasting in any other window. the cat command concatenates files to standard output. See the next tip for examples of that command.

The following (or a variation of it) is something I use all the time. This example uses the unix find command to locate all files with the suffix .html, prints the full path for each, and pipes those paths into a new bbedit document. This requires that you have BBEdit and have installed BBEdit's command line tool.

# Find all files with .html extension and show paths in bbedit $ find ~/Sites -name '*.html' -print | bbedit # Grab a web page and copy the html to clipboard $ curl http://www.google.com/ | pbcopy

cat - concatenate files

cat echos one or more files to standard output and is typically used for joining multiple files into one, or processing multiple files with some other utility. For example, to count the total number of lines in three different files, we can use the wc word count command with the -l option to only display line statistics:

$ cat file1 file2 file 3 | wc -l 178

Which printed to standard out a total line count for all files combined of 178. Without the -l option, it would have produced three numbers, the line count, word count and byte count.

say - Speak text

This tool uses the Speech Synthesis manager to convert input text to audible speech and either play it through the sound output device cho- sen in System Preferences or save it to an AIFF file. You pass it a string to be spoken on the command line and the available options allow for choosing the voice to be used, a file to be spoken, or an output file to save the spoken text in AIFF format.

defaults

Defaults allows users to read, write, and delete Mac OS X user defaults from a command-line shell. Mac OS X applications and other programs use the defaults system to record user preferences and other information that must be maintained when the applications aren't running (such as default font for new documents, or the position of an Info panel). Much of this information is accessible through an application's Preferences panel, but some of it isn't, such as the position of the Info panel. You can access this information with defaults

host - DNS lookup utility

host is a simple utility for performing DNS lookups. It is normally used to convert names to IP addresses and vice versa. When no arguments or options are given, host prints a short summary of its command line arguments and options.

Example: host www.apple.com

osascript, osacompile - Compile / Execute AppleScript

osascript executes the given script file, or standard input if none is given. Scripts may be plain text or compiled scripts. osascript was designed for use with AppleScript, but will work with any Open Scripting Architecture (OSA) language. To get a list of the OSA languages installed on your system, use osalang(1). For documentation on Apple- Script itself, see http://www.apple.com/applescript.

open - Open <Anything>

The open command opens a file (or a directory or URL), just as if you had double-clicked the file's icon. If no application name is specified, the default application as determined via LaunchServices is used to open the specified files.

If the file is in the form of a URL, the file will be opened as a URL.

You can specify one or more file names (or pathnames), which are interpreted relative to the shell or Terminal window's current working directory. Examples:

For example, the following command would open all Word files in the current working directory:

Open all word documents in the current directory
open *.doc
Open a Finder window on the current directory
open .
Open a Finder window for your home directory
open ~
Open a Finder window for the Applications directory
open /Applications
Open a URL
open http://www.google.com/
Open a file named "foo.txt" with SimpleText
open -a /Applications/TextEdit.app

sw_vers - Print OS X version info

sw_vers prints version information about the Mac OS X or Mac OS X Server operating system running on the local machine.

killall - Quit a process by name

Killall kills processes selected by name, as opposed to the selection by pid as done by kill(1). By default, it will send a TERM signal to all processes with a real UID identical to the caller of killall that match the name procname.

For example, the command: killall Dock
would restart the Dock application.