macosluahammerspoon

Why does hammerspoon return focused window nil when it should not?


I was trying to use hammerspoon to control my windows (on MAC OSX) but was having some issue. Straight from the getting started page I copied the code for moving the focused window left:

hs.hotkey.bind({"cmd", "alt", "ctrl"}, "Left", function()
  local win = hs.window.focusedWindow()
  local f = win:frame()
  local screen = win:screen()
  local max = screen:frame()

  f.x = max.x
  f.y = max.y
  f.w = max.w / 2
  f.h = max.h
  win:setFrame(f)
end)

I then when back reloaded my config and tried it out. Unfortunately, it doesn't work and opens the hammerspoon console with the following error:

*** ERROR: hs.hotkey callback error: /Users/my_secret_username/.hammerspoon/init.lua:25: attempt to index a nil value (local 'win')
stack traceback:
    /Users/my_secret_username/.hammerspoon/init.lua:25: in function </Users/my_secret_username/.hammerspoon/init.lua:23>

it says that my variable is nil but I don't see why it should be nil. I tried inserting a (what I assume in hammerspoon is) a print statement hs.console.printStyledtext(win) right after the variable win to inspect it but that also throws the same error except it print nil right before the error.

The strange thing is that the functionality or the code "works" (i.e. moves m screen left) once it focuses on the console but nothing else that I have tried.

Has anyone seen this type of bug and been able to fix it?


I did see:

Lua Hammerspoon: hs.window.focusedWindow() is nil when assigned to a variable

and it seems that changing the variables from local to global makes no difference and the bug persists.


Solution

  • It seemed that it might have been a incompatibility of the OS I had. Make sure to have the latest OS X and hammerspoon software.