2

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.

Hotschke
  • 9,402
  • 6
  • 46
  • 53
Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
  • What do you mean it "works" "once it focuses on the console"? Do you mean it works when you have a window focused but doesn't otherwise? Does that privacy setting bit help any? – Etan Reisner Mar 15 '16 at 20:16
  • @EtanReisner it works as in it moves the console around the screen as it should, though it only moves it around as it should iff I am focused on the console screen. – Charlie Parker Mar 16 '16 at 21:13
  • @EtanReisner it seems updating my OS X to the most recent OS fixed this bug. Such a trivial solution, not sure if there might be anything else wrong but so far so good. – Charlie Parker Mar 16 '16 at 21:14
  • If there isn't a focused window then you can't expect `hs.window.focusedWindow()` to return anything. So it returning `nil` in that case makes perfect sense. – Etan Reisner Mar 17 '16 at 12:38
  • @EtanReisner but there **was** a focused window. – Charlie Parker Mar 17 '16 at 16:14
  • Then I didn't understand your comment about the console window. Are you saying that when you focus the hammerspoon console window it moves some *other* window? But that if you have the other window focused nothing moves and you get the `nil` error? – Etan Reisner Mar 17 '16 at 17:26
  • @EtanReisner sorry. What I meant to say exactly was, that it only moves the windows around when I focused on the cosole window. If I focus on any other window it would open the console window without me asking it to do so and display an error message. – Charlie Parker Mar 17 '16 at 19:54
  • 2
    Ah, ok. That sounds like the accessibility feature not being allowed for hammerspoon then I think (there's a FAQ about that I believe). But if an upgrade got it working who knows. – Etan Reisner Mar 17 '16 at 19:59

2 Answers2

0

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.

Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
0

This happened to me and was solved by granting "Accessibility" permissions to Hammerspoon in the "Security & Privacy" panel in OSX System Preferences.

Mich
  • 1