Jon Knapp (.com)

Interested? jon@coffeeandcode.com | 330.249.1007

byte artist Read more »

Toggle hidden file viewing in Mac OS X { 3 }

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:
[sourcecode language="sh"]
# 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
[/sourcecode]

3 Comments 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? . )

  2. cheung 26 May 2011 at 1:50 pm #

    Here’s an update incase anyone stumbles onto this page. I was messing around with making an Automator service so you could just right click in a Finder window and show or hide the hidden files/folders. Here’s a link to the website where most of the code came from:

    http://gigaom.com/apple/quick-tip-showhide-hidden-files/

    But with their’s you needed two services and I wanted more of a toggle one, so I wrote up my own script. You can follow their directions, except in Snow Leopard you just “Save as” rather than “Save as plugin”, and paste in my code below rather than their’s:

    VALUE=$(defaults read com.apple.finder AppleShowAllFiles).
    if [ "$VALUE" = FALSE ]; then.
    defaults write com.apple.finder AppleShowAllFiles TRUE.
    elsedefaults write com.apple.finder AppleShowAllFiles FALSE.fiosascript -e ‘tell application “Finder” to quit’;.osascript -e ‘tell application “Finder” to activate’;.

    Also, their code used the bash “killall” command which made you lose your open Finder window, but mine is a bit more subtle and just closes and reopens Finder allowing you to keep your place in Finder.

    The only weird thing I’ve noticed is that my Finder icon in the dock seems to get translucent like it’s hidden. I’m not sure why…

  3. cheung 26 May 2011 at 1:55 pm #

    Not sure why but the previous post chose to ignore a few of the line breaks in my code. I’m not sure if it would mess it up or not, but here it is again (hopefully it works):

    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px ‘Andale Mono’}

    VALUE=$(defaults read com.apple.finder AppleShowAllFiles).
    if [ "$VALUE" = FALSE ]; then.
    defaults write com.apple.finder AppleShowAllFiles TRUE.
    else
    defaults write com.apple.finder AppleShowAllFiles FALSE.
    fi
    osascript -e ‘tell application “Finder” to quit’;.
    osascript -e ‘tell application “Finder” to activate’;.

Trackbacks/Ping

Leave a Reply

You must be logged in to post a comment.