DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Show/Hide Hidden Files
// for toggling invisible files on and off in the finder
-- found this script here: http://forums.macosxhints.com/showpost.php?p=251001&postcount=3
-- i added the "delay 5" because after running script, finder didn't relaunch, maybe that's not needed on a faster machine
-- it's for toggling invisible files on and off in the finder, use for when i need to copy .htaccess files via a GUI
on run
set hiddenState to (do shell script "/usr/bin/defaults read com.apple.Finder AppleShowAllFiles")
try
if hiddenState = "0" or hiddenState contains "does not exist" then
set showPrompt to display alert "Are you sure you want to show all hidden files?" message "The Finder will quit and relaunch afterwards." buttons {"Cancel", "Show Hidden Files"} default button "Show Hidden Files"
if button returned of showPrompt = "Show Hidden Files" then
tell application "Finder" to quit
do shell script "/usr/bin/defaults write com.apple.Finder AppleShowAllFiles 1"
delay 5 --give finder time to quit so it can be relaunched below
else if button returned of showPrompt = "Cancel" then
return 0
end if
else if hiddenState = "1" then
set showPrompt to display alert "Are you sure you want to hide all hidden files?" message "The Finder will quit and relaunch afterwards." buttons {"Cancel", "Hide Hidden Files"} default button "Hide Hidden Files"
if button returned of showPrompt = "Hide Hidden Files" then
tell application "Finder" to quit
do shell script "/usr/bin/defaults write com.apple.Finder AppleShowAllFiles 0"
delay 5 --give finder time to quit so it can be relaunched below
else if button returned of showPrompt = "Cancel" then
return 0
end if
end if
on error theError
FinderCheck()
display alert theError as critical message "Changes may or may not have been made."
end try
FinderCheck()
end run
on FinderCheck()
tell application "System Events"
if (name of every process) does not contain "Finder" then
tell application "Finder" to launch
end if
end tell
end FinderCheck






Comments
Jasper Blues replied on Thu, 2010/09/23 - 12:31am
Snippets Manager replied on Mon, 2012/05/07 - 2:17pm