file association fixes macOS
This commit is contained in:
@@ -337,45 +337,6 @@ def _register_macos(ext: str, app_name: str, bundle_id: str) -> Tuple[bool, str]
|
||||
if not app_bundle_path.endswith(".app"):
|
||||
return False, "Could not locate outer .app bundle."
|
||||
|
||||
info_plist_path = os.path.join(app_bundle_path, "Contents", "Info.plist")
|
||||
if not os.path.exists(info_plist_path):
|
||||
return False, f"Info.plist not found at {info_plist_path}"
|
||||
|
||||
clean_ext = ext.lstrip('.')
|
||||
uti = f"{bundle_id}.{clean_ext}"
|
||||
|
||||
try:
|
||||
with open(info_plist_path, "rb") as f:
|
||||
plist = plistlib.load(f)
|
||||
|
||||
# Tell macOS this app can open files with our UTI
|
||||
doc_types = plist.get("CFBundleDocumentTypes", [])
|
||||
if not any(uti in dt.get("LSItemContentTypes", []) for dt in doc_types):
|
||||
doc_types.append({
|
||||
"CFBundleTypeName": f"{app_name} Project File",
|
||||
"CFBundleTypeRole": "Editor",
|
||||
"LSHandlerRank": "Owner",
|
||||
"LSItemContentTypes": [uti],
|
||||
})
|
||||
plist["CFBundleDocumentTypes"] = doc_types
|
||||
|
||||
# Declare the UTI itself (required — without this the type is unknown to LS)
|
||||
exported_types = plist.get("UTExportedTypeDeclarations", [])
|
||||
if not any(t.get("UTTypeIdentifier") == uti for t in exported_types):
|
||||
exported_types.append({
|
||||
"UTTypeIdentifier": uti,
|
||||
"UTTypeDescription": f"{app_name} File",
|
||||
"UTTypeConformsTo": ["public.data"],
|
||||
"UTTypeTagSpecification": {"public.filename-extension": [clean_ext]},
|
||||
})
|
||||
plist["UTExportedTypeDeclarations"] = exported_types
|
||||
|
||||
with open(info_plist_path, "wb") as f:
|
||||
plistlib.dump(plist, f)
|
||||
|
||||
except Exception as e:
|
||||
return False, f"Failed to update Info.plist: {str(e)}"
|
||||
|
||||
try:
|
||||
lsregister_path = (
|
||||
"/System/Library/Frameworks/CoreServices.framework/Frameworks/"
|
||||
@@ -385,9 +346,9 @@ def _register_macos(ext: str, app_name: str, bundle_id: str) -> Tuple[bool, str]
|
||||
[lsregister_path, "-f", app_bundle_path],
|
||||
check=True, capture_output=True,
|
||||
)
|
||||
return True, f"Registered {ext} with {app_bundle_path} via macOS Launch Services!"
|
||||
return True, f"Refreshed Launch Services registration for {app_bundle_path}."
|
||||
except subprocess.CalledProcessError as e:
|
||||
stderr = e.stderr.decode(errors="ignore") if e.stderr else str(e)
|
||||
return False, f"macOS Launch Services registration failed: {stderr}"
|
||||
return False, f"macOS Launch Services refresh failed: {stderr}"
|
||||
except Exception as e:
|
||||
return False, f"macOS Registration failed: {str(e)}"
|
||||
Reference in New Issue
Block a user