Hammerspoon 是 macOS 上强大的开源自动化工具,通过 Lua 脚本可以控制窗口、绑定快捷键、切换应用等,大幅提升工作效率。
brew install --cask hammerspoon
从 GitHub Releases 下载 .zip 文件并解压。解压后移到/Applications
系统设置 > 隐私与安全性 > 辅助功能
Hammerspoon 以允许控制电脑~/.hammerspoon/init.lua
编辑配置文件:
vim ~/.hammerspoon/init.lua
配置重载快捷键,出错时会弹窗提示:
-- 安全重载:出错时弹窗提示
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)
hs.mouse.setAbsolutePosition(win:screen():frame().center)
end
end)
使用 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)
-- 快速打开 GitHub
hs.hotkey.bind({"cmd", "ctrl"}, "G", function()
hs.urlevent.openURL("https://github.com")
end)
-- 菜单栏显示通知
hs.alert.show("Hammerspoon 已加载!")
修改 init.lua 后,有两种方式重新加载:
Cmd+Ctrl+R(默认快捷键)Reload Config
如果应用切换不生效,可能是应用名称不正确。在 Hammerspoon 控制台中运行:
hs.application.runningApplications()
或者在终端中运行:
osascript -e 'tell application "System Events" to get name of every process'
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 已加载!")