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?

3 Answers 3

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)
1
  • I get error "field 'allSpaces' is not callable (a nil value)" running this code. May 2 at 8:49
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!

0
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.

1
  • Not really a fan of applescript, especially as my solution already worked, by +1 anyway!
    – Andrew
    May 26, 2022 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.