I've got a window resize/position script that works fine if I invoke it with a hotkey, but if I try to invoke it with a hammerspoon:// url it seems to cause active window to lose focus after the script executes.
I'm not sure if this is a Hammerspoon issue or an issue with the way OS X handles shell commands, but the issue happens when I use the 'open' command, either from the Terminal or from a Karabiner-Elements complex mod.
e.g. from Terminal:
open 'hammerspoon://windowMove?left=1&top=0&width=1&height=1&base=2'
The script has the desired effect (sizing and repositioning the window) but then the Terminal window loses focus (also: the menu bar continues to show Terminal's menu commands, but keyboard shortcuts e.g. cmd-N do not work, so possibly the app has lost focus as well?)
When calling using a hotkey (either directly from keyboard or remapped via Karabiner-elements) the window retains focus. When Karabiner rule is changed to execute shell command instead, window loses focus.
init.lua:
~~~~
function win_move(left, top, width, height, base)
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
local screen_left = max.x
local screen_top = max.y
local base_width = max.w / base
local base_height = max.h / base
new_left = screen_left + left * base_width
new_width = width * base_width
new_top = screen_top + top * base_height
new_height = height * base_height
f.x = new_left
f.y = new_top
f.w = new_width
f.h = new_height
win:setFrame(f)
end
hs.urlevent.bind("windowMove", function(eventName, params)
w = win_move(params["left"], params["top"], params["width"], params["height"], params["base"])
end)
hs.hotkey.bind({"ctrl","alt"},"f1", function()
win_move(1,0,1,1,2)
end)
~~~~
By default, open focuses the application that's handling the URL. Use open --background (or open -g) to send the URL event in the background and leave focus where it is :)
Thank you!
Most helpful comment
By default,
openfocuses the application that's handling the URL. Useopen --background(oropen -g) to send the URL event in the background and leave focus where it is :)