如何在 Hammerspoon 的显示器之间移动应用程序?

pau*_*cha 5 hammerspoon

在工作中,我有一个 3 显示器设置。我想将当前应用程序移动到具有键绑定的第二个或第三个监视器。怎么做?

小智 12

我使用以下脚本在屏幕上循环聚焦窗口。

-- 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)
Run Code Online (Sandbox Code Playgroud)


Kar*_* S. 7

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))
Run Code Online (Sandbox Code Playgroud)


Nie*_*Bom 5

不完全是OP的答案,但将其留给其他也想循环浏览监视器并在每个屏幕上最大化的人:

local app = hs.window.focusedWindow()
app:moveToScreen(app:screen():next())
app:maximize()
Run Code Online (Sandbox Code Playgroud)

您可以将其放入函数中并将其绑定到 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)
Run Code Online (Sandbox Code Playgroud)