Skip to content
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

Open
prashcr opened this issue Jul 8, 2015 · 10 comments
Open

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

prashcr opened this issue Jul 8, 2015 · 10 comments

Comments

@prashcr
Copy link

prashcr commented Jul 8, 2015

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
Copy link

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

@sldx
Copy link

sldx commented Oct 13, 2016

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

@tvararu
Copy link

tvararu commented Aug 1, 2017

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 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
Copy link

mikew commented Jan 25, 2018

@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
Copy link
Owner

kerma commented Jan 26, 2018

@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
Copy link

p-eh commented Jun 24, 2019

@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
Copy link

gibfahn commented Nov 11, 2019

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
Copy link

aoenth commented Jul 9, 2021

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

@h0adp0re
Copy link

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
Copy link
Contributor

jonasbn commented Nov 15, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests