UniformTypeIdentifiers Framework (PyObjC)
PyObjC is a comprehensive bridge that enables Python developers to interact with Objective-C frameworks on macOS. The `pyobjc-framework-uniformtypeidentifiers` package provides Python wrappers for Apple's UniformTypeIdentifiers framework, which allows applications to work with system-declared and custom Uniform Type Identifiers (UTIs) to describe file types, data formats, and other resources. The library is currently at version 12.1 and maintains an active release cadence, often aligning with macOS and Python version updates.
Warnings
- breaking PyObjC frequently drops support for older Python versions. PyObjC 12.0 dropped Python 3.9 support, and PyObjC 11.0 dropped Python 3.8 support. Version 12.1 corrected packaging metadata that incorrectly showed support for Python 3.9, which could lead to installation failures on that version.
- breaking PyObjC 11.1 aligned its core bridge with `clang`'s Automatic Reference Counting (ARC) documentation for initializer methods. Methods in the 'init' family now correctly steal a reference to `self` and return a new reference. This changed behavior for `[NSObject alloc]` proxies and can affect custom `init` implementations, potentially leading to crashes if old patterns are used.
- gotcha Version 10.3 initially broke `__init__` usage for Python subclasses of Objective-C classes, particularly when a user implemented `__new__` or when relying on PyObjC's `__new__` behavior. This was partially reverted in 10.3.1 to reintroduce `__init__` support when a user implements `__new__`.
- gotcha `NSString` and `NSMutableString` are marked as 'final' in PyObjC versions 11.1 and later. Attempting to subclass these in Python will result in crashes due to special handling within PyObjC.
- gotcha Installing PyObjC (especially from source or if binary wheels aren't suitable) requires Xcode or the Command Line Tools to be installed on macOS, including a compatible SDK for the target macOS version.
- gotcha PyObjC is a macOS-specific library and does not function on other platforms like iOS, Linux, or Windows.
Install
-
pip install pyobjc-framework-uniformtypeidentifiers
Imports
- UniformTypeIdentifiers
import UniformTypeIdentifiers
Quickstart
import UniformTypeIdentifiers
from Foundation import NSURL # Foundation is often implicitly needed or useful
# Get a system-declared UTI for an image
image_type = UniformTypeIdentifiers.UTType.image
print(f"Image UTI Identifier: {image_type.identifier}")
print(f"Image UTI Preferred filename extension: {image_type.preferredFilenameExtension}")
# Check conformance
text_type = UniformTypeIdentifiers.UTType.text
is_text_conforming_to_data = text_type.conformsToType_(UniformTypeIdentifiers.UTType.data)
print(f"Does text UTI conform to data UTI? {is_text_conforming_to_data}")
# Get a UTI from a filename extension
pdf_type = UniformTypeIdentifiers.UTType.typeWithFilenameExtension_("pdf")
if pdf_type:
print(f"PDF UTI Identifier: {pdf_type.identifier}")