Lua Hammerspoon: hs.window.focusedWindow () is zero when assigning a variable

I am using automation software called hammerspoon in osx.

When I use the following code in the hammerspoon console, win is nil:

 > local win = hs.window.focusedWindow() > win nil 

But actually the function returns some value:

 > hs.window.focusedWindow() hs.window: Hammerspoon Console (0x60000025f798) 

This strange behavior violates all functions of moving / scaling a window, such as:

 hs.hotkey.bind({"cmd", "alt", "ctrl"}, "H", function() local win = hs.window.focusedWindow() local f = win:frame() fx = fx - 10 win:setFrame(f) end) 

Hammerspoon gives this error:

 /Users/mertnuhoglu/.hammerspoon/init.lua:6: attempt to index a nil value (local 'win') stack traceback: /Users/mertnuhoglu/.hammerspoon/init.lua:6: in function </Users/mertnuhoglu/.hammerspoon/init.lua:4> stack traceback: 

I do not know if this problem is caused by my computer or something else.

I have osx yosemite, version 10.10.5 and hammerspoon 0.9.43.

Update:

I found a solution to the error. This is due to the osx privacy settings.

Decision:

 Prefences > Security > Privacy > Allow Apps: Hammerspoon 

But still, I don't understand why hs.window.focusedWindow() returns something if it is not assigned to a variable, and returns nil when it is assigned to a variable.

+6
source share
1 answer

Hammerspoon executes each line as its own piece, so local variables are available only in this fragment, and not after the piece has been executed.

If you want to access the variables after the piece is executed, make them global, i.e. cancel the keyword "local".

+2
source

All Articles