いいものをつくろう

CTOの日記

env

mac how to move active window to another space using hammerspoon

投稿日:

 

In mac, How can you move a window to another space?

I found that I can do that with while click and holding a window by the mouse,

press ctrl and then press number 1 (or 2 that desktop number you wish to send )

 

This is okay, at least I now know how to do that, but this is not quick enough and my hand are almost cramped.

 

That's why I looked for a way to do it in better way, such as shortcut with only keyboard typing, ideally, ctrl ->(right arrow) . Notice that ctrl + -> is already taken by switching desktop, so probably, different key combinations 🙂

 

1, I found a way to do that, it can be achieved using hammerspoon.

you can check how to install it in Try key binding with Hammerspoon(Japanese) 

 

2, install plugin MiroWindowManager. please note that this is optional as I later, found out this is not mandatory

cp -r ~/Downloads/miro-windows-manager-1.1/MiroWindowsManager.spoon ~/.hammerspoon/Spoons

3, just add configuration to use the window manager like

local hyper = {"ctrl", "alt", "cmd", "fn"}

hs.loadSpoon("MiroWindowsManager")

hs.window.animationDuration = 0.3
spoon.MiroWindowsManager:bindHotkeys({
  up = {hyper, "up"},
  right = {hyper, "right"},
  down = {hyper, "down"},
  left = {hyper, "left"},
  fullscreen = {hyper, "f"}
})

 

oh, no ~~

the above code just gonna move the active window to half of the current active desktop(space)....

but, I want to do is moving over spaces(desktops)

 

probably, this module can help me https://github.com/asmagill/hs._asm.undocumented.spaces

3, install that

cd ~/.hammerspoon/
git clone https://github.com/asmagill/hs._asm.undocumented.spaces spaces
cd spaces/
make install

then, you'll have hs/_asm and more in ~/.hammerspoon directory

 

Now that you restart hammerspoon in order hammerspoon to recognized the newly installed module

4, write your script in init.lua for finally moving window to another space!

local spaces = require("hs._asm.undocumented.spaces")
-- move current window to the space sp
function MoveWindowToSpace(sp)
    local win = hs.window.focusedWindow()      -- current window
    local uuid = win:screen():spacesUUID()     -- uuid for current screen
    local spaceID = spaces.layout()[uuid][sp]  -- internal index for sp
    spaces.moveWindowToSpace(win:id(), spaceID) -- move window to new space
    spaces.changeToSpace(spaceID)              -- follow window to new space
end
hs.hotkey.bind({"ctrl", "shift"}, '1', function() MoveWindowToSpace(1) end)
hs.hotkey.bind({"ctrl", "shift"}, '2', function() MoveWindowToSpace(2) end)

hinted by stackoverflow.com using-hammerspoon-and-the-spaces-module-to-move-window-to-new-space

 

Amazingly, yon press ctrl + shift + 2 to move currently active window to space 2(desktop2)

 

Enjoy!

 

That's it.

 

Reference:

https://git.picodevelopment.nl/hendrikpeter/dotfiles-hammerspoon

https://stackoverflow.com/questions/46818712/using-hammerspoon-and-the-spaces-module-to-move-window-to-new-space

 

 

-env

Copyright© CTOの日記 , 2020 All Rights Reserved.