Total Complexity | 6 |
Total Lines | 41 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import plistlib |
||
2 | from pathlib import Path |
||
3 | |||
4 | import log |
||
5 | |||
6 | from . import script |
||
7 | |||
8 | FUNCTIONS = Path(__file__).parent / "browser.applescript" |
||
9 | PREFERENCES = ( |
||
10 | Path.home() |
||
11 | / "Library" |
||
12 | / "Preferences" |
||
13 | / "com.apple.LaunchServices/com.apple.launchservices.secure.plist" |
||
14 | ) |
||
15 | |||
16 | NAMES = { |
||
17 | "com.apple.safari": "Safari", |
||
18 | "com.google.chrome": "Google Chrome", |
||
19 | "org.mozilla.firefox": "Firefox", |
||
20 | } |
||
21 | DEFAULT = NAMES["com.google.chrome"] |
||
22 | |||
23 | |||
24 | def detect(data: dict | None = None) -> str: |
||
25 | if data is None: |
||
26 | with PREFERENCES.open("rb") as fp: |
||
27 | data = plistlib.load(fp) |
||
28 | |||
29 | for handler in data.get("LSHandlers", []): |
||
30 | if handler.get("LSHandlerURLScheme") == "http": |
||
31 | role = handler["LSHandlerRoleAll"] |
||
32 | return NAMES[role] |
||
33 | |||
34 | log.warn("Unable to determine the default browser") |
||
35 | return DEFAULT |
||
36 | |||
37 | |||
38 | def close(name: str = "") -> bool: |
||
39 | replacements = {DEFAULT: name or detect()} |
||
40 | return script.call(FUNCTIONS, "close()", replacements) |
||
41 |