Advertisement
Python253

what_browser_is_default

Mar 16th, 2024
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: what_browser_is_default.py
  4. # Version: 1.00
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script retrieves the default web browser set in the Windows registry.
  9.  
  10. Requirements:
  11. - This script is designed for Windows operating systems.
  12. - Python must be installed on the system.
  13. - The script requires access to the Windows registry.
  14. """
  15.  
  16. import os
  17. import winreg
  18.  
  19. def get_default_browser_windows():
  20.     try:
  21.         # Open the Windows registry key for HTTP associations
  22.         with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice") as key:
  23.             # Read the ProgId value
  24.             prog_id, _ = winreg.QueryValueEx(key, 'ProgId')
  25.             # Open the registry key for the ProgId
  26.             with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, f"\\{prog_id}\\shell\\open\\command") as browser_key:
  27.                 # Read the default browser path
  28.                 browser_path, _ = winreg.QueryValueEx(browser_key, '')
  29.                 # Extract the clean path to the browser executable (remove command-line arguments)
  30.                 browser_path = browser_path.split('"')[1]
  31.                 return os.path.basename(browser_path), browser_path
  32.     except Exception as e:
  33.         print("Error:", e)
  34.         return None, None
  35.  
  36. def main():
  37.     default_browser, default_browser_path = get_default_browser_windows()
  38.     if default_browser:
  39.         print("Default Browser:")
  40.         print("Name:", default_browser)
  41.         print("Path:", default_browser_path)
  42.     else:
  43.         print("Default browser not found.")
  44.  
  45. if __name__ == "__main__":
  46.     main()
  47.  
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement