Jon Knapp (.com)

I'm a fan of "web development" on Facebook. Read more »

Toggle hidden file viewing in Mac OS X { 1 }

My one regret with OS X’s Finder is that I have found no easy way to toggle viewing hidden files and folders. In fact I was so perturbed that I went and wrote a small application to toggle hidden file and folder visibility when ran.

There are a couple of sites out there that show the way to do it, but none are that graceful. They require the user to force restart Finder (which is not desirable) and they usually call upon the use of Terminal and typing out the full command.

Not satisfied with a simple answer, and trying to avoid actual work, I decided to write up an AppleScript that would toggle the visibility of hidden files and folders from “on” to “off” every time you clicked it. I then packaged it up in a pretty little application, and even gave it a custom small icon so you can drag it to the right side of current icons in a Finder window and it looks pretty sharp. I got the icon idea from here.

Here’s the application! Hope it helps you out like it did for me.

And for those out there that like code:

# Jonathan Knapp
# Caffeinated Solutions
# www.coffeeandcode.com

# This application toggles the visiblity of hidden files and folders in Mac OSX.
set userCancelled to false
try
	# give the user a warning about the negative effects of restarting Finder
	set alertResult to display alert "Warning! Restarting Finder can have bad results on opened files. Are you sure you want to continue?" buttons {"Sure!", "Not really..."} as warning default button "Not really..." cancel button "Not really..."
on error number -128
	# user hit the cancel button
	set userCancelled to true
end try

# if the user wants to proceed...
if userCancelled is false and button returned of alertResult is "Sure!" then
	try
		# get the current value; an error is thrown if none exists
		set currentValue to do shell script "defaults read com.apple.finder AppleShowAllFiles"
		if currentValue is "TRUE" then
			set currentValue to "FALSE"
		else
			set currentValue to "TRUE"
		end if
	on error
		# there are a couple errors that could catch here if the AppleShowAllFiles has no current value (ran for the first time)
		set currentValue to "TRUE"
	end try

	do shell script "defaults write com.apple.finder AppleShowAllFiles " & currentValue
	do shell script "osascript -e 'tell application \"Finder\" to quit'"
	do shell script "osascript -e 'tell application \"Finder\" to activate'"
end if

1 Comment For This Post

  1. cheung 30 October 2008 at 4:04 pm #

    hey man, nice bit of code. I can’t say I’ve really got a need to see my hidden files, but cool any way.

    I was actually just thinking about you, dood. I was just playing with a bit of code that switched my sleep mode on leopard to the “hibernation” mode that XP uses. I was thinking about what a pain in the ass it was to have to use terminal everytime I wanted to switch between the two. My initial thought was that this was the perfect excuse I needed to play with writing a short AppleScript prog but I wasn’t sure even where to start.

    Oh yea, when are you going to get around to making your site iPhone compatible? . )

Trackbacks/Ping

Leave a Reply