I went ahead and converted this function to Lua. Works great:
function movemouse(x1,y1,x2,y2,sleep)
local xdiff = x2 - x1
local ydiff = y2 - y1
local loop = math.floor( math.sqrt((xdiff*xdiff)+(ydiff*ydiff)) )
local xinc = xdiff / loop
local yinc = ydiff / loop
sleep = math.floor((sleep * 1000000) / loop)
for i=1,loop do
x1 = x1 + xinc
y1 = y1 + yinc
hs.mouse.absolutePosition({x = math.floor(x1), y = math.floor(y1)})
hs.timer.usleep(sleep)
end
hs.mouse.absolutePosition({x = math.floor(x2), y = math.floor(y2)})
end
--
Example: movemouse(0,0,1920,1280,2)
I haven’t tried anything quite like this, but it seems like you’d be better served by using window filters to filter by the current space?
http://www.hammerspoon.org/docs/hs.window.filter.html
specifically http://www.hammerspoon.org/docs/hs.window.filter.html#defaultCurrentSpace
You could use the hs.osascript module in Hammerspoon and use AppleScript to inject JavaScript on some hotkey:
lua
hs.hotkey.bind({ "alt", "cmd", "shift" }, "s", function()
hs.osascript.applescript([[
tell application "Safari"
set js to read ("/Users/QualitativeEasing/bookmarklet.js") as text using delimiter linefeed
do JavaScript js in current tab of first window
end tell
]])
end)
The AppleScript for Chrome would look only a little different, you'd tell application "Google Chrome"
and the do JavaScript
line would instead be execute front window's active tab javascript js
.