PyObjC DataDetection Framework

12.1 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates how to use `DDScanner` to detect phone numbers, dates, and email addresses in a given string. It combines multiple detection conditions using the bitwise OR operator (`|`) and then iterates through the `DDScannerResult` objects to extract matched strings and their specific values.

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()}")

view raw JSON →