DISCLAIMER/WARNING
Hidden files are hidden for a reason usually. Use the script below only if you're reasonably sure you know what you're doing. In other words: If you break your system by randomly deleting hidden files, it's your own fault.
Here's the script I use to toggle show/don't show hidden files inside Finder.
(The OSX native shortcut to toggle hidden files in Open/Save dialogues was mentioned by Mateusz Szlosek above already)
The difference between my script and others I've seen so far:
Most scripts don't do anything else after killing Finder. Usually, this means you have to navigate back to the folder you were in originally, since the automatic Finder relaunch doesn't reliably open your last active folder.
The script below instead reopens, and focuses on, your last active folder, i.e. the folder you were in when using the toggle, but now showing (or not showing) hidden files.
I assigned a shortcut to it (cmd+shift+.) and find working with hidden files in Finder a lot more convenient now.
Credit to Tetsujin who posted a script on this site a while ago that I adapted to add the 'reopen last folder' functionality.
on run {input, parameters}
tell application "Finder"
set var1 to POSIX path of (target of window 1 as alias)
end tell
set newHiddenVisiblesState to "YES"
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
do shell script "defaults write com.apple.finder AppleShowAllFiles " & newHiddenVisiblesState
---- Sleep hack mentioned below. 0.5s minimum, 1-2s safer. Ugly, but (probably) unavoidable.
do shell script "killall Finder; sleep 0.5s"
tell application "Finder"
---- Close other windows if Finder managed to relaunch already
if application "Finder" is running then
close every window
end if
open (var1 as POSIX file)
activate
end tell
end run
To use the script: Open a new workflow in Automator, drag in "Run AppleScript", copy&paste the code above, and save inside your services folder (usually, ~/Library/Services/, afaik).
Attaching a shortcut to execute a script should be covered somewhere else on this site. I limited the scope of my shortcut to Finder alone.
Style remark:
I have to resort to a bit of an ugly hack, using 'sleep' after killing Finder, before manually opening the original folder.
If I don't force this delay, there's a chance OSX relaunched Finder already and opened a folder (not necessarily the last active one, in my experience). Together with my own 'open' command, this can lead to the nasty effect of two (unkillable, even) Finder processes running, and Finder not working anymore at all. If this happens, logging out and back in again fixes it, but it's a waste of time obviously, so I use 'sleep'.
A sleep value of 0.5s works for me, but to be on the safe side, you can slightly increase it (adding a more noticeable delay to the process then).
If someone knows of a way to either suppress the Finder relaunch and unreliable opening of some window after sending the killall signal -- or some other way to get rid of the 'sleep' hack -- I'd be happy to hear it!