Chapter 11: Mac OS X Scripts


One of the most important changes in the world of Unix and Unix-like operating systems was the release of the completely rewritten Apple Mac OS X system. Jumping from the older Mac OS 9, Mac OS X is built atop a solid and reliable Unix core called Darwin. Darwin is an open source Unix based on BSD Unix, and if you know your Unix at all, the first time you open the Terminal application in Mac OS X you'll doubtless gasp and swoon with delight. Everything you'd want, from development tools to standard Unix utilities, is included with the latest generation of Macintosh computers, with a gorgeous GUI quite capable of hiding all that power for people who aren't ready for it.

There are some significant differences between Mac OS X and Linux/Unix, however, not the least of which is that Mac OS X uses a system database called NetInfo as a replacement for a number of flat information files, notably /etc/passwd and /etc/aliases . This means that if you want to add a user to the system, for example, you have to inject his or her information into the NetInfo database, not append it to the /etc/passwd file.

Additional changes are more on the fun and interesting side, fortunately. One tremendously popular Mac OS X application that many people adore is iTunes , an elegant and powerful MP3 player and online radio tuner application. Spend enough time with iTunes, though, and you'll find that it's very hard to keep track of what songs are on your system. Similarly, Mac OS X has an interesting command-line application called open , which allows you to launch graphical ("Aqua" in Mac OS X parlance) applications from the command line. But open could be more flexible than it is, so a wrapper helps a lot.

There are other Mac OS X tweaks that can help you in your day-to-day interaction. For example, if you work on the command line with files created for the GUI side of the Macintosh, you'll quickly find that the end-of-line character in these files isn't the same as the character you need when working on the command line. In technical parlance, Aqua systems have end-of-line carriage returns (notationally, an \r character), while the Unix side wants newlines (an \n ). Instead of a file in which each line is displayed one after the other, a Mac Aqua file will show up in the Terminal without the proper line breaks. Have a file that's suffering from this problem? Here's what you'd see if you tried to cat it:

  $ cat mac-format-file.txt   $  

Yet you know there's content. To see that there's content, use the -v flag to cat , which makes all otherwise hidden control characters visible. Suddenly you see something like this:

  $ cat -v mac-format-file.txt  The rain in Spain^Mfalls mainly on^Mthe plain.^MNo kidding. It does.^M $ 

Clearly there's something wrong! Fortunately, it's easy to fix with tr :

 $  tr '\r' '\n' < mac-format-file.txt > unix-format-file.txt  

Once this is applied to the sample file, things start to make a lot more sense:

 $  tr '\r' '\n' < mac-format-file.txt  The rain in Spain falls mainly on the plain. No kidding. It does. 

If you open up a Unix file in a Mac application like Microsoft Word and it looks all wonky, you can also switch end-of-line characters in the other direction ” toward an Aqua application:

  $ tr '\n' '\r' < unixfile.txt > macfile.txt  

One last little snippet before we get into the specific scripts for this chapter: easy screen shots in the world of Mac OS X. If you've used the Mac for any length of time, you've already learned that it has a built-in screen capture capability that you access by pressing CMD-SHIFT-3. You can also use the Mac OS X utility Grab located in the Applications/Utilities folder, and there are some excellent third-party choices, including Ambrosia Software's Snapz Pro X , which I've used for the screen shots in this book.

However, did you know that there's a command-line alternative too? There's no man page for it, but screencapture can take shots of the current screen and save them to the Clipboard or to a specific named file (in JPEG or TIFF format). Type in the command without any arguments, and you'll see the basics of its operation:

 $  screencapture  screencapture: illegal usage, file required if not going to clipboard usage: screencapture [-icmwsWx] [file] [cursor]   -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   file    where to save the screen capture 

This is an application begging for a wrapper script. For example, to take a shot of the screen 30 seconds in the future, you could use

 $  sleep 30; screencapture capture.tiff  

But what if you wanted to take a series of screen shots, spaced one minute apart? A simple loop would work:

 maxshots=60; counter=0 while [ $counter -lt $maxshots ] ; do   screencapture capture${counter}.tiff   counter=$((counter + 1))   sleep 60 done 

This will take a screen shot every 60 seconds for 1 hour , creating 60 rather large TIFF files, over 1.5MB each, sequentially numbered capture1.tiff , capture2.tiff , ... capture60.tiff . This could be very useful for training purposes, or perhaps you're suspicious that someone has been using your computer while you're at lunch : Set this up, and you can go back and review what occurred without anyone ever knowing.

Let's look at some more complex scripts for Mac OS X.

Wicked Cool Shell Scripts. 101 Scripts for Linux, Mac OS X, and Unix Systems
Wicked Cool Shell Scripts
ISBN: 1593270127
EAN: 2147483647
Year: 2004
Pages: 150
Authors: Dave Taylor

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: