Project97.Connect with Aqua


Project 97. Connect with Aqua

"How do I cross the great divide between Mac OS X graphical applications and the command line?"

This project shows you some techniques for sharing data and services between Aqua (Mac OS X graphical) applications and command-line utilities. It covers the commands pbcopy and pbpaste. Project 98 shows you how to open graphical applications from the command line.

Learn More

Project 6 covers standard input, standard output, redirection, and pipelining.


Copy and Paste

All graphical applications use the pasteboard (also called the clipboard) to save text between copy and paste operations (Command-c and Command-v). The pbcopy command reads text from its standard input and writes it to the pasteboard, providing a way to copy Terminal output directly to the clipboard.

To copy an entire file to the clipboard, we might type

$ cat ~/Sites/index.html | pbcopy


In a graphical text editor such as Apple's TextEdit, we need only press the familiar Command-v to paste the copied file ~/Sites/index.html into the current document.

Even more useful, we can copy the output from any Unix command directly to the clipboard. Let's copy the output from the cal command, covered in Project 96, and paste it into a Microsoft Word document. We issue the cal command and pipe its output to pbcopy.

$ cal | pbcopy


In Word, we type Command-v to paste the copied text into the current document (which is exactly how the following example was generated).

 September 2005  S   M Tu  W Th  F  S               1  2  3  4   5  6  7  8  9 10  11 12 13 14 15 16 17  18 19 20 21 22 23 24  25 26 27 28 29 30


Going in the other direction, to copy the contents of the clipboard to standard output, use the pbpaste command. Copy a section of text in TextEdit by selecting it and typing Command-c, for example. Then use pbpaste to write the text to the Terminal screen or, more usefully, pipe it to the standard input of a Unix command.

Let's count the number of lines, words, and characters in the copied text by piping the clipboard contents to the wc command.

$ pbpaste | wc       28      80     857


Drag and Drop

Apple's Terminal recognizes when a file, folder, or application is dropped onto the Terminal window. It responds to the drag and drop by writing the full pathname of the dropped entity on the command line. Terminal doesn't write contents to the command linejust names.

Use this technique whenever a command requires a filename. For example, type

$ cd


followed by a space. Drag a folder from the Finder or the Desktop to the Terminal window. The command line will be completed with the full pathname of the dropped folder.

$ cd /Users/saruman/1dot1/Peachpit/101-projects-book/archive


Now press Return to set your home directory to the folder you just dropped.

Double-Click a Shell Script

Apple's Terminal recognizes a file that contains a shell script. Double-clicking such a file launches a new Terminal window in which the script is automatically executed. A simple script will illustrate this. We write a standard shell script and save it to a file whose name includes the extension .command. The example script that follows displays the names of all files in your Documents folder that were modified less than a day ago.

$ cat mod.command #!/bin/bash echo "All Documents modified less than a day ago" find ~/Documents -mtime -1


Learn More

Refer to Project 9 if you are new to writing shell scripts.


The script must be made executable before it can be executed by double-clicking.

$ chmod +x mod.command


Learn More

Project 44 shows many Terminal tricks.


To run the script, select the file mod.command in the Finder, and double-click it. A new Terminal window opens and immediately starts executing the script. The window remains open so that you can view the results, which are followed by logout and [Process completed].




Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net