Created
May 20, 2022 01:39
-
-
Save AWtnb/44fd1eef8431810f7f2a9e11f8831c64 to your computer and use it in GitHub Desktop.
get default browser path with Python (on Windows10)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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