<<

. 27
( 69 .)



>>


124
9
COMBINE UNIX AND AQUA


After you have captured a screenshot, you will probably want to edit it. The
Mac OS X screen capture only saves files in Adobe™s PDF format, which is
usually not the best format for images. Many graphics applications do not
open PDF files, and you cannot use a PDF file as an image in a Web page or
word processing document.
There are two ways to deal with this inconvenient file format. The first is to
use the Preview application, a standard Mac OS X application, to convert
the screenshot to a different format. You can open the screenshot in Preview
using the open command.

open -a Preview screenshot.pdf

The Export function is available through the File menu and allows you to
choose a different format, such as TIFF, JPG, or BMP.
The other approach saves your screenshot directly to the Aqua paste buffer,
using the -c option with the screencapture command.

screencapture -c -i

No filename is necessary because the captured portion of the screen is
stored in your clipboard. You can paste it into a graphics application such as
Photoshop or Preview and then save the file in whatever format you prefer.




– Your screenshot is saved in
‹ Position the cursor over ˇ Drag the mouse to
the corner that you want to indicate the section you the PDF file.
capture. want to capture.
› Click and hold the mouse Á Release the mouse button.
button.
125
UNIX FOR MAC




ACCESS THE AQUA CLIPBOARD
pbcopy < filename

Y
ou can copy and paste using the Aqua clipboard with
shell-command | pbcopy
the pbcopy and pbpaste commands. Mac OS X
keeps temporary data in a special memory space This action stores the contents of the file or the output of
known as the clipboard or paste buffer. When you use the the command in the paste buffer, and you can paste it into
Copy function of an Aqua application, whatever you copy other applications, such as TextEdit or Mail.
replaces the contents of the clipboard. When you use the
Paste function, you paste the contents of the clipboard into If you have copied text from another application, you can
the document. The Copy and Paste functions are usually access the paste buffer™s contents by using the pbpaste
listed in an application™s Edit menu, or you can use the command. This command sends the contents of the
shortcuts ” + C for copy and ” + V for paste. clipboard to standard output, where you can redirect it
to a file or pipe it to another command.
The pbcopy command reads text from the standard input
and places it into the Aqua clipboard buffer. You can use pbpaste > filename
this command to read a file™s content into the clipboard by pbpaste | shell-command
using input redirection from a file or by piping the output
Typing pbpaste alone displays the current contents of the
from another command to the pbcopy command.
paste buffer. If you have forgotten what is on your
clipboard, this is an easy way to check.

ACCESS THE AQUA CLIPBOARD




‹ Type a text file's filename ‹ Type pbcopy and press
COPY A TEXT FILE COPY COMMAND OUTPUT
TO THE CLIPBOARD TO THE CLIPBOARD
and press return. Return.
„ Type pbcopy and a space. „ Type a command that
– The clipboard now – The clipboard now
outputs text and a space.
¤ Type < and a space. contains the file that you can contains the command
¤ Type a vertical pipe
paste into other programs. output that you can paste
into other programs.
character (|) and a space.


126
9
COMBINE UNIX AND AQUA




You can use the following command to sort the
You can combine the pbpaste and pbcopy
contents and then store the text in your clipboard:
commands by piping the commands together.
This action allows you to insert a shell command
[ferro:˜] user% pbpaste | sort -bfi | pbcopy
to modify the contents of your paste buffer. For
If you paste the text into your word processor, the
example, you could copy the following text from
list of pets is sorted:
a Web site using your browser™s Copy function:
Angie, a dog
Nying, a dog
Kim, a dog
Olorin, a cat
Nying, a dog
Kim, a dog
Olorin, a cat
Angie, a dog

You can extract a list of the dogs only with the
following pipe through the grep command:

[ferro:˜] user% pbpaste | grep ˜dog™ | pbcopy




‹ Type a filename and press ‹ Type a command that
PASTE THE CLIPBOARD PASTE THE CLIPBOARD
INTO A TEXT FILE TO STANDARD OUTPUT
Return. reads standard input.
„ Type pbpaste and a space. „ Type pbpaste and a space.
– The file now contains the – The contents of the
¤ Type > and a space. ¤ Type a vertical pipe
contents of the clipboard. clipboard are sent to the
command.
character (|) and a space.



