macOS 15 起,蘋果提供了新的 Window Tiling (並排視窗) 功能,Setting 中也可以自行設定快捷鍵。然而這功能 Windows很久以前就有了,所以很多使用者在舊的 macOS 就會使用 Spectacle 之類的 app 來實現並排視窗。如果你是 macOS 14 以下的使用者,不想升級至 macOS 15,你也可以利用 Hammerspoon 來實作這個便利的功能哦。


延伸閱讀:
– Hammerspoon – 轉換映射 MacBook 鍵盤按鍵配置
Source
它的邏輯很清楚簡單,首先 bind 快捷鍵,接著抓取 active 中的視窗 (使用中) 計算尺吋,再計算 desktop / screen 的尺吋後,重新放置視窗的位置
local hotkey = require "hs.hotkey"
local window = require "hs.window"
-- Tile active window to left half
hotkey.bind({"cmd", "alt", "ctrl"}, "Left", function()
local win = window.focusedWindow()
if not win then return end
local screen = win:screen()
local frame = screen:frame()
frame.w = frame.w / 2
frame.h = frame.h
frame.x = frame.x
frame.y = frame.y
win:setFrame(frame)
end)
-- Tile active window to right half
hotkey.bind({"cmd", "alt", "ctrl"}, "Right", function()
local win = window.focusedWindow()
if not win then return end
local screen = win:screen()
local frame = screen:frame()
frame.w = frame.w / 2
frame.h = frame.h
frame.x = frame.x + frame.w
frame.y = frame.y
win:setFrame(frame)
end)
-- Tile active window to top half
hotkey.bind({"cmd", "alt", "ctrl"}, "Up", function()
local win = window.focusedWindow()
if not win then return end
local screen = win:screen()
local frame = screen:frame()
frame.w = frame.w
frame.h = frame.h / 2
frame.x = frame.x
frame.y = frame.y
win:setFrame(frame)
end)
-- Tile active window to bottom half
hotkey.bind({"cmd", "alt", "ctrl"}, "Down", function()
local win = window.focusedWindow()
if not win then return end
local screen = win:screen()
local frame = screen:frame()
frame.w = frame.w
frame.h = frame.h / 2
frame.x = frame.x
frame.y = frame.y + frame.h
win:setFrame(frame)
end)
Comments