New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to set global varable in Hammerspoon Lua #3601
Comments
I found how to solve this issue using rawset
|
It works fine on my Hammerspoon. Do you have any code in your config (or loaded Spoons) that would be setting a metatable on the global environment? What does this code print for you when pasted into the console? local mt = getmetatable(_G)
if mt then
print(hs.inspect(mt))
for k,v in pairs(mt) do
if type(v) == 'function' then
local info = debug.getinfo(v)
if what ~= 'C' then
print(hs.inspect(info))
end
end
end
else
print('no metatable on _G')
end |
I get this:
2024-02-12 16:03:37: no metatable on _G
… On 12 Feb 2024, at 16:01, Rhys-T ***@***.***> wrote:
local mt = getmetatable(_G)
if mt then
print(hs.inspect(mt))
for k,v in pairs(mt) do
if type(v) == 'function' then
local info = debug.getinfo(v)
if what ~= 'C' then
print(hs.inspect(info))
end
end
end
else
print('no metatable on _G')
end
|
Weird… When you ran into the issue, was it in the console or the config file? If it was the config, try putting the code I sent there instead of in the console and see if that prints anything different. Actually, just in case local function checkMT(name, tbl)
local mt = getmetatable(tbl)
if mt then
print(hs.inspect(mt))
for k,v in pairs(mt) do
if type(v) == 'function' then
local info = debug.getinfo(v)
if what ~= 'C' then
print(hs.inspect(info))
end
end
end
else
print('no metatable on '..name)
end
end
checkMT('_G', _G)
if rawequal(_ENV, _G) then
print '_ENV is _G'
else
print '_ENV is not _G'
checkMT('_ENV', _ENV)
end |
This the result of running this code directly in the console:
local function checkMT(name, tbl)
local mt = getmetatable(tbl)
if mt then
print(hs.inspect(mt))
for k,v in pairs(mt) do
if type(v) == 'function' then
local info = debug.getinfo(v)
if what ~= 'C' then
print(hs.inspect(info))
end
end
end
else
print('no metatable on '..name)
end
end
checkMT('_G', _G)
if rawequal(_ENV, _G) then
print '_ENV is _G'
else
print '_ENV is not _G'
checkMT('_ENV', _ENV)
end
2024-02-12 16:29:06: no metatable on _G
2024-02-12 16:29:06: _ENV is _G
… On 12 Feb 2024, at 16:27, Rhys-T ***@***.***> wrote:
Weird… When you ran into the issue, was it in the console or the config file? If it was the config, try putting the code I sent there instead of in the console and see if that prints anything different.
Actually, just in case _G isn't what's getting used for some reason, can you try using this version instead?
local function checkMT(name, tbl)
local mt = getmetatable(tbl)
if mt then
print(hs.inspect(mt))
for k,v in pairs(mt) do
if type(v) == 'function' then
local info = debug.getinfo(v)
if what ~= 'C' then
print(hs.inspect(info))
end
end
end
else
print('no metatable on '..name)
end
end
checkMT('_G', _G)
if rawequal(_ENV, _G) then
print '_ENV is _G'
else
print '_ENV is not _G'
checkMT('_ENV', _ENV)
end
—
Reply to this email directly, view it on GitHub <#3601 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/BFGZ5IQG5AVTV2HAK336BQ3YTIYHDAVCNFSM6AAAAABDC4FR62VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZYHA4TKMBUGE>.
You are receiving this because you authored the thread.
|
What about when you run it from within your |
2024-02-12 16:45:02: no metatable on _G
2024-02-12 16:45:02: _ENV is _G
2024-02-12 16:45:02: -- Done.

… On 12 Feb 2024, at 16:41, Rhys-T ***@***.***> wrote:
What about when you run it from within your reset and/or inc functions? Also, is there anywhere you're declaring counter and/or offset as locals (or function argument names)?
—
Reply to this email directly, view it on GitHub <#3601 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/BFGZ5IQ5KKTV7VLBDBRQZPDYTIZZ3AVCNFSM6AAAAABDC4FR62VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZYHEZDMMJQGI>.
You are receiving this because you authored the thread.
|
I don't know of anything else that could interfere with setting global variables. As far as I know, Hammerspoon should be using an unmodified Lua interpreter. The only other thing I can think of to try right now is moving your entire |
I’ll maybe try this but don’t worry because I found a workaround
… On 12 Feb 2024, at 17:57, Rhys-T ***@***.***> wrote:
I don't know of anything else that could interfere with setting global variables. As far as I know, Hammerspoon should be using an unmodified Lua interpreter.
The only other thing I can think of to try right now is moving your entire init.lua file aside and making a new one with just the code from your initial post. If that works correctly, we can try to narrow down exactly where it starts breaking. If it still fails, I'm not sure what to do.
—
Reply to this email directly, view it on GitHub <#3601 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/BFGZ5IV7N7L54IOWXX4Y5PTYTJCYDAVCNFSM6AAAAABDC4FR62VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZZGE2DKNRUGY>.
You are receiving this because you authored the thread.
|
This is the content of my init.lua:
hyper = {"cmd","alt","ctrl"}
function updateCounter(value)
counter = counter + value
menu:setTitle("{" .. counter .. "}")
hs.alert.show(counter)
end
function reset()
menu:setTitle("{" .. counter .. "}")
hs.alert.show(counter)
end
counter, offset = 0, 1
menuMenu = {
{ title = "Increase", fn = function() updateCounter(offset) end },
{ title = "Decrease", fn = function() updateCounter(-offset) end },
{ title = "-" },
{ title = "Set offset (" .. offset .. ")" , fn = function() offset = setOffset(offset) end },
{ title = "Reset counter", fn = function() offset = reset() end }
}
menu = hs.menubar.new()
menu:setMenu(menuMenu)
menu:autosaveName("counter")
menu:setTooltip("Counter")
reset()
hs.hotkey.bind(hyper, "$", function()
updateCounter(offset)
end)
hs.hotkey.bind(hyper, "^", function()
updateCounter(-offset)
end)
… On 12 Feb 2024, at 17:57, Rhys-T ***@***.***> wrote:
I don't know of anything else that could interfere with setting global variables. As far as I know, Hammerspoon should be using an unmodified Lua interpreter.
The only other thing I can think of to try right now is moving your entire init.lua file aside and making a new one with just the code from your initial post. If that works correctly, we can try to narrow down exactly where it starts breaking. If it still fails, I'm not sure what to do.
—
Reply to this email directly, view it on GitHub <#3601 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/BFGZ5IV7N7L54IOWXX4Y5PTYTJCYDAVCNFSM6AAAAABDC4FR62VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZZGE2DKNRUGY>.
You are receiving this because you authored the thread.
|
I see a couple of issues in that code that might explain why the counter isn't updating properly:
These next two aren't directly related to the counter problem, but:
hyper = {"cmd","alt","ctrl"}
hyperShift = {"cmd","alt","ctrl","shift"}
hs.hotkey.bind(hyperShift, "4", function()
updateCounter(offset)
end)
hs.hotkey.bind(hyperShift, "6", function()
updateCounter(-offset)
end)
menuMenu[4].title = "Set offset (" .. offset .. ")"
menu:setMenu(menuMenu) in your function menuMenu()
return {
{ title = "Increase", fn = function() updateCounter(offset) end },
{ title = "Decrease", fn = function() updateCounter(-offset) end },
{ title = "-" },
{ title = "Set offset (" .. offset .. ")" , fn = function() offset = setOffset(offset) end },
{ title = "Reset counter", fn = function() offset = reset() end }
}
end |
Thx for your feedback and your patience!
Concerning both keys (« $ » and « ^ »), they are direct own keys on a French keyboard!
This is the revised code which works fine now with or without using [declare()]:
…--[[
function declare (name, initval)
rawset(_G, name, initval or false)
end
--]]
function setOffset(value)
value = tostring(offset)
local button, text = hs.dialog.textPrompt("Set offset value", "New value:", "" .. value, "Set", "Cancel")
if button ~= "Cancel" then
text = string.match(text, '%d+')
if text == nil then text = "1" end
offset = tonumber(text)
menu:setMenu(menuMenu)
end
return offset
end
function updateCounter(value)
print(value)
if value == nil then return end
counter = counter + value
menu:setTitle("{" .. counter .. "}")
hs.alert.show(counter)
end
function reset()
counter = 0
-- declare("counter", 0)
menu:setTitle("{" .. counter .. "}")
hs.alert.show(counter)
end
counter, offset = 0, 1
-- declare("counter", 0)
-- declare("offset", 1)
function menuMenu()
return {
{ title = "Increase", fn = function() updateCounter(offset) end },
{ title = "Decrease", fn = function() updateCounter(-offset) end },
{ title = "-" },
{ title = "Set offset (" .. offset .. ")" , fn = function() offset = setOffset(offset) end },
{ title = "Reset counter", fn = function() reset() end }
}
end
menu = hs.menubar.new()
menu:setMenu(menuMenu())
menu:autosaveName("counter")
menu:setTooltip("Counter")
reset()
hs.hotkey.bind(hyper, "$", function()
updateCounter(offset)
end)
hs.hotkey.bind(hyper, "^", function()
updateCounter(-offset)
end)
On 12 Feb 2024, at 21:53, Rhys-T ***@***.***> wrote:
I see a couple of issues in that code that might explain why the counter isn't updating properly:
I don't see a definition for that setOffset function anywhere, and even if it exists, any change it makes to offset will get overwritten by the offset = part of the menu item's fn - changing it to whatever setOffset's return value was.
This version of reset doesn't seem to actually set counter at all - it just updates the menu's title and shows the alert. Also, it's setting offset to reset's return value, which is nil - meaning that any Increase/Decrease commands after that point will throw an attempt to perform arithmetic on a nil value error and not actually change the counter.
These next two aren't directly related to the counter problem, but:
$ and ^ aren't their own keys, at least on most keyboard layouts that I know of, so they don't have keycodes defined in hs.keycodes.map and can't be passed directly to hs.hotkey.bind like that. Assuming that they're Shift-4 and Shift-6 like on my keyboard, you'll need to do something like:
hyper = {"cmd","alt","ctrl"}
hyperShift = {"cmd","alt","ctrl","shift"}
hs.hotkey.bind(hyperShift, "4", function()
updateCounter(offset)
end)
hs.hotkey.bind(hyperShift, "6", function()
updateCounter(-offset)
end)
"Set offset (" .. offset .. ")": This title will get evaluated once when your config first runs, and won't update when the offset is changed. You can either include something like:
menuMenu[4].title = "Set offset (" .. offset .. ")"
menu:setMenu(menuMenu)
in your setOffset function, or change menuMenu to a function so it gets re-evaluated each time the menu opens:
function menuMenu()
return {
{ title = "Increase", fn = function() updateCounter(offset) end },
{ title = "Decrease", fn = function() updateCounter(-offset) end },
{ title = "-" },
{ title = "Set offset (" .. offset .. ")" , fn = function() offset = setOffset(offset) end },
{ title = "Reset counter", fn = function() offset = reset() end }
}
end
—
Reply to this email directly, view it on GitHub <#3601 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/BFGZ5IUHZCZ45H5XAUW436LYTJ6LPAVCNFSM6AAAAABDC4FR62VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZZGU2TIOBZGU>.
You are receiving this because you authored the thread.
|
Glad to hear it's working for you.
Gotcha. For some reason I wasn't sure if Hammerspoon knew that, though, but I see now that it does properly update |
The following code runs well under Lua:
... but running the same code under Hammerspoon Lua doesn't work as expected, the last print statement gives 2, meaning that the global variable counter has not been reseted.
Thx for your help
The text was updated successfully, but these errors were encountered: