Hammerspoonを利用し、デュアルモニター運用を快適にする

⌥+1 / 2を押すことで、Display 1 / 2それぞれの中央にカーソルを移動させるだけのシンプルな自作スクリプトですが、かなり快適になるので載せておきます。

require("hs.ipc")

local function moveCursorToDisplay(idx)
  local screens = hs.screen.allScreens()
  if idx < 1 or idx > #screens then
    hs.alert.show("Display index out of range: " .. idx)
    return
  end

  local frame = screens[idx]:fullFrame()
  local center = { x = frame.x + frame.w / 2, y = frame.y + frame.h / 2 }

  hs.eventtap.event.newMouseEvent(hs.eventtap.event.types.mouseMoved, center):post()
end

local hotkeys = {
  { { "alt" }, "1", 1 },
  { { "alt" }, "2", 2 },
}

for _, hk in ipairs(hotkeys) do
  hs.hotkey.bind(hk[1], hk[2], function()
    moveCursorToDisplay(hk[3])
  end)
end