首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Hammerspoon的监视器之间移动应用程序?

如何在Hammerspoon的监视器之间移动应用程序?
EN

Stack Overflow用户
提问于 2019-01-12 01:27:05
回答 4查看 2K关注 0票数 6

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-10-15 23:39:31

screen库有助于找到正确的“显示”。allScreens按系统定义的顺序列出显示内容。hs.window:moveToScreen函数移动到给定的屏幕,可以在其中设置UUID。

下面的代码适用于我。点击CTRL+ALT+CMD+ 3将当前聚焦的窗口移动到display 3,就像你在Dock的选项菜单中选择了"Display 3“一样。

代码语言:javascript
复制
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))
票数 7
EN

Stack Overflow用户

发布于 2019-11-01 23:54:48

我使用以下脚本在屏幕之间循环显示焦点窗口。

代码语言:javascript
复制
-- 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)
票数 14
EN

Stack Overflow用户

发布于 2019-04-21 06:31:04

我已经在Reddit post here中回答了这个问题,但如果有人遇到这个问题,这里有答案:

Hammerspoon API没有提供显式的函数来实现这一点,因此您必须使用自定义实现来实现这一点:

代码语言:javascript
复制
-- 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)

将上面的代码片段包装在一个函数中,并将其绑定到一个热键,应该会在不同的监视器之间循环当前关注的应用程序。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54151343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档