在工作中,我有一个3显示器的设置。我想将当前应用程序移动到第二个或第三个具有键绑定的监视器。如何做到这一点呢?
发布于 2019-10-15 23:39:31
screen库有助于找到正确的“显示”。allScreens按系统定义的顺序列出显示内容。hs.window:moveToScreen函数移动到给定的屏幕,可以在其中设置UUID。
下面的代码适用于我。点击CTRL+ALT+CMD+ 3将当前聚焦的窗口移动到display 3,就像你在Dock的选项菜单中选择了"Display 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))发布于 2019-11-01 23:54:48
我使用以下脚本在屏幕之间循环显示焦点窗口。
-- 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)发布于 2019-04-21 06:31:04
我已经在Reddit post here中回答了这个问题,但如果有人遇到这个问题,这里有答案:
Hammerspoon API没有提供显式的函数来实现这一点,因此您必须使用自定义实现来实现这一点:
-- 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)将上面的代码片段包装在一个函数中,并将其绑定到一个热键,应该会在不同的监视器之间循环当前关注的应用程序。
https://stackoverflow.com/questions/54151343
复制相似问题