Toggling File Visibility in the Finder via AppleScript
3 Comments Published April 10th, 2003 Tagged with: applescript × file visibility × finder × invisible files × mac os xIn using Mac OS X over the past two years, I have from time to time wished I could quickly switch my view of files in the Finder from hiding hidden files to showing them, and back again. Often, instead of going the cumbersome route of resetting the visibility settings in SystemPreferences and then restarting the Finder, I have just fired up a Terminal window in the directory and done a ‘ls -al’.
Now, however, thanks to a thread begun by “frogstomp” at <http://www.macosxhints.com/article.php?story=20030409015020645>, I have put together a little AppleScript that does this quickly and easily from within the Finder. You can add to the sweetness by launching the script via a quick keystroke by using a macro program like HotApp or QuickKeys to bind a command key sequence to the script. Enjoy!
Here’s the script:
set onOff to do shell script "defaults read com.apple.finder AppleShowAllFiles"
if onOff = "NO" or onOff = "OFF" or onOff = "0" then
set newState to "show"
set OnOffCommand to "defaults write com.apple.finder AppleShowAllFiles ON"
else
set newState to "hide"
set OnOffCommand to "defaults write com.apple.finder AppleShowAllFiles OFF"
end if
display dialog "Are you sure you want to " & newState & " hidden files? (This will restart the Finder.)" buttons {"Cancel", "OK"} default button 2
copy the result as list to {buttonPressed}
if the buttonPressed is "OK" then
try
tell application "Finder" to quit
do shell script OnOffCommand
delay 1
tell application "Finder" to launch
end try
end if
The line in the script above starting with ‘display dialog’ should be on one line in the script. If it is probably soft-wrapped in your browser.
To install the script, do one of the following:
- Paste the above script into your ScriptEditor and save it as a compiled script (or run only script, if you prefer) to your ~/Scripts folder, or into the main Finder scripts folder at /Library/Scripts/Finder Scripts.*
- Download the script, unstuff it, and put it in ~/Scripts or /Library/Scripts/Finder Scripts.
You can then attach a keystroke to the script via a macro utility like HotApp, or QuickKeys, or you can access it via your scripts menu item (assuming you’ve installed the scripts menu extra item**).
When launched, the script will ask for confirmation that you want to toggle the visibility of hidden files, will quit the Finder, rewrite Finder preferences to the new state, and relaunch the Finder. Just relaunch to set the state back to where you were before.
* If you have trouble with the cut and paste method, try the stuffed file. Most likely you are having trouble with line breaks or character encoding.
**To install the script menu extras, go to /Applications/AppleScript/ and drag the ‘Script Menu.menu’ folder to your menu bar.
A better version of the script.
--Bruce Philips
--macscripter.net
try
do shell script "/usr/bin/defaults read com.apple.Finder AppleShowAllFiles"
get result as integer
on error
get 0
end try
set i to (result + 1) mod 2 -- Get opposite of current value
tell application "Finder"
activate
get item (i + 1) of {"Hide", "Show"}
display dialog (result & " all hidden files in Finder?") buttons {"Cancel", (result & " Hidden Files")} default button 2 with icon note
quit
end tell
try
do shell script "/usr/bin/defaults write com.apple.Finder AppleShowAllFiles " & i
launch application "Finder"
on error errorMsg number errorNum
display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1 with icon caution
end try
The the script posted by kristofer produces errors when run in OS X v10.5; the most serious of these is that the shell script as written doesn’t seem to accept the options of 0 or 1, but rather only seems to work when the string “ON” is used.
Thus, if one wishes, the prettier dialogs of his script as well as the error checking can be rather easily incorporated into the original, which runs very quickly on my MB-Pro:
—-
set onOff to do shell script “defaults read com.apple.finder AppleShowAllFiles”
if onOff = “NO” or onOff = “OFF” or onOff = “0″ then
set newState to “SHOW”
set OnOffCommand to “defaults write com.apple.finder AppleShowAllFiles ON”
else
set newState to “HIDE”
set OnOffCommand to “defaults write com.apple.finder AppleShowAllFiles OFF”
end if
display dialog “Do you want to ” & newState & ” invisible files? (This will restart the Finder.)” buttons {“Cancel”, (newState & ” Invisible Files”)} default button 2 with icon note
copy the result as list to {buttonPressed}
if the buttonPressed is not “Cancel” then
try
tell application “Finder” to quit
do shell script OnOffCommand
delay 1
tell application “Finder” to launch
on error errorMsg number errorNum
display dialog “Error (” & errorNum & “):” & return & return & errorMsg buttons “Cancel” default button 1 with icon caution
end try
end if
Thanks for the suggestions and improvements, guys! Great stuff.
[Update: There is a good thread on this topic at macscripter.net.]