Project 99. OS XSpecific Commands"What Mac OS Xspecific commands might I find useful?" This project illustrates miscellaneous and useful commands that are specific to Mac OS X Unix. It covers the ditto, screencapture, SetFile, GetFileInfo, defaults, and plutil commands. Other Mac OS Xspecific commands are covered elsewhere in this book. They are
Copy with dittoThe ditto command recursively copies directories and files, preserving permissions and timestamps in a similar way to the Unix command cp -pr. In versions of Mac OS X before 10.4 (Tiger), ditto must be used in preference to cp to copy files that have resource forks, which the Unix cp command did not preserve. In the next example, we copy the entire letters directory and any subdirectories it contains to a new directory called letter.bak, preserving resource forks by passing the option --rsrc. $ ditto --rsrc letters letters.bak Learn More
In Mac OS X 10.4, ditto is less useful because cp, and most other Unix commands, now preserve HFS+ metadata, including resource forks. In a role reversal, ditto can be used to strip HFS+ metadata when passed the option --norsrc. Use this feature to trim the fat when preparing files for transfer to non-HFS+ file systems. The ditto command provides other useful options. Specify -V (verbose) to tell ditto to echo the name of each file as it is copied. Options also create and extract Unix cpio archives and PKZip archives for Windows compatibility: Read the man page for more details on these options. Note
A difference in the ways that ditto and cp behave can cause confusion. Both create the target directory when it does not exist. When the target directory does exist, ditto performs the copy in exactly the same way. The cp command, however, creates a new directory within an existent target directory, named after the source directory, and copies into that instead. If you've installed Apple's Developer Tools, you'll find a command called CpMac, which copies files preserving resource forks and attributes just as ditto does. $ /Developer/Tools/CpMac usage: CpMac [-r] [-p] <source-path> <dest-path> CpMac [-r] [-p] <source-path>... <dest-dir> CpMac has a Unix man page, but remember to type man CpMac and not man cpmac. Snap with Screen CaptureThe screencapture command provides functionality equivalent to pressing Command-Shift-4, taking a snapshot of the screen. It has many options, including an interactive mode, but of greatest use for the command line and scripting is its "hands-free" mode, in which it operates without user intervention. To take a screen shot, specify option -x to silence the sound effects and provide a filename. For example: $ screencapture -x ~/Desktop/p1.png The default image format is PNG (Portable Network Graphics)hence, our chosen file extension. Other useful options include -C to capture the cursor and -c to capture to the clipboard (in which case a filename is not necessary). Also, change the format of captured images by passing the -t option. Here are some examples, in which you can guess the chosen image format. $ screencapture -xC -ttiff ~/Desktop/p1.tiff $ screencapture -xC -tjpeg ~/Desktop/p1.jpg $ screencapture -xC -tpdf ~/Desktop/p1.pdf To check that the file format is as expected, use the file command. $ file ~/Desktop/p1.tiff /Users/saruman/Desktop/p1.tiff: TIFF image data, big-endian Just for fun, here's a trick in which we take a snapshot of the login screen while logged out. Issue the following command and then log out, allowing Terminal to terminate. Make sure that the login screen is showing 60 seconds after issuing the commands. $ sudo -s Password: (admin password here) $ nohup bash -c "sleep 60; ¬ screencapture ~/Pictures/screen.pdf" To create time-delayed screen shots, use screencapture in combination with the at command (see Project 70). Manage HFS+ File AttributesIf you've installed Apple's Developer Tools, you'll have two commands that view and set HFS+ file attributes. Such attributes include a file's Mac OS type and creator codes, and the hide-file-extension attribute (where the Finder hides the file's extension). We won't explain the meanings of HFS+ attributes; we'll just present examples showing how you might view and set them. To display a brief usage summary, issue the SetFile command with no parameters. $ /Developer/Tools/SetFile Usage: SetFile [option...] file... -a attributes # attributes (lowercase = 0, uppercase = 1)* ... E Hidden extension* ... Z Busy* ... Display the HFS+ attributes for a file named p1.jpg by typing $ /Developer/Tools/GetFileInfo p1.jpg file: "/Users/saruman/Desktop/p1.jpg" type: "" creator: "" attributes: avbstclinmEdz created: 10/17/2005 10:11:12 modified: 10/17/2005 10:11:12 The capital E in the attributes list tells us that the extension is hidden (the hide-file-extension attribute is switched on), and the filename shows as p1 in the Finder. To make the extension visible, type $ /Developer/Tools/SetFile -a e p1.jpg A lowercase letter e switches off the hide-file-extension attribute. View the file in the Finder, and you'll see that its name now shows as p1.jpg. (You might have to click the file icon to make the Finder refresh before the extension shows.) To switch the attribute back on and hide the extension, type $ /Developer/Tools/SetFile -a E p1.jpg Read and Set Preferences FilesYour personal preference settings are stored in plist files (files with the extension .plist) in the directory ~/Library/Preferences. System preferences applicable to all users are in the directory /Library/Preferences. In versions of Mac OS X before 10.4 (Tiger), preferences are stored in XML (eXtensible Markup Language) format. Preferences in Tiger are stored in binary format. Tip
It's possible to examine and change preferences settings from the command line with the defaults command. To examine preferences, such as your personal preferences settings for iChat, type $ defaults read ~/Library/Preferences/com.apple.iChat { ABDirectoryResultColumnTitle = "Instant Messaging"; AccountSortOrder = ("90AFA166-D274-4514-9358-136AEF... AutoReplyWithAway = 0; AutosaveChats = 1; AwayOnFastUserSwitch = 1; BuddyInfoSelectedTab = 0; "BuddyList.EnableGroups" = 0; "BuddyList.Visible" = 1; ... (Notice that we don't include the extension in the filename given to defaults.) Most of the preferences settings can be related to those in the application's Preferences window, accessed from its Preferences menu. In the next example, we set the preference AwayOnFastUserSwitch to 0 (off or FALSE). $ defaults write ~/Library/Preferences/com.apple.iChat ¬ AwayOnFastUserSwitch 0 You'll have to explore preferences files to determine what settings an application has and what values they can be set to. Manage Preferences FilesUse the plutil command to check and convert preferences files. The next example checks all preferences files in the directory ~/Library/Preferences for signs of corruption. Specify the -lint option to plutil and option -s to report only on preferences files that fail the check. $ plutil -lint -s ~/Library/Preferences/*.plist 2005-10-17 11:57:22.847 plutil[6653] CFLog (0): CFPropertyListCreateFromXMLData(): plist parse failed; the data is not proper UTF-8. The file name for this data could be: com.apple.help.plist The parser will retry as in 10.2, but the problem should be corrected in the plist. com.apple.help.plist: Conversion of data failed. The file is not UTF-8, or in the encoding specified in XML header if XML. This command provides a handy integrity check of application preferences, to be run when applications start to act strangely or fail to start up. To convert a preferences file's contents between XML and the new Tiger binary format, specify the -convert option. We might want to make a binary Tiger preferences file readable in a text editor by converting it to XML. $ plutil -convert xml1 ¬ ~/Library/Preferences/com.apple.TextEdit.plist A Tiger application can read both formats, but tests show that applications rewrite their preferences files in binary format. To convert back to binary format manually, type $ plutil -convert binary1 ¬ ~/Library/Preferences/com.apple.TextEdit.plist |