Search code examples
luahammerspoon

How to open a directory in Hammerspoon?


I would like to open a directory on Hammerspoon with a keyboard shortcut. In order to open any apps via shortcut, you use the following:

hs.hotkey.bind({"ctrl"}, "n", function()
    hs.application.launchOrFocus("Safari")
    end
)

However, this doesn't work on the filesystem. For example, if you want to open ~/Dropbox, what method should you do to open the app?


Solution

  • I'm not sure if there is an API specifically suitable for this task, but I found that one solution is bind keys to execute a shell command on Hammerspoon (via hs.execute()).

    local function directoryLaunchKeyRemap(mods, key, dir)
        local mods = mods or {}
        hs.hotkey.bind(mods, key, function()
            local shell_command = "open " .. dir
            hs.execute(shell_command)
        end)
    end
    
    directoryLaunchKeyRemap({"ctrl"}, "1", "/Applications") 
    

    This lets you open /Applications directory via +1.