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

Snippets has posted 5883 posts at DZone. View Full User Profile

Show/Hide Hidden Files

12.11.2005
| 15261 views |
  • submit to reddit
        // 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

Thanks - still useful in 2010 ;) Ran it through Quicksilver with a Cmd+H trigger, if Finder is the current app. Now it works just like Ctrl+H in Gnome's Nautilus. Added the following in FinderCheck() to restore window focus: tell application "Finder" to activate

Snippets Manager replied on Mon, 2012/05/07 - 2:17pm

Sweet, thanks!