PDA

View Full Version : Toggle Hidden Files Mac OS X




Omniwheel
Jul 7, 2008, 10:59 PM
Hi all,

I have been using Ubuntu for 2 years now, and a few days ago I bought a Macbook. This is my first time using a Mac, so I have spent the better of two days learning the user interface, and configuring my Macbook. One thing I noticed is that there is no easy way to turn on and off hidden files, so I wrote a shell script to make it easy.

This is my first time ever making a script (I have previously never written a script on any OS), I hope you guys can comment on it, tell me whats good, bad, and how it can be done differently.

Specifically:

-Why does my terminal not close after the script is complete?

-Whats the best way to restart an application, ie one example has killall, the other uses osascript and envokes the quit command?

-Which application is safer?

-I named these files .command files so they can execute on double click, is the the best way to do it?

-Why do I have to execute 'open -a Finder' twice?



Example 1
#!/bin/sh
# This Application Makes Hidden Files Visible
# Note: The Application Will Close All Finder Windows
echo Warning This Application Will Close All Finder Windows!
echo Type y/n to Continue?
read answer
if [ "$answer" = "y" ]
then
filesVisible=$(defaults read com.apple.finder AppleShowAllFiles)
if [ "$filesVisible" = "0" ]
then
defaults write com.apple.finder AppleShowAllFiles -bool true
killall Finder
else
defaults write com.apple.finder AppleShowAllFiles -bool false
killall Finder
fi
else
exit
fi
exit




Example 2
#!/bin/sh
# This Application Makes Hidden Files Visible
# Note: The Application Will Close All Finder Windows
echo Warning This Application Will Close All Finder Windows!
echo Type y/n to Continue?
read answer
if [ "$answer" = "y" ]
then
filesVisible=$(defaults read com.apple.finder AppleShowAllFiles)
if [ "$filesVisible" = "0" ]
then
defaults write com.apple.finder AppleShowAllFiles -bool true
osascript -e 'tell app "Finder" to quit'
open -a Finder
open -a Finder
else
defaults write com.apple.finder AppleShowAllFiles -bool false
osascript -e 'tell app "Finder" to quit'
open -a Finder
open -a Finder
fi
else
exit
fi
exit


Thanks,
Omniwheel