-
Notifications
You must be signed in to change notification settings - Fork 36
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
Is it possible to set the default browser silently? #3
Comments
It will be nice, I agree. Did you find a way doing that @prashcr ? |
maybe an setdefaultbrowser follwed by a scripted click on the button. |
The system dialog that appears is a security feature from macOS and it isn't possible to circumvent its appearance AFAICT. You can write an AppleScript to click on the dialog; I've done what @sldx describes here: tvararu/dotfiles@2d3bcf4 The try
tell application "System Events"
tell application process "CoreServicesUIAgent"
tell window 1
tell (first button whose name starts with "use")
perform action "AXPress"
end tell
end tell
end tell
end tell
end try You will need to use admin privileges to give your terminal emulator (iTerm2 in my case) permission from System Preferences > Security & Privacy > Accessibility. (the dialog pops up only once the first time you use the script) |
@mikew if you can figure out how to call osascript inside the ObjC code I'd be happy to include it. If this is not possible I'd include the osascript and shell script separately under usage examples in README. |
To compile the applescript you can use: # Run once to create the applescript app.
echo \
'try
tell application "System Events"
tell application process "CoreServicesUIAgent"
tell window 1
tell (first button whose name starts with "use")
perform action "AXPress"
end tell
end tell
end tell
end tell
end try
' >confirm-system-dialog.applescript
osacompile -o confirm-system-dialog.app confirm-system-dialog.applescript Then to change the default just create a bash script like: # Run every time you want to change
./defaultbrowser "$1"
osascript ./confirm-system-dialog.app And then run with Obviously you want to move the app and binary to somewhere in the path. |
@pvieito Doesn't seem like your link is working. |
FWIW I solved the issue by creating a shell script instead of compiling an app. The only caveat with this solution is that you need to grant accessibility rights to your terminal emulator in
#!/usr/bin/env bash
osascript -e 'tell application "System Events"
tell application process "CoreServicesUIAgent"
tell window 1
tell (first button whose name starts with "use")
perform action "AXPress"
end tell
end tell
end tell
end tell' where you use defaultbrowser $1 &> /dev/null &&
confirm-macos-dialog &> /dev/null I think this could be solved in this project but the flashing dialog would not be pretty nonetheless, so I'm not sure it should be solved in this project. What is more, specifically this part of the script might differ between macOS versions: |
My workaround to this has been to enable keyboard navigation in macOS. Then I just have to press tab and space to complete the operation and dismiss the dialogue, it is not silent, but you do not have to switch away from the keyboard.
And you are good to go. I have a write-up here with screenshots. This does not solve the issue of the dialogue popping up, but it does make it easier to dismiss it easily. |
I wanted to use this in a python script so I was hoping to find a silent way to change my default browser, without the annoying pop-up.
I tried using
sudo defaultbrowser -set <browser>
but it doesn't set the browser in that case and it's probably a bad idea too.The text was updated successfully, but these errors were encountered: