Posts RSS Comments RSS 48 Posts and 155 Comments till now

Viewing or hiding hidden files in the Finder using AppleScript

Mac OS X has a lot of files that are hidden from view in the Finder. They are hidden for a very good reason, which is most of them are system files that you don’t need to mess with if you want your computer to keep working well. However, there are occasions where it would nice to be able to view these files in the Finder.

This script lets you toggle the view on and off. So, you can turn it on, do what you need and then turn it off.

try
	set toggleView to (do shell script "defaults read com.apple.Finder AppleShowAllFiles")
on error
	set toggleView to "NO"
end try
if toggleView is "NO" or toggleView is "0" then
	set br to display dialog "Showing hidden files is disabled." & return & "Would you like to enable it?" buttons {"Yes", "No"} default button {"No"}
	set theAnswer to the button returned of br
	if theAnswer is "Yes" then
		do shell script "defaults write com.apple.Finder AppleShowAllFiles YES"
		do shell script "killall Finder"
		display dialog "Showing hidden files has been enabled" buttons {"Ok"} giving up after 5
	end if
else
	if toggleView is "YES" or toggleView is "1" then
		set br to display dialog "Showing hidden files is enabled." & return & "Would you like to disable it?" buttons {"Yes", "No"} default button {"Yes"}
		set theAnswer to the button returned of br
		if theAnswer is "Yes" then
			do shell script "defaults write com.apple.Finder AppleShowAllFiles NO"
			do shell script "killall Finder"
			display dialog "Showing hidden files has been disabled" buttons {"Ok"} giving up after 5
		end if
	end if
end if

Copy the code into AppleScript Editor and then save it out as an application. Double clicking on it will bring up a dialog box telling you the current state of viewing hidden files and gives you the option of switching it. Since you have to restart the Finder after you make this change you’ll see the Finder quit and restart.

Be Sociable, Share!

3 Responses to “Viewing or hiding hidden files in the Finder using AppleScript”

  1. on 02 Feb 2011 at 3:59 pmJustin Kobylarz

    Here is a little script I made to allow you to make folders visible and invisible, or browse and open invisible folders.

    It is password protected as well…

    — This applescript helps you manipulate invisible folders easily.
    — Save this script as an application and launch it whenever you need to Hide, Show, or, Browse an invisible folder.
    –Password Protected Script

    set ChoicePass to text returned of (display dialog “Password” default answer “” buttons {“Cancel”, “OK”} default button 2)
    if (ChoicePass = “”) then

    — ask user what he wants to do
    set choice to button returned of (display dialog “Now You See It… Now You Don’t…” buttons {“Make Visible”, “Make Invisible”, “Browse”} default button “Browse”)
    — just browse his file system while showing invisible files/folders and open the selected invisible folder
    if choice = “Browse” then
    set x to (choose folder with invisibles) — Browse invisible folders
    tell application “Finder” to open x — Open folder

    else if choice = “Make Visible” then
    tell application “Finder”

    set FolderPath to (choose folder with invisibles) as alias — sets file path to folder you select
    set ParentFolder to container of FolderPath as text — sets the parent folder of the folder you select
    set NewFolderPath to FolderPath as text — strips out the . from the file path
    set AppleScript’s text item delimiters to “.”
    set the item_list to every text item of NewFolderPath
    set AppleScript’s text item delimiters to “”
    set NewFolderPath to the item_list as string
    set AppleScript’s text item delimiters to “”

    end tell
    try
    do shell script “mv ” & quoted form of the POSIX path of (FolderPath) & ” ” & quoted form of the POSIX path of (NewFolderPath)
    on error the error_message number the error_number
    display dialog “Error: The File IS Visible!” buttons {“OK”} default button 1
    end try
    else if choice = “Make Invisible” then
    tell application “Finder”

    set FolderPath to (choose folder) as alias — sets file path to folder you select
    set ParentFolder to container of FolderPath as text — sets the parent folder of the folder you select
    set Foldername to name of folder FolderPath as text — sets the folder name as text

    end tell
    try
    do shell script “mv ” & quoted form of the POSIX path of (ParentFolder & Foldername) & ” ” & quoted form of the POSIX path of (ParentFolder & “.” & Foldername)
    on error the error_message number the error_number
    display dialog “Error: The File IS Invisible!” buttons {“OK”} default button 1
    end try
    end if
    end if

  2. […] Mac Stuff ยป Viewing or hiding hidden files in the Finder using AppleScript […]

  3. […] Mac Stuff […]

Trackback this post | Feed on Comments to this post

Leave a Reply