Last active
April 20, 2021 21:17
-
-
Save ffoxin/9d6bfcadbae4b2fc1ff0be1ccb76bf58 to your computer and use it in GitHub Desktop.
wget -O- -q 'raw-link-to-script/get_macos_browsers.py' | python3 -
This file contains hidden or 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
""" | |
Lists all browsers in MacOS with browser name and supported protocols. | |
Useful for custom rules in Finicky (https://github.com/johnste/finicky). | |
""" | |
import json | |
import operator | |
import plistlib | |
from pathlib import Path | |
from typing import Dict, Any, Optional, List | |
import pkg_resources | |
dependencies = [ | |
'pydantic', | |
] | |
try: | |
pkg_resources.require(dependencies) | |
except (pkg_resources.DistributionNotFound, pkg_resources.VersionConflict) as e: | |
print('Requirement not installed, please use virtualenv with "pip install {}"'.format(' '.join(dependencies))) | |
exit(1) | |
else: | |
from pydantic import BaseModel | |
class AppMetaInfo(BaseModel): | |
name: Optional[str] | |
url_types: List[Dict[str, Any]] | |
plist_path: Path | |
def check_dir(dir_path: Path) -> Optional[AppMetaInfo]: | |
if not dir_path.exists(): | |
return | |
if not dir_path.is_dir(): | |
return | |
if dir_path.name.endswith('.app'): | |
plist_path = dir_path / 'Contents' / 'Info.plist' | |
if not plist_path.exists(): | |
raise RuntimeError('Unexpected app config: no Info.plist found', dir_path, plist_path) | |
with plist_path.open('rb') as f: | |
plist_data = plistlib.load(f) | |
plist_url_types = plist_data.get('CFBundleURLTypes') | |
if not plist_url_types: | |
return | |
for name_id in ['CFBundleName', 'CFBundleGetInfoString']: | |
name = plist_data.get(name_id) | |
if name: | |
break | |
yield AppMetaInfo( | |
name=name, | |
url_types=plist_url_types, | |
plist_path=plist_path, | |
) | |
else: | |
for sub_dir in sorted(dir_path.iterdir(), key=operator.attrgetter('name')): | |
yield from check_dir(sub_dir) | |
def main(): | |
apps = Path('/Applications') | |
for app in check_dir(apps): | |
print(app.name) | |
for url_type in app.url_types: | |
print('-', url_type.get('CFBundleURLName', json.dumps(url_type))) | |
print(' ', ', '.join(url_type.get('CFBundleURLSchemes', []))) | |
print('-' * 40) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment