Created
May 10, 2022 17:22
-
-
Save dmohs/78f80c16caf13edfa0a5b493525e5ade to your computer and use it in GitHub Desktop.
Hammerspoon function to set default browser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setDefaultBrowser(appFileName, notify) | |
local logger = hs.logger.new('defbr', 5) | |
local app = hs.application.open('com.apple.systempreferences') | |
local axapp = hs.axuielement.applicationElement(app) | |
-- Attempt to open the menu until it successfully opens (returns true) | |
hs.timer.waitUntil(function() | |
return app:selectMenuItem({'View', 'General'}) | |
end, function() | |
print('General group opened.') | |
findPopup() | |
end) | |
function findPopup() | |
local dwbPopup = nil | |
-- Attempt to find the popup (combo box) | |
hs.timer.doUntil(function() return dwbPopup end, function() | |
axapp:elementSearch(function(msg, results) | |
if results then dwbPopup = results[1] end | |
end, function(e) | |
return e:attributeValue('AXDescription') == 'Default Web Browser popup' | |
end) | |
end) | |
hs.timer.waitUntil(function() return dwbPopup end, function() | |
print('Popup found.') | |
chooseAppFileName(dwbPopup) | |
end) | |
end | |
function chooseAppFileName(dwbPopup) | |
dwbPopup:performAction('AXShowMenu') | |
dwbPopup:elementSearch(function(msg, results) | |
local item = results[1] | |
item:performAction('AXPress') | |
notify() | |
end, function(e) | |
return e:attributeValue('AXTitle') == appFileName | |
end) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Re adding details, it's hinted at in the first line of the Notes (• This method utilizes coroutines to keep Hammerspoon responsive), but you're right, it's not clear and just mentions slow when
includeParents
is set. I'll give it some thought to see how best to re-word it -- I have some time coming up in the next couple of weeks that I plan to devote to Hammerspoon and will clarify it then.Glad that it helps! I have a couple of tools I use when trying to narrow
axuielement
searches and I'm hoping to provide some wiki articles showing and discussing them during the same period.