前一段时间更新了MacOS 14 Sonoma之后,到今天想用 Command + ` 来切换窗口的时候发现怎么按都不行,上苹果社区看发现很多人都表示遇到了这个问题,但是都没有解决办法。后来去reddit看了一下,找到一个老哥用Hammerspoon——类似于Windows那边的AutoHotkey,可以自己写lua脚本调用macos的API实现一些操作——来手工实现这个快捷键本来应有的功能(中文叫做“将焦点移到下一个窗口”,英文是“move focus to next window”)。原帖在这:Mesieu: I’m going to fix this through Hammerspoon…。
安装HammerSpoon直接上github:Releases,下载zip之后双击解压,再手动移动到Finder的“应用程序”里面,然后双击打开,钩上登录自启动(Launch Hammerspoon at login),然后点击“Enable Accessibility”前往设置App启用辅助功能:
functiongetOrderedWindowsOfCurrentApp() local currentWindow = hs.window.focusedWindow() local appWindows = currentWindow:application():allWindows()
-- Filter out minimized windows and sort by window id local orderedWindows = {} for _, win inpairs(appWindows) do ifnot win:isMinimized() then table.insert(orderedWindows, win) end end table.sort(orderedWindows, function(a, b)return a:id() < b:id() end)
return orderedWindows, currentWindow end
functionswitchToNextWindowOfSameApp() local orderedWindows, currentWindow = getOrderedWindowsOfCurrentApp()
for i, win inipairs(orderedWindows) do if win == currentWindow then local nextWindow = orderedWindows[i + 1] or orderedWindows[1] nextWindow:focus() break end end end
functionswitchToPreviousWindowOfSameApp() local orderedWindows, currentWindow = getOrderedWindowsOfCurrentApp()
for i, win inipairs(orderedWindows) do if win == currentWindow then local previousWindow = orderedWindows[i - 1] or orderedWindows[#orderedWindows] previousWindow:focus() break end end end