Skip to content

Instantly share code, notes, and snippets.

@AWtnb
Created May 20, 2022 01:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AWtnb/44fd1eef8431810f7f2a9e11f8831c64 to your computer and use it in GitHub Desktop.
Save AWtnb/44fd1eef8431810f7f2a9e11f8831c64 to your computer and use it in GitHub Desktop.
get default browser path with Python (on Windows10)
import re
from winreg import HKEY_CURRENT_USER, HKEY_CLASSES_ROOT, OpenKey, QueryValueEx
def get_browser_name() -> str:
register_path = r'Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice'
with OpenKey(HKEY_CURRENT_USER, register_path) as key:
return str(QueryValueEx(key, "ProgId")[0])
def format_cmd(s:str) -> str:
exe_path = re.sub(r"(^.+exe)(.*)", r"\1", s)
return exe_path.replace('"', "")
def get_exe_path(name:str) -> str:
register_path = r'{}\shell\open\command'.format(name)
fullpath = ""
with OpenKey(HKEY_CLASSES_ROOT, register_path) as key:
cmd = str(QueryValueEx(key, "")[0])
fullpath = format_cmd(cmd)
return fullpath
def main():
prog_name = get_browser_name()
return get_exe_path(prog_name)
if __name__ == '__main__':
print(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment