Using hammerspoon and the spaces module to move window to new space
Asked Answered
B

3

5

I have installed the "undocumented spaces" module from https://github.com/asmagill/hs._asm.undocumented.spaces. In particular, it provides a method moveWindowToSpace that I am trying to use to bind cmd+1 to move the the current window to space 1 using the following:

local spaces = require("hs._asm.undocumented.spaces")
function MoveWindowToSpace(sp)
    local spaceID = spaces.query()[sp]
    spaces.moveWindowToSpace(hs.window.focusedWindow():id(), spaceID)
    spaces.changeToSpace(spaceID)
end
hs.hotkey.bind({"cmd"}, "1",function() MoveWindowToSpace(1) end)

This works in the sense that it moves the window to a new space, however, the spaces appear to be in a pseudo random order.

Does any one know how to correctly map spaceIDs, as returned by spaces.query(), to the actual spaces?

Baggy answered 18/10, 2017 at 20:29 Comment(0)
E
5

As undocumented spaces has moved to spaces, the new code would be as follows (some lines could be merged, but I like the clarity of splitting operations):

spaces = require("hs.spaces")
-- move current window to the space sp
function MoveWindowToSpace(sp)
  local win = hs.window.focusedWindow()      -- current window
  local cur_screen = hs.screen.mainScreen()
  local cur_screen_id = cur_screen:getUUID()
  local all_spaces=spaces.allSpaces()
  local spaceID = all_spaces[cur_screen_id][sp]
  spaces.moveWindowToSpace(win:id(), spaceID)
  spaces.gotoSpace(spaceID)              -- follow window to new space
end
hs.hotkey.bind(hyper, '1', function() MoveWindowToSpace(1) end)
hs.hotkey.bind(hyper, '2', function() MoveWindowToSpace(2) end)
hs.hotkey.bind(hyper, '3', function() MoveWindowToSpace(3) end)
Espalier answered 25/5, 2022 at 13:36 Comment(1)
I get error "field 'allSpaces' is not callable (a nil value)" running this code.Molasses
B
4

After some hints from the author of the spaces module I came up with the following, which seems to do the trick.

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(hyper, '1', function() MoveWindowToSpace(1) end)

Previously I was using a variation on the code at https://github.com/Hammerspoon/hammerspoon/issues/235, which hooks into osx defined hotkeys for switching spaces, but the code above is much faster!

Baggy answered 19/10, 2017 at 16:21 Comment(0)
N
1

For those in 2020 looking for simpler working solution, you could use apple scripts:

function moveWindowOneSpace(direction)
    local keyCode = direction == "left" and 123 or 124

    return hs.osascript.applescript([[
        tell application "System Events" 
            keystroke (key code ]] .. keyCode .. [[ using control down)
        end tell
    ]])
end

I tried to use solutions from this issue, but it didn’t work. On the other hand apple scripts work like charm.

Nunhood answered 5/11, 2020 at 12:14 Comment(1)
Not really a fan of applescript, especially as my solution already worked, by +1 anyway!Baggy

© 2022 - 2024 — McMap. All rights reserved.