127
UNIX FOR MAC




COPY APPLE RESOURCE FORKS
The Unix cp command does not normally copy resource

Y
ou can copy a file and its resource fork using the
fork files because they are effectively invisible to most shell
ditto command. Mac OS X uses resource forks to
commands. If you copy a file or directory with cp, your
store additional information about the file. A resource
copy does not have a resource fork. However, resource fork
fork is a hidden file that accompanies another document.
sizes are included in the disk usage totals provided by the
When you use Aqua applications, resource forks are created
du command; thus, your copy may not appear to be the
automatically but are kept invisible to both the Mac OS X
same size as the original.
Finder and the Unix shell. To list resource forks in your
current directory, type this command:
The ditto command is another way to copy files and
directories. Unlike the cp command, ditto copies a
[ferro:˜] user% ls -l */rsrc
directory and its contents automatically so you do not need
to include an -r option as you would with cp. You do need
You see that your files are shadowed by smaller files of the
to include the -rsrcFork option:
same name with /rsrc appended. Each of these files contains
encoded information about which application created the
[ferro:˜] user% ditto -rsrcFork original copy
file and other types of metadata. Aqua applications use this
information, but Unix shell commands ignore it.
This action produces a copy with the resource forks
preserved.

COPY APPLE RESOURCE FORKS




– The ditto command
¤ Type -rsrcFork and a › Type the name of the
COPY A FILE
space. destination file and press copies the file and its
„ Type ditto and a space. Return. resource fork.
‹ Type the name of a file
and a space.




128
9
COMBINE UNIX AND AQUA


Two other commands work with resource forks to ensure that the Mac OS X Finder has
access to metadata. These commands are the CpMac and MyMac commands. CpMac is a
version of cp that copies files along with resource forks, and MvMac is a version of mv
that moves files while preserving resource forks.
The CpMac and MvMac commands are not automatically installed on all Mac OS X
computers, but are on the Mac OS X Developer Tools CD-ROM. For more information
on installing the Developer Tools, see Chapter 12.
The executable files for the CpMac and MvMac commands are stored in the
/Developer/Tools directory. If you have not added that directory to your shell™s
path, you will have to type the full pathname to use these commands.

Example:
[ferro:˜] user% du -s cat-pics/
48744 cat-pics/
[ferro:˜] user% cp -r cat-pics/ cat-pics-cp
[ferro:˜] user% du -s cat-pics-cp
37872 cat-pics-cp
[ferro:˜] user% /developer/Tools/CpMac -r cat-pics/ cat-pics-CpMac
[ferro:˜] user% du -s cat-pics-CpMac
48744 cat-pics-CpMac/




– The ditto command
¤ Type the name of a ‹ Type the name of the
COPY A DIRECTORY
directory and a space. destination directory and creates a copy of the
„ Type ditto -rsrcFork and a press Return. directory and copies the
space.
resource forks as well.




129
UNIX FOR MAC


RUN APPLESCRIPT FROM THE SHELL
The first version runs a file containing an OSA script

Y
ou can run Applescript commands or files from the
language; you do not need to set the script-file
command line, letting you access and control other
executable. If you omit the script-file argument, the
Mac OS X applications. Applescript is a simple but
osascript command reads lines of script from the
powerful programming language built into the Mac OS X.
standard input.
It is relatively easy to read and understand because it uses
a syntax similar to English. For example, to have the Finder
The second version of the command reads and executes a
application create a pop-up dialog box, you can use the
single line of script that is specified on the command line.
following Applescript:
You can include multiple -e arguments if you want to
execute more than one line of script at the same time.
tell Application “Finder” to display dialog
“Good morning!”
The default scripting language for osascript is Applescript.
If you have another OSA language installed, you can specify
The Applescript language follows the Open Scripting
it by giving a -l option to osascript, followed by the
Architecture (OSA) standard, making it an OSA language.
name of the language.
For a full list of OSA languages installed on your computer,
you can type the osalang command. To execute scripts
You can use the Applescript language in many more ways
written in OSA languages from the command line, use the
than shown here. You can use Applescript to control most
osascript command. There are two forms of this
Mac OS X applications. To learn more about the Applescript

<<

. 27
( 69 .)



>>