Show/Hide hidden files without restarting finder?

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?


Solution 1:

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

Solution 2:

On macOS Sierra, version 10.12.4 and above, you can press +Shift+. (period), to toggle hidden files inside of Finder.

Late edit: This currently works on Mojave too, as of b5, 18 Aug 2018.