hammerspoon: window/filter.lua throws "attempt to index a nil value" when trying to start a global watcher

I am writing a spoon that iterates over all the windows on your screens and re-positions them. This is the code loop that spawns the error:

    app:activate()
    local windows = hs.window.filter.new(appTitle):getWindows()

    for k, v in pairs(windows) do
        obj.logger.d('Positioning window ' .. v:id() .. ' of app ' .. appTitle)
        v:moveToScreen(screen)
        v:setFrame(frame, 0)
    end

Randomly, the attempt dies out leaving this error from the Hammerspoon console:

2020-09-17 21:39:39: ********
2020-09-17 21:39:39: 21:39:39 ERROR:   LuaSkin: hs.menubar:setClickCallback() callback error: ...n.app/Contents/Resources/extensions/hs/window/filter.lua:1561: attempt to index a nil value (field 'watcher')
stack traceback:
	...n.app/Contents/Resources/extensions/hs/window/filter.lua:1561: in upvalue 'startGlobalWatcher'
	...n.app/Contents/Resources/extensions/hs/window/filter.lua:1722: in upvalue 'start'
	...n.app/Contents/Resources/extensions/hs/window/filter.lua:1838: in method 'getWindows'
	...s/luis/.hammerspoon/Spoons/ArrangeDesktop.spoon/init.lua:38: in function 'ArrangeDesktop._positionApp'
	...s/luis/.hammerspoon/Spoons/ArrangeDesktop.spoon/init.lua:58: in function 'ArrangeDesktop.arrange'
	...s/luis/.hammerspoon/Spoons/ArrangeDesktop.spoon/init.lua:83: in function <...s/luis/.hammerspoon/Spoons/ArrangeDesktop.spoon/init.lua:83>
2020-09-17 21:39:39: ********

The line throwing this error is global.watcher:start() with global.watcher being set a few lines above by global.watcher = appwatcher.new(appEvent). I tried going into the appwatcher.new code to see what happens there and why it might be returning nil at times. However, I’m not as familiar with the code in watcher.m to make a reasonable guess.

It seems like wrapping the global.watcher:start() in a guard clause and only running it if global.watcher is not nil would solve my issue, but I’m not sure what, if any, side effects that would have. Again, this seems to happen randomly. It could happen after any number of windows being positioned or not at all. If there is any other information I could provide, happy to help diagnose this.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 22 (14 by maintainers)

Most upvoted comments

@sprak3000 Those “errors” are actually just log messages about an accessibility item that background applications traditionally don’t have – they can be safely ignored and are actually removed by #2505 which should be in the next release.

Ok, I can confirm that #2485 does not fix this, so we still have something unexpected going on. I’ll keep digging and let you know what I find.

I’m getting this error with 0.9.79 and my config is not loading. I had to downgrade to 0.9.78.