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?

Andrew
  • 281
  • 4
  • 16

3 Answers3

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)
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Jean-Pascal J.
  • 146
  • 1
  • 8
  • Thanks for letting me know that the status of `spaces` has changed! I have given you the green tick as it's an updated solution – Andrew May 26 '22 at 14:15
  • 1
    thanks @Andrew :-) I wanted to comment on your answer so that you could update it, but I didn't have enough reputation, that's why I put another answer – Jean-Pascal J. May 26 '22 at 21:40
  • I get error "field 'allSpaces' is not callable (a nil value)" running this code. – user3156459 May 02 '23 at 08: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!

Andrew
  • 281
  • 4
  • 16
  • 1
    Thanks, works nicely! There is a typo though: `moveWindwToSpace`. Could you please correct it? I can't as i'd have to change at least 6 characters for some reason. Thank you :) – sudoremo Nov 22 '18 at 09:59
  • @Remo Cheers. Fixed. – Andrew Nov 22 '18 at 11:40
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.

alice kibin
  • 572
  • 1
  • 4
  • 18