05.01.2013 Views

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Line 8 sets the variable show to the result of the defaults read com.apple.finder AppleShowAllFiles<br />

command. defaults provides a way to access and alter <strong>Mac</strong> <strong>OS</strong> X user default settings from<br />

the command line. In this case, we use it to read the AppleShowAllFiles value stored in the<br />

com.apple.finder preference to determine the existing setting (0 or 1) so we can then toggle the<br />

setting. If the AppleShowAllFiles value doesn’t exist yet in your Finder preference file, you will<br />

get an error similar to this:<br />

2007-06-26 12:24:00.175 defaults[1180]<br />

The domain/default pair of (com.apple.finder, AppleShowAllFiles) does not exist<br />

togglevis.sh: line 10: [: ==: unary operator expected<br />

In this case you can ignore this, since the script will continue and actually create this value.<br />

However, if you continue to get errors after this (or if you get a vastly different error), then something<br />

else is wrong, and you should check your script closely.<br />

Lines 10 through 16 provide a conditional if...then...else statement and provide the<br />

expected actions based on the condition. First, in line 10 we check to see whether the show variable<br />

(which is the set value of com.apple.finder AppleShowAllFiles) is 1; if it is, then we move on<br />

to line 11 where we change com.apple.finder AppleShowAllFiles to 0. Then in line 12 we kill the<br />

Finder using $1, which we set in line 6. If the condition in line 10 is false (the show variable is not<br />

1), then we move on to the else part of the script, and in line 14 we set com.apple.finder<br />

AppleShowAllFiles to 1 and then kill the Finder. Line 16 ends the if...then...else statement<br />

with fi (that’s if backward).<br />

The script ends when it runs out of things to do.<br />

There are a few ways to run this script: one is to pass the script to the shell executable as an<br />

argument, and the other is to make the actual script executable. Passing the script as an argument<br />

to the shell looks like this:<br />

leopard:~/bin scott$ sh togglevis<br />

If you want to have access to the script easily from anywhere, though, you must first make<br />

the script executable and make sure it’s in a directory that is included in your $PATH (as discussed<br />

in the previous chapter). For example, we’ve placed this file in our own bin directory, which you<br />

can see is set in our path with the following statement:<br />

leopard:~/bin scott$ echo $PATH<br />

/opt/local/bin:/Users/scott/bin:/usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin<br />

To make it executable, we use the chmod command:<br />

leopard:~/bin scott$ chmod u+x togglevis<br />

Now provided the script is in your path, you can use togglevis as you would any command<br />

from the command line.<br />

Variables<br />

Variables in shell scripts are generally reserved for use from within the script itself unless they are<br />

explicitly exported as environmental variables. Environmental variables (which include things<br />

like $PATH) are variables that are available to all shell scripts and programs, where shell variables<br />

are available only within your script.<br />

Setting a variable is quite easy; just create a variable name, and set its value like this:<br />

aVariable=Value<br />

CHAPTER 19 EXTENDING THE POWER OF DARWIN 331

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

Saved successfully!

Ooh no, something went wrong!