Skip to content

Instantly share code, notes, and snippets.

@ansiwen
Created February 5, 2021 17:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ansiwen/7e17fcb414fc6deaf2aa61d7e3269a46 to your computer and use it in GitHub Desktop.
moveWindowOneSpace for hammerspoon
local hotkey = require "hs.hotkey"
local window = require "hs.window"
local spaces = require "hs._asm.undocumented.spaces"
function getGoodFocusedWindow(nofull)
local win = window.focusedWindow()
if not win or not win:isStandard() then return end
if nofull and win:isFullScreen() then return end
return win
end
function flashScreen(screen)
local flash=hs.canvas.new(screen:fullFrame()):appendElements({
action = "fill",
fillColor = { alpha = 0.25, red=1},
type = "rectangle"})
flash:show()
hs.timer.doAfter(.15,function () flash:delete() end)
end
function moveWindowOneSpace(dir)
local win = getGoodFocusedWindow(true)
if not win then return end
local screen=win:screen()
local uuid=screen:spacesUUID()
local userSpaces=nil
for k,v in pairs(spaces.layout()) do
userSpaces=v
if k==uuid then break end
end
if not userSpaces then return end
local thisSpace=win:spaces() -- first space win appears on
if not thisSpace then return else thisSpace=thisSpace[1] end
local last=nil
local skipSpaces=0
for _, spc in ipairs(userSpaces) do
if spaces.spaceType(spc)~=spaces.types.user then -- skippable space
skipSpaces=skipSpaces+1
else -- A good user space, check it
if last and
((dir=="left" and spc==thisSpace) or
(dir=="right" and last==thisSpace))
then
win:spacesMoveTo(dir=="left" and last or spc)
win:focus()
return
end
last=spc -- Haven't found it yet...
skipSpaces=0
end
end
flashScreen(screen) -- Shouldn't get here, so no space found
end
mash = {"ctrl", "cmd"}
hotkey.bind(mash, "right",nil,
function() moveWindowOneSpace("right",true) end)
hotkey.bind(mash, "left",nil,
function() moveWindowOneSpace("left",true) end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment