Mac 把 Mac 变成键盘流神器:Hammerspoon 配置指南(窗口管理 + 应用秒开 + 快捷键自定义)

ThxFly · November 28, 2025 · 28 hits

Hammerspoon 是 macOS 上强大的开源自动化工具,通过 Lua 脚本可以控制窗口、绑定快捷键、切换应用等,大幅提升工作效率。

🔧 安装

方式一:使用 Homebrew(推荐)

brew install --cask hammerspoon

方式二:手动安装

GitHub Releases 下载 .zip 文件并解压。解压后移到/Applications

首次配置

  1. 授权辅助功能
    • 打开 系统设置 > 隐私与安全性 > 辅助功能
    • 勾选 Hammerspoon 以允许控制电脑
  2. 配置文件位置
    • 配置文件位于:~/.hammerspoon/init.lua
    • 首次启动会自动生成示例文件

📝 配置示例

编辑配置文件:

vim ~/.hammerspoon/init.lua

1. 安全重载配置

配置重载快捷键,出错时会弹窗提示:

-- 安全重载:出错时弹窗提示
hs.hotkey.bind({"cmd", "ctrl"}, "R", function()
  local success, msg = pcall(hs.reload)
  if not success then
    hs.alert.show("❌ 配置错误:\n" .. tostring(msg))
  else
    hs.alert.show("✅ 配置已重载", 1)
  end
end)

2. 窗口管理

窗口分屏(四象限布局)

-- 窗口分屏函数
local function moveWindowTo(rect)
  return function()
    local win = hs.window.focusedWindow()
    if win then win:move(rect, nil, true) end
  end
end

-- 快捷键绑定
hs.hotkey.bind({"cmd", "ctrl"}, "Up",   moveWindowTo(hs.geometry.rect(0, 0, 1, 0.5)))      -- 上半屏
hs.hotkey.bind({"cmd", "ctrl"}, "Down", moveWindowTo(hs.geometry.rect(0, 0.5, 1, 0.5)))   -- 下半屏
hs.hotkey.bind({"cmd", "ctrl"}, "Left", moveWindowTo(hs.geometry.rect(0, 0, 0.5, 1)))     -- 左半屏
hs.hotkey.bind({"cmd", "ctrl"}, "Right", moveWindowTo(hs.geometry.rect(0.5, 0, 0.5, 1)))  -- 右半屏

多显示器切换

-- 将窗口移到下一显示器,并移动鼠标到窗口中心
hs.hotkey.bind({"cmd", "ctrl"}, "F", function()
  local win = hs.window.focusedWindow()
  if win then
    win:moveToScreen(win:screen():next())
    hs.alert.show("窗口已移至下一屏幕", 1)
    hs.mouse.setAbsolutePosition(win:screen():frame().center)
  end
end)

3. 应用快速切换

使用 Option + 字母 快速切换到常用应用:

-- 浏览器
hs.hotkey.bind({"option"}, "s", function()
  hs.application.launchOrFocus("Safari")
end)

hs.hotkey.bind({"option"}, "g", function()
  hs.application.launchOrFocus("Google Chrome")
end)

-- 开发工具
hs.hotkey.bind({"option"}, "a", function()
  hs.application.launchOrFocus("Apifox")
end)

hs.hotkey.bind({"option"}, "t", function()
  hs.application.launchOrFocus("iTerm")
end)

hs.hotkey.bind({"option"}, "c", function()
  hs.application.launchOrFocus("Cursor")
end)

hs.hotkey.bind({"option"}, "d", function()
  hs.application.launchOrFocus("DBeaver")
end)

-- AI 工具
hs.hotkey.bind({"option"}, "e", function()
  hs.application.launchOrFocus("Qianwen")
end)

-- 社交应用
hs.hotkey.bind({"option"}, "w", function()
  hs.application.launchOrFocus("WeChat")
end)

hs.hotkey.bind({"option"}, "q", function()
  hs.application.launchOrFocus("企业微信")
end)

-- 系统工具
hs.hotkey.bind({"option"}, "p", function()
  hs.application.launchOrFocus("System Settings")
end)

-- 娱乐
hs.hotkey.bind({"option"}, "y", function()
  hs.application.launchOrFocus("NeteaseMusic")
end)

4. 快速打开网页

-- 快速打开 GitHub
hs.hotkey.bind({"cmd", "ctrl"}, "G", function()
  hs.urlevent.openURL("https://github.com")
end)

5. 启动提示

-- 菜单栏显示通知
hs.alert.show("Hammerspoon 已加载!")

🔄 重新加载配置

修改 init.lua 后,有两种方式重新加载:

  1. 快捷键:按 Cmd+Ctrl+R(默认快捷键)
  2. 菜单栏:点击 Hammerspoon 菜单栏图标 → Reload Config

💡 实用技巧

查找应用名称

如果应用切换不生效,可能是应用名称不正确。在 Hammerspoon 控制台中运行:

hs.application.runningApplications()

或者在终端中运行:

osascript -e 'tell application "System Events" to get name of every process'

常用 API 参考

  • hs.window.focusedWindow() - 获取当前焦点窗口
  • hs.application.launchOrFocus(name) - 启动或聚焦应用
  • hs.alert.show(message, seconds) - 显示提示
  • hs.hotkey.bind(modifiers, key, fn) - 绑定快捷键

📚 更多资源

🎯 快速上手

一句话总结:写 Lua 脚本 → 绑定快捷键 → 自动化你的 Mac!


提示:根据个人需求调整快捷键和应用名称,打造专属的自动化工作流。

完整代码示例

-- 安全重载:出错时弹窗提示
hs.hotkey.bind({"cmd", "ctrl"}, "R", function()
  local success, msg = pcall(hs.reload)
  if not success then
    hs.alert.show("❌ 配置错误:\n" .. tostring(msg))
  else
    hs.alert.show("✅ 配置已重载", 1)
  end
end)

-- 切换窗口
local function moveWindowTo(rect)
  return function()
    local win = hs.window.focusedWindow()
    if win then win:move(rect, nil, true) end
  end
end

hs.hotkey.bind({"cmd", "ctrl"}, "Up",   moveWindowTo(hs.geometry.rect(0, 0, 1, 0.5)))
hs.hotkey.bind({"cmd", "ctrl"}, "Down", moveWindowTo(hs.geometry.rect(0, 0.5, 1, 0.5)))
hs.hotkey.bind({"cmd", "ctrl"}, "Left", moveWindowTo(hs.geometry.rect(0, 0, 0.5, 1)))
hs.hotkey.bind({"cmd", "ctrl"}, "Right", moveWindowTo(hs.geometry.rect(0.5, 0, 0.5, 1)))

-- 将窗口移到下一显示器
hs.hotkey.bind({"cmd", "ctrl"}, "F", function()
  local win = hs.window.focusedWindow()
  if win then
    win:moveToScreen(win:screen():next())
    hs.alert.show("窗口已移至下一屏幕", 1)
  end
  local win = hs.window.focusedWindow()
  if win then
    hs.mouse.setAbsolutePosition(win:screen():frame().center)
  end
end)

-- 快速打开 GitHub
hs.hotkey.bind({"cmd", "ctrl"}, "G", function()
  hs.urlevent.openURL("https://github.com")
end)

-- 快捷键: Option+s 切换到 Safari
hs.hotkey.bind({"option"}, "s", function()
    hs.application.launchOrFocus("Safari")
end)

-- 快捷键: Option+g 切换到 Google Chrome
hs.hotkey.bind({"option"}, "g", function()
    hs.application.launchOrFocus("Google Chrome")
end)

-- 快捷键: Option+a 切换到 Apifox
hs.hotkey.bind({"option"}, "a", function()
    hs.application.launchOrFocus("Apifox")
end)

-- 快捷键: Option+t 切换到 iTerm
hs.hotkey.bind({"option"}, "t", function()
    hs.application.launchOrFocus("iTerm")
end)

-- 快捷键: Option+c 切换到 Cursor
hs.hotkey.bind({"option"}, "c", function()
    hs.application.launchOrFocus("Cursor")
end)

-- 快捷键: Option+e 切换到 千问
hs.hotkey.bind({"option"}, "e", function()
    hs.application.launchOrFocus("Qianwen")
end)

-- 快捷键: Option+w 切换到 微信
hs.hotkey.bind({"option"}, "w", function()
    hs.application.launchOrFocus("WeChat")
end)

-- 快捷键: Option+q 切换到 企业微信
hs.hotkey.bind({"option"}, "q", function()
    hs.application.launchOrFocus("企业微信")
end)

-- 快捷键: Option+p 切换到 系统设置
hs.hotkey.bind({"option"}, "p", function()
    hs.application.launchOrFocus("System Settings")
end)

-- 快捷键: Option+f 切换到 Finder
hs.hotkey.bind({"option"}, "f", function()
    hs.application.launchOrFocus("Finder")
end)

-- 快捷键: Option+y 切换到 网易云音乐
hs.hotkey.bind({"option"}, "y", function()
    hs.application.launchOrFocus("NeteaseMusic")
end)

-- 快捷键: Option+d 切换到 DBeaver
hs.hotkey.bind({"option"}, "d", function()
    hs.application.launchOrFocus("DBeaver")
end)

-- 菜单栏显示通知
hs.alert.show("Hammerspoon 已加载!")
No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.