在工作中,我有一个 3 显示器设置。我想通过键绑定将当前应用程序移动到第二个或第三个监视器。怎么做?
4 回答
我使用以下脚本在屏幕中循环聚焦窗口。
-- bind hotkey
hs.hotkey.bind({'alt', 'ctrl', 'cmd'}, 'n', function()
-- get the focused window
local win = hs.window.focusedWindow()
-- get the screen where the focused window is displayed, a.k.a. current screen
local screen = win:screen()
-- compute the unitRect of the focused window relative to the current screen
-- and move the window to the next screen setting the same unitRect
win:move(win:frame():toUnitRect(screen:frame()), screen:next(), true, 0)
end)
该screen
库有助于找到正确的“显示”。allScreens
按照系统定义的顺序列出显示。该hs.window:moveToScreen
函数移动到给定的屏幕,可以在其中设置 UUID。
以下代码适用于我。点击CTRL
+ ALT
+ CMD
+3
将当前聚焦的窗口移动到显示 3,就像您在 Dock 的选项菜单中选择“显示 3”一样。
function moveWindowToDisplay(d)
return function()
local displays = hs.screen.allScreens()
local win = hs.window.focusedWindow()
win:moveToScreen(displays[d], false, true)
end
end
hs.hotkey.bind({"ctrl", "alt", "cmd"}, "1", moveWindowToDisplay(1))
hs.hotkey.bind({"ctrl", "alt", "cmd"}, "2", moveWindowToDisplay(2))
hs.hotkey.bind({"ctrl", "alt", "cmd"}, "3", moveWindowToDisplay(3))
I've answered this in Reddit post here, but in case anyone comes across this question here's the answer:
The Hammerspoon API doesn't provide an explicit function for doing this, so you gotta roll out with a custom implementation to achieve this:
-- Get the focused window, its window frame dimensions, its screen frame dimensions,
-- and the next screen's frame dimensions.
local focusedWindow = hs.window.focusedWindow()
local focusedScreenFrame = focusedWindow:screen():frame()
local nextScreenFrame = focusedWindow:screen():next():frame()
local windowFrame = focusedWindow:frame()
-- Calculate the coordinates of the window frame in the next screen and retain aspect ratio
windowFrame.x = ((((windowFrame.x - focusedScreenFrame.x) / focusedScreenFrame.w) * nextScreenFrame.w) + nextScreenFrame.x)
windowFrame.y = ((((windowFrame.y - focusedScreenFrame.y) / focusedScreenFrame.h) * nextScreenFrame.h) + nextScreenFrame.y)
windowFrame.h = ((windowFrame.h / focusedScreenFrame.h) * nextScreenFrame.h)
windowFrame.w = ((windowFrame.w / focusedScreenFrame.w) * nextScreenFrame.w)
-- Set the focused window's new frame dimensions
focusedWindow:setFrame(windowFrame)
Wrapping the snippet above in a function and binding it to a hotkey should cycle the currently focused application across your different monitors.
不完全是 OP 的答案,而是将其留给其他想要循环浏览监视器并在每个屏幕上最大化的人:
local app = hs.window.focusedWindow()
app:moveToScreen(app:screen():next())
app:maximize()
您可以将它放在一个函数中并将其绑定到 Ctrl + Alt + n ,如下所示:
function moveToNextScreen()
local app = hs.window.focusedWindow()
app:moveToScreen(app:screen():next())
app:maximize()
end
hs.hotkey.bind({"ctrl", "alt"}, "n", moveToNextScreen)