hammerspoon

How to move an application between monitors in Hammerspoon?


At work I have a 3 monitor setup. I would like to move the current application to a second or a third monitor with a key binding. How to do that?


Solution

  • The screen library helps finding the right "display". allScreens lists the displays in the same order as they are defined by the system. The hs.window:moveToScreen function moves to a given screen, where it's possible to set the UUID.

    The following code works for me. Hitting CTRL+ALT+CMD+ 3 moves the currently focused window to display 3, same as if you would choose "Display 3" in the Dock's Option menu.

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