kerma / defaultbrowser

Command line tool for getting and setting a default browser (HTTP handler) in macOS
MIT License
310 stars 35 forks source link

Is it possible to set the default browser silently? #3

Open prashcr opened 9 years ago

prashcr commented 9 years ago

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.

charlie-wasp commented 8 years ago

It will be nice, I agree. Did you find a way doing that @prashcr ?

sldx commented 7 years ago

maybe an setdefaultbrowser follwed by a scripted click on the button.

tvararu commented 7 years ago

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)

mikew commented 6 years ago

@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.

kerma commented 6 years ago

@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.

p-eh commented 5 years ago

@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.

Could you by any chance share this shell script, would love to give it a try.

gibfahn commented 4 years ago

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.

aoenth commented 3 years ago

@pvieito Doesn't seem like your link is working.

h0adp0re commented 2 years ago

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".

jonasbn commented 10 months ago

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.

  1. Open System Settings
  2. Choose Accessibility
  3. Choose Keyboard on the right hand side
  4. Choose Keyboard Settings...
  5. Toggle Keyboard navigation to 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.