defaultbrowser icon indicating copy to clipboard operation
defaultbrowser copied to clipboard

Is it possible to set the default browser silently?

Open prashcr opened this issue 10 years ago • 10 comments

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.

prashcr avatar Jul 08 '15 10:07 prashcr

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

charlie-wasp avatar Jan 28 '16 13:01 charlie-wasp

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

sldx avatar Oct 13 '16 13:10 sldx

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 avatar Aug 01 '17 17:08 tvararu

@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 avatar Jan 25 '18 20:01 mikew

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

kerma avatar Jan 26 '18 07:01 kerma

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

mega-3000 avatar Jun 24 '19 22:06 mega-3000

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.

gibfahn avatar Nov 11 '19 17:11 gibfahn

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

aoenth avatar Jul 09 '21 14:07 aoenth

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

h0adp0re avatar Jan 25 '22 22:01 h0adp0re

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.

  • http://jonasbn.github.io/til/osx/change_default_browser_from_cli.html

This does not solve the issue of the dialogue popping up, but it does make it easier to dismiss it easily.

jonasbn avatar Nov 15 '23 15:11 jonasbn