Skip to content

Instantly share code, notes, and snippets.

@al3xandru
Last active February 17, 2019 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save al3xandru/bc5074fce216db2b271d14f988269967 to your computer and use it in GitHub Desktop.
Save al3xandru/bc5074fce216db2b271d14f988269967 to your computer and use it in GitHub Desktop.
Hammerspoon function for moving window to next desktop
-- Move window to adjacent Desktop
-- direction param can be 'left' or 'right'
function moveWndNextSpace(direction)
local win = hs.window.focusedWindow() or hs.window.frontmostWindow()
if not win then
return
end
if not win:isStandard() then
return
end
if win:isFullScreen() then
return
end
local clickPoint = win:zoomButtonRect()
clickPoint.x = clickPoint.x + clickPoint.w + 5
clickPoint.y = clickPoint.y + (clickPoint.h / 2)
hs.eventtap.event.newMouseEvent(hs.eventtap.event.types.leftMouseDown, clickPoint):post()
-- compacting these events seem to not trigger the change of desktop event
-- also according to https://www.hammerspoon.org/docs/hs.eventtap.event.html#newKeyEvent
-- this is the correct way to implement key events
hs.eventtap.event.newKeyEvent(hs.keycodes.map.ctrl, true):post()
hs.eventtap.event.newKeyEvent(direction, true):post()
hs.eventtap.event.newKeyEvent(direction, false):post()
hs.eventtap.event.newKeyEvent(hs.keycodes.map.ctrl, false):post()
hs.timer.doAfter(.5, function()
hs.eventtap.event.newMouseEvent(hs.eventtap.event.types.leftMouseUp, clickPoint):post()
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment