05.01.2013 Views

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

330<br />

CHAPTER 19 EXTENDING THE POWER OF DARWIN<br />

1: #!/bin/sh<br />

2:<br />

3: # togglevis<br />

4: # A shell script to toggle the visibility of hidden files in the Finder<br />

5:<br />

6: set `ps acx | grep Finder`<br />

7:<br />

8: show=`defaults read com.apple.finder AppleShowAllFiles`<br />

9:<br />

10: if [ $show -eq 1 ]; then<br />

11: defaults write com.apple.finder AppleShowAllFiles 0<br />

12: kill $1<br />

13: else<br />

14: defaults write com.apple.finder AppleShowAllFiles 1<br />

15: kill $1<br />

16: fi<br />

NOTE For those of you who really hate line numbers in programs because they prevent you<br />

from copying the text out of an e-book and running it, later in this chapter there will be a Perl<br />

script that you should be able to tweak (when you are done with this chapter) to strip them out<br />

automatically.<br />

This script will toggle the visibility of hidden items in the <strong>Mac</strong> <strong>OS</strong> X Finder, which is done<br />

by setting the hidden Finder preference AppleShowAllFiles to 1 (to show all files) or 0 (to hide<br />

hidden files) and then restarting the Finder to immediately enact the change.<br />

NOTE Truth be told, this script doesn’t exactly restart the Finder. The script instead just kills<br />

the Finder. <strong>OS</strong> X, depending on the Finder, notices this and restarts the Finder for you.<br />

The first line, #!/bin/sh, often referred to as the interpreter line, is important for all scripts<br />

because it points the shell to the executing script interpreter.<br />

NOTE On <strong>Leopard</strong>, /bin/sh and /bin/bash are both Bash shell executables. Most shell scripts<br />

in <strong>OS</strong> X (and generally in Unix) are written for the Bourne shell (sh) for compatibility across various<br />

Unix platforms (of which almost all have the Bourne shell installed). The Bash shell itself<br />

(a.k.a. Bourne Again SHell) is 100 percent backward compatible with the Bourne shell, with<br />

some extra features borrowed from the C shell and Korn shell. The general scripting syntax we<br />

are covering in this chapter is mostly Bourne shell compatible; however, there may be certain<br />

commands issued inside a script that are specific to <strong>OS</strong> X.<br />

Lines 3 and 4, like all script lines beginning with # (except the sh-bang line), are comments.<br />

These are included to provide a clue about what’s going on for anyone reading the script. Comments<br />

have no effect on how the script runs and in this respect are purely optional elements. It is<br />

a good habit to use comments, not only for others who may be looking at your script but also for<br />

yourself should you have to look at a script weeks, months, or even years after you wrote it.<br />

Here the first comment line gives the name of the script, and the second comment describes what<br />

the script does.<br />

Line 6 shows us two things, the use of the set command and the use of backticks (`) in<br />

scripts. In a shell script, anything within backticks is executed, and the result is passed to the<br />

script (this is called command substitution). The set command sets the arguments used in the<br />

script just as if they were entered at the command line. So, line 6 takes the results from the ps acx<br />

| grep Finder command (which would be something like 261 ?? S 0:00.93 Finder) and<br />

makes them our arguments (of which there would be 5). The importance of line 6 for the rest of<br />

the script is that it specifically assigns the PID of the Finder to our first argument that can then<br />

be accessed as the variable $1.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!