Open prashcr opened 9 years ago
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: https://github.com/tvararu/dotfiles/commit/2d3bcf47ba8baffe6b829a166fc53fbf8453677f
The confirm-system-dialog.scpt
is compiled using Script Editor.app from:
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)
@tvararu Thanks for that!
@kerma Would you like a PR that adds this "silent" functionality? Currently I have it working, but my defaultbrowser-silent
is just a shell script that calls defaultbrowser
and osascript
.
@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.
@tvararu Thanks for that!
@kerma Would you like a PR that adds this "silent" functionality? Currently I have it working, but my
defaultbrowser-silent
is just a shell script that callsdefaultbrowser
andosascript
.
Could you by any chance share this shell script, would love to give it a try.
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 ./script chrome
for example.
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 System Preferences > Security & Privacy > Privacy
. Since I do that by default anyway, it's a non-issue for me.
confirm-macos-dialog
:
#!/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
:
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: first button whose name starts with "use"
.
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.
System Settings
Accessibility
On
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.