How to set ⌘ + H to enable show hidden files

I want my Mac to show hidden files when i hit ⌘ + H (like ubuntu). I can see hidden files by putting this defaults write com.apple.finder AppleShowAllFiles YES command in terminal but i want to get a keyboard shortcut for it. Also, if ⌘ + H is already used by some other process please let me know what might be the closest and most related shortcut for this new process and how to assign it to the respective, show hidden files process.


Edit at Nov 2018
This is all now moot since High Sierra. In Finder, Cmd ⌘ Shift ⇧ . [period, full stop] will toggle invisible files on the fly, nothing else needed.


Cmd ⌘ H is the system-wide shortcut for "Hide frontmost app"

To try tie a new shortcut to that, you could maybe use Automator to add a Service - but I still don't know how you would persuade it to override the existing System default.

This works as a Service if you use a non-system key, but not with Cmd ⌘ H

on run {input, parameters}

    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

    return input
end run

Edit for El Capitan…
I've noticed that the Finder window refresh no longer works in El Capitan, so this is a modified version, quitting the Finder instead.

on run {input, parameters}      
    --Toggle Invisibles, El Capitan Version     
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"
end run

enter image description here

I guess you could make it work for the Finder only, if you specified some other key command for Hide Finder - as it is visible as a Menu item it would be possible to override for that app alone [by putting something 'useless' as an alternative], leaving you free to then use Cmd ⌘ H to toggle invisibles from the Finder only.

Tested - does work this way

enter image description here


enter image description here


Cmd ⌘ Shift ⇧ . will toggle hidden file display in the finder(In Sierra or later(but works in the open file dialogue in earlier versions)).