Skip to content

Instantly share code, notes, and snippets.

@kizzx2
Last active December 19, 2022 06:47
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kizzx2/e542fa74b80b7563045a to your computer and use it in GitHub Desktop.
Save kizzx2/e542fa74b80b7563045a to your computer and use it in GitHub Desktop.
Hammerspoon script to move/resize window under cursor
-- Inspired by Linux alt-drag or Better Touch Tools move/resize functionality
function get_window_under_mouse()
-- Invoke `hs.application` because `hs.window.orderedWindows()` doesn't do it
-- and breaks itself
local _ = hs.application
local my_pos = hs.geometry.new(hs.mouse.getAbsolutePosition())
local my_screen = hs.mouse.getCurrentScreen()
return hs.fnutils.find(hs.window.orderedWindows(), function(w)
return my_screen == w:screen() and my_pos:inside(w:frame())
end)
end
dragging_win = nil
dragging_mode = 1
drag_event = hs.eventtap.new({ hs.eventtap.event.types.mouseMoved }, function(e)
if dragging_win then
local dx = e:getProperty(hs.eventtap.event.properties.mouseEventDeltaX)
local dy = e:getProperty(hs.eventtap.event.properties.mouseEventDeltaY)
local mods = hs.eventtap.checkKeyboardModifiers()
-- Ctrl + Shift to move the window under cursor
if dragging_mode == 1 and mods.ctrl and mods.shift then
dragging_win:move({dx, dy}, nil, false, 0)
-- Alt + Shift to resize the window under cursor
elseif mods.alt and mods.shift then
local sz = dragging_win:size()
local w1 = sz.w + dx
local h1 = sz.h + dy
dragging_win:setSize(w1, h1)
end
end
return nil
end)
flags_event = hs.eventtap.new({ hs.eventtap.event.types.flagsChanged }, function(e)
local flags = e:getFlags()
if flags.ctrl and flags.shift and dragging_win == nil then
dragging_win = get_window_under_mouse()
dragging_mode = 1
drag_event:start()
elseif flags.alt and flags.shift and dragging_win == nil then
dragging_win = get_window_under_mouse()
dragging_mode = 2
drag_event:start()
else
drag_event:stop()
dragging_win = nil
end
return nil
end)
flags_event:start()
@arrelid
Copy link

arrelid commented Nov 15, 2021

@dbalatero Thanks for the workaround - works like a charm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment