MacOS – Show/Hide hidden files without restarting finder

findermacmacos

I came to understand that the following is the way to toggle the visibility of hidden files:

defaults write com.apple.finder AppleShowAllFiles YES
# replace YES with NO to hide hidden files
killall -HUP Finder /System/Library/CoreServices/Finder.app

Is there a way to show/hide hidden files without killing the Finder?

Best Answer

Edit: This no longer appears to work since El Capitan. killall Finder seems to be the only way now.

This is my current El Capitan method, which should also work for Mountain Lion & older

set newHiddenVisiblesState to "YES"
try
    set oldHiddenVisiblesState to do shell script "defaults read com.apple.finder AppleShowAllFiles"
    if oldHiddenVisiblesState is in {"1", "YES"} then
        set newHiddenVisiblesState to "NO"
    end if
end try
do shell script "defaults write com.apple.finder AppleShowAllFiles " & newHiddenVisiblesState & "; killall Finder"

For Mavericks & Yosemite…

You don't need to restart the Finder, only refresh the windows.

This Applescript will toggle the state & refresh...

set newHiddenVisiblesState to "YES"
try
    set oldHiddenVisiblesState to do shell script "defaults read com.apple.finder AppleShowAllFiles"
    if oldHiddenVisiblesState is in {"1", "YES"} then
        set newHiddenVisiblesState to "NO"
    end if
end try
do shell script "defaults write com.apple.finder AppleShowAllFiles " & newHiddenVisiblesState


tell application "Finder"
    set theWindows to every Finder window
    repeat with i from 1 to number of items in theWindows
        set this_item to item i of theWindows
        set theView to current view of this_item
        if theView is list view then
            set current view of this_item to icon view
        else
            set current view of this_item to list view

        end if
        set current view of this_item to theView
    end repeat
end tell

Credit to ganbustein for the improved hide/show routine