Chapter 18. Hacking Mac OS X

 <  Day Day Up  >  

17.5. Putting It Together

All of the Unix syntax and vocabulary presented in this chapter and Chapter 16 is all well and good, and it'll give you the rosy glow of having mastered something new. But it still doesn't entirely explain why Unix gives programmers sweaty palms and dilated pupils.

The real power of Unix comes farther down the road ”when you start stringing these commands together.

Suppose, for example, you want to round up all the TIFF image files related to your Yosemite project, scale them to a common size , convert them to JPEG files, and copy them to an FTP site. How would you go about it?

You could, of course, use Spotlight to search for all TIFF files that have "Yosemite" in their names. But what if your images were named otherwise but kept in folders with Yosemite in their names ? You would have to find those folders first, and then the TIFF files within them.

You could perform the next step (scaling and converting the image) either manually or by a preprogrammed script, using a program like Photoshop or even iPhoto. Once the images were all done, you'd need to collect them and then use your favorite FTP program to upload them to the server.

If you've mastered Unix, though, you could shave 12 minutes off of your workday just by changing to an empty working directory and typing this as one long line:

 find ~ -type f -ipath '*yosemite*tif' -print0  xargs -0 sips -Z 250 -s for- mat jpeg out . ; ftp -u ftp://ftp. coast-photo.com/Incoming * 

Even after two chapters of Unix basics, that mass of commands probably looks a tad intimidating. And, indeed, if you've never programmed before, even the following breakdown may make your eyes glaze over. Even so, pieces of it should now look familiar:

  • find ~ -type f -ipath '*yosemite*tif' -print0 . This segment searches your Home directory (~) for files ( -type f ) whose pathnames ( -ipath , meaning "capitalization doesn't matter") contain the word Yosemite and end in tif . Remember, the asterisks here are wildcard characters, which stand for "any number of characters ."

    The command so far makes a list of all matching files, which it keeps in its little head.

    The -print0 command formats this list of found files' pathnames, separating them with the null character (a special character that programmers use to indicate where one string of text ends and another begins) instead of the usual spaces. It lets the command work with pathnames that contain spaces, a common occurrence on Macs (but a rarity in Unix). You'll see how it does this shortly.

    Then comes the pipe (the vertical bar), which you can use to direct the results (output) of one command into the input of another. In this case, it sends the list of found pathnames on to the next command.

  • xargs -0 sips -Z 250 -s format jpeg ”out . ; . xargs is an argument builder. In this case, it builds an argument from the list of files it received from the find command and provides it to sips for processing. In other words, xargs hands a list of files to sips , and sips runs the same command on each one.

    The -0 flag tells xargs that the pathnames are separated by the null character. (Otherwise, xargs would separate pathnames at each space character, which would choke sips .)

    GEM IN THE ROUGH
    The Famous Animated-Desktop Trick

    It was one of the first great Mac OS X hacks to be passed around the Internet: the classic "screen-saver-on-the desktop" trick. In this scheme, your desktop shows more than some wussy, motionless desktop picture. It actually displays one of the Screen Effects animation modules.

    Start by choosing the screen saver module you prefer, using the Screen Effects panel of System Preferences. (The one called "flurry" makes a good choice.)

    Then, in Terminal, type: /System/Library/Frameworks/ ScreenSaver.framework/Resources/ScreenSaver Engine.app/Contents/MacOS/ScreenSaverEngine - background &

    and then press Enter. (Note that there are no spaces or Returns in the command, even though it appears broken onto more than one line here.)

    Presto: The active screen saver becomes your desktop picture! Fall back into your chair in astonishment.

    Once you've regained your composure , look in the Terminal window again. The number that follows the [1] in the following line is the process ID of your background desktop program.

    You'll need that number when it comes time to turn off the effect, which is a good idea, since the desktop/screen saver business drains a massive amount of your Mac's processing power. The whole thing is a gimmicky showoff stunt you'll generally want to turn off before conducting any meaningful work.

    To turn off this effect, type kill 496 (or whatever the process ID is) and then press Enter.

    And if you get tired of typing out that long command, download xBack from www.gideonsoftworks.com/xback.html. It's a simple piece of shareware that lets you turn this effect, plus many additional options, on and off with the click of a mouse.


    For each file it gets, sips first scales the image's largest dimension to 250 pixels, and its other dimension proportionally. (That way, any image will fit into a 250 x 250 “pixel box on a Web page, for example.)

    sips then sets the format ( -s format ) of the image to JPEG and saves it, with the correct .jpg extension, in the working directory (indicated by the single dot that follows ” out ).

    The semicolon at the end of this fragment tells the shell to run the next command when it's finished with the previous one. So, once sips is done with each file it gets from xargs , the shell moves on to this:

  • ftp -u ftp://ftp.coast-photo.com/Incoming * . The ftp utility included with Tiger can work a lot like curl in the way it can upload and downloads files with a single command. In this case, the command uploads ( -u ) every file from the working directory (as specified by the asterisk ”that is, all of the sips - processed files) to the Incoming directory of the coast-photo.com FTP site.


Note: As written, this command works only if you don't need a password to get into the FTP site. Otherwise, build your FTP account name and password into its address like this: ftp://chris:password@ftp.coast-photo.com/Incoming.

When you press Return or Enter after this gigantic command, Mac OS X scans all the directories inside your Home directory, rounds up all the Yosemite-related images, scales them, converts and renames them, and then uploads each to the FTP directory.

Once you've gained some experience with Unix commands and programs like these, you'll find it fairly easy to adapt them to your own tasks . For example, here's a more versatile command that searches a directory called Projects for all TIFF files modified after 6:00 that morning, converts them to thumbnail- sized jpegs, and then plops them into the images directory of your FTP-accessible Web server:

 find ~/Projects -type f -iname *tif - newermt 6:00 -print0  xargs -0 sips -Z 128 -s format jpeg out . ; ftp -u ftp:// carlos:birdie@ftp.coast-photo.com/ht- docs/images * 

Of course, not everyone's going to want to type out that entire command line every time. Fortunately, you don't have to; having read the box on Section 17.1.6, you know that you can just save the whole thing as a .command file ”and run it thereafter just by double-clicking.

Trouble is, a new Terminal window comes with the working directory set to your Home folder ”and you don't want your .command file to upload your entire life! Ideally, then, you should build a "change directory" command into the .command file. Change Terminal's attention to, say, a new, empty directory, which collects the processed files. Next, you should include a final move command that empties that directory when the rest of the command is done. That way, you'll again have an empty working directory for the next time you run the following command:

 cd ~/Stage ; find ~/Projects -type f -iname *tif -newermt 6:00 -print0  xargs -0 sips -Z 128 -s format jpeg out . ; ftp -u ftp://carlos:birdie@ ftp.coast-photo.com/htdocs/images * ; mv * ~/Backup/ 

With just a few more keystrokes, you could modify that command to collect some files, lock them, and place copies of each in every account holder's Home directory, as well as several different servers at the same time. What's more, it emails you a report when it's done. Using launchd , you could even configure this routine to trigger itself automatically every day at 11:00 p.m. Considering the hundreds of Unix programs included with Mac OS X and the thousands of others available on the Internet, the possibilities are limitless.

For some guidance in picking up your Unix career from here, see Appendix E.

 <  Day Day Up  >  


Mac OS X. The Missing Manual
Mac OS X Snow Leopard: The Missing Manual (Missing Manuals)
ISBN: 0596153287
EAN: 2147483647
Year: 2005
Pages: 506
Authors: David Pogue

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