Skip to content

Commit cf66a9b

Browse files
authored
Fix startup when browser app manifests are weird on MacOS and Linux + BUILD
1 parent 0a5d6a2 commit cf66a9b

1 file changed

Lines changed: 47 additions & 38 deletions

File tree

modules/globals.py

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def _():
7676
try:
7777
name = winreg.QueryValue(browsers, key)
7878
args = shlex.split(winreg.QueryValue(browsers, key + "\\shell\\open\\command"))
79-
if args and pathlib.Path(shutil.which(args[0])).exists():
79+
if args and shutil.which(args[0]) and pathlib.Path(shutil.which(args[0])).exists():
8080
Browser.add(name, args=args)
8181
except Exception:
8282
pass # Non-standard key
@@ -88,51 +88,60 @@ def _():
8888
elif os is Os.Linux:
8989
import configparser
9090
for xdg_dir in _os.environ.get("XDG_DATA_DIRS", "/usr/share/").split(":"):
91-
if not xdg_dir:
92-
continue
93-
app_dir = pathlib.Path(xdg_dir) / "applications"
94-
apps_file = app_dir / "mimeinfo.cache"
95-
if not apps_file.is_file():
96-
continue
97-
raw = apps_file.read_bytes()
98-
apps = []
99-
for match in re.finditer(rb"x-scheme-handler/https?=(.+)", raw):
100-
for app in match.group(1).split(b";"):
101-
app = str(app, encoding="utf-8")
102-
if app and app not in apps:
103-
apps.append(app)
104-
for app in apps:
105-
app_file = app_dir / app
106-
if not app_file.is_file():
91+
try:
92+
if not xdg_dir:
93+
continue
94+
app_dir = pathlib.Path(xdg_dir) / "applications"
95+
apps_file = app_dir / "mimeinfo.cache"
96+
if not apps_file.is_file():
10797
continue
108-
parser = configparser.RawConfigParser()
109-
parser.read(app_file)
110-
name = parser.get("Desktop Entry", "Name")
111-
args = [arg for arg in shlex.split(parser.get("Desktop Entry", "Exec")) if not (len(arg) == 2 and arg.startswith("%"))]
112-
if args and pathlib.Path(shutil.which(args[0])).exists():
113-
Browser.add(name, args=args)
98+
raw = apps_file.read_bytes()
99+
apps = []
100+
for match in re.finditer(rb"x-scheme-handler/https?=(.+)", raw):
101+
for app in match.group(1).split(b";"):
102+
app = str(app, encoding="utf-8")
103+
if app and app not in apps:
104+
apps.append(app)
105+
for app in apps:
106+
try:
107+
app_file = app_dir / app
108+
if not app_file.is_file():
109+
continue
110+
parser = configparser.RawConfigParser()
111+
parser.read(app_file)
112+
name = parser.get("Desktop Entry", "Name")
113+
args = [arg for arg in shlex.split(parser.get("Desktop Entry", "Exec")) if not (len(arg) == 2 and arg.startswith("%"))]
114+
if args and shutil.which(args[0]) and pathlib.Path(shutil.which(args[0])).exists():
115+
Browser.add(name, args=args)
116+
except Exception:
117+
pass
118+
except Exception:
119+
pass
114120
elif os is Os.MacOS:
115121
import plistlib
116122
app_dir = pathlib.Path("/Applications")
117123
empty = []
118124
matches = ["http", "https"]
119125
for app in app_dir.glob("*.app"):
120-
app_file = app / "Contents/Info.plist"
121-
if not app_file.is_file():
122-
continue
123-
parser = plistlib.loads(app_file.read_bytes())
124-
found = False
125-
for handler in parser.get("CFBundleURLTypes", empty):
126-
for scheme in handler.get("CFBundleURLSchemes", empty):
127-
if scheme in matches:
128-
name = parser["CFBundleName"]
129-
args = [app / f"Contents/MacOS/{parser['CFBundleExecutable']}"]
130-
if args and pathlib.Path(shutil.which(args[0])).exists():
131-
Browser.add(name, args=args)
132-
found = True
126+
try:
127+
app_file = app / "Contents/Info.plist"
128+
if not app_file.is_file():
129+
continue
130+
parser = plistlib.loads(app_file.read_bytes())
131+
found = False
132+
for handler in parser.get("CFBundleURLTypes", empty):
133+
for scheme in handler.get("CFBundleURLSchemes", empty):
134+
if scheme in matches:
135+
name = parser["CFBundleName"]
136+
args = [app / f"Contents/MacOS/{parser['CFBundleExecutable']}"]
137+
if args and shutil.which(args[0]) and pathlib.Path(shutil.which(args[0])).exists():
138+
Browser.add(name, args=args)
139+
found = True
140+
break
141+
if found:
133142
break
134-
if found:
135-
break
143+
except Exception:
144+
pass
136145
_()
137146

138147
# Check self launch command and startup settings

0 commit comments

Comments
 (0)