Skip to content

Instantly share code, notes, and snippets.

@kyounger
Created July 25, 2020 04:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyounger/c0d9ee86aec0e16a9f05cbe33c8f50ae to your computer and use it in GitHub Desktop.
Save kyounger/c0d9ee86aec0e16a9f05cbe33c8f50ae to your computer and use it in GitHub Desktop.
hammerspoon draw box and obtain coordinates
hs.hotkey.bind(modShiftHyper, "W", function()
-- local tracking = false
local startingMousePosition = hs.mouse.getAbsolutePosition()
local max = hs.screen.mainScreen():fullFrame()
local maxCanvas = hs.canvas.new{x=max.x, y=max.y, h=max.h, w=max.w}
maxCanvas:clickActivating(false)
maxCanvas:canvasMouseEvents(true, true, false, true)
maxCanvas:mouseCallback(function(_, event, id, x, y)
local currentMousePosition = hs.mouse.getAbsolutePosition()
if event == "mouseMove" then
-- if tracking then
maxCanvas:replaceElements({
type="rectangle",
action="stroke",
strokeWidth=2.0,
strokeColor= {green=1.0},
frame = {
x=startingMousePosition.x,
y=startingMousePosition.y,
h=(currentMousePosition.y-startingMousePosition.y),
w=(currentMousePosition.x-startingMousePosition.x)
},
})
-- end
-- elseif event == "mouseDown" then
-- tracking=true
elseif event == "mouseUp" then
print(hs.inspect(startingMousePosition))
print(hs.inspect(currentMousePosition))
maxCanvas:delete()
end
end)
maxCanvas:level("dragging")
maxCanvas:show()
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment