PyObjC DataDetection Framework
PyObjC is a bridge between Python and the Objective-C runtime, allowing Python scripts to interact with macOS frameworks. `pyobjc-framework-datadetection` provides Python bindings for the DataDetection framework on macOS, enabling programmatic detection of data such as phone numbers, dates, and addresses in text. The current version is 12.1, with releases typically aligning with new macOS SDKs and Python versions.
Warnings
- breaking PyObjC has periodically dropped support for older Python versions. PyObjC 12.0 dropped Python 3.9, and PyObjC 11.0 dropped Python 3.8. Ensure your Python environment meets the `>=3.10` requirement.
- breaking Changes in `__init__` and `__new__` behavior can break custom class implementations. PyObjC 10.3 initially removed `__init__` support when `__new__` was provided by PyObjC, which was partially reverted in 10.3.1 to allow `__init__` when the user implements `__new__`.
- gotcha PyObjC 11.1 aligned its `alloc`/`init` behavior with Clang's Automatic Reference Counting (ARC) documentation. This means methods in the 'init' family now correctly steal a reference to `self` and return a new one. This can subtly change object lifecycle behavior and may require adjustments in specific patterns of object creation and retention.
- gotcha PyObjC's support for Python 3.13's experimental free-threading (PEP 703) is not yet fully stable or recommended. While significant changes were made in PyObjC 11.0 for experimental support, caution is advised for production use.
- gotcha The `os.fspath()` function will not work with `NSURL` or `CFURLRef` objects that refer to local filesystem paths in PyObjC versions prior to 10.1, raising a `TypeError` for other URLs. This was fixed in PyObjC 10.1.
Install
-
pip install pyobjc-framework-datadetection
Imports
- DDScanner
from DataDetection import DDScanner
- DDConditionIsPhoneNumber
from DataDetection import DDConditionIsPhoneNumber
- DDScannerResult
from DataDetection import DDScannerResult
- NSString
from Foundation import NSString
Quickstart
from DataDetection import DDScanner, DDConditionIsPhoneNumber, DDConditionIsDate, DDConditionIsEmail
from Foundation import NSString # Can be used for explicit Objective-C string creation, though Python strings often bridge automatically
# Constants for content types (exposed directly from the DataDetection module)
PHONE_NUMBER = DDConditionIsPhoneNumber
DATE = DDConditionIsDate
EMAIL = DDConditionIsEmail
text = "My phone number is 123-456-7890 and the date is January 15, 2024. Email me at test@example.com."
# Create a scanner for phone numbers, dates, and emails
# The | operator is used to combine multiple conditions
scanner = DDScanner.scannerWithContentTypes_(PHONE_NUMBER | DATE | EMAIL)
# Scan the string
# PyObjC automatically converts Python strings to NSString for Objective-C APIs
results = scanner.scanString_(text)
print(f"Scanning text: '{text}'")
print("Found results:")
for result in results:
print(f" Matched String: '{result.matchedString()}'")
print(f" Type (raw): {result.type()}") # Raw Objective-C type constant
# The 'value()' method returns an Objective-C object specific to the detected type
if result.type() == PHONE_NUMBER:
print(f" Value (Phone Number): {result.value()}")
elif result.type() == DATE:
print(f" Value (Date): {result.value()}")
elif result.type() == EMAIL:
print(f" Value (Email): {result.value()}")