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

时间:2019-01-11 17:27:05

标签: hammerspoon

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

4 个答案:

答案 0 :(得分:2)

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

以下代码对我有用。 击中CTRL + ALT + CMD + 3会将当前聚焦的窗口移至显示3,就像在Dock的“选项”菜单中选择“显示3”一样。 / p>

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))

答案 1 :(得分:1)

我使用以下脚本在窗口中循环显示焦点窗口。

public static void main(String[] args) {
    Main main = new Main();

    System.out.println(main.drawMap());
}

public int[][] drawMap(){
    int[][] map = new int[5][5];
    char coin = 'o';

    for(int i =0; i<map.length; i++){
        for(int j =0; j<map[i].length; j++){
            map[i][j] = (int)(Math.random()*10);
            if(map[i][j]<2){
                System.out.print(coin+ " ");
            }
            else
            System.out.print("*"+ " ");
        }
        System.out.println("");
    }

    calculateCoin(map, coin);
    System.out.println("");

    return map;
}

public int calculateCoin(int[][] map, char coin){
    int result = 0;

    for(int i = 0; i<map.length; i++){
        for(int j = 0; j<map[i].length; j++){
            if(map[i][j] == coin){
                result++;
            }
        }
    }
    return result;
}

答案 2 :(得分:0)

我已经在Reddit帖子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)

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

答案 3 :(得分:0)

不完全是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)