I try to use Hammerspoon to open a new window in Firefox with the following script:
function newWindow()
local app = hs.application.find("Firefox")
print(hs.inspect.inspect(app))
print(app:title())
print(app:bundleID())
local item = app:findMenuItem("File")
print(item)
end
hs.hotkey.bind({'alt', 'ctrl', 'cmd'}, 'n', newWindow)
While the script is able to find Firefox, it is not able to find the menu item I am looking for. But at the same time, I am able to use app:getMenuItems()
to retrieve the whole menu structure.
Does anyone have an idea why or an working example for any application?
I am using MacOS Big Sur 11.2.3
I think what you are looking for is the app:selectMenuItem()
method:
function newWindow()
local app = hs.application.find("Firefox")
app:selectMenuItem({"File", "New Window"})
end
hs.hotkey.bind({'alt', 'ctrl', 'cmd'}, 'n', newWindow)
hs.application:selectMenuItem(menuitem[, isRegex]) -> true or nil
Selects a menu item (i.e. simulates clicking on the menu item)
True if the menu item was found and selected, or nil if it wasn't (e.g. because the menu item couldn't be found)
Depending on the type of menu item involved, this will either activate or tick/untick the menu item
http://www.hammerspoon.org/docs/hs.application.html#selectMenuItem
-- EDIT --
To avoid multiple language menu structure you can also open the new window using the keyboard shortcut for that:
function newWindow()
local app = hs.application.find("Firefox")
hs.eventtap.keyStroke({'cmd'}, 'N', nil, app)
end
hs.hotkey.bind({'alt', 'ctrl', 'cmd'}, 'n', newWindow)