PyObjC Framework DictionaryServices
pyobjc-framework-dictionaryservices provides Python wrappers for the macOS DictionaryServices framework, enabling Python applications to interact with the system's dictionary and thesaurus. It is part of the larger PyObjC project, which actively maintains bindings for various macOS frameworks, with releases typically tied to new macOS SDKs and Python version support. The current version is 12.1.
Warnings
- deprecated The underlying macOS DictionaryServices framework is deprecated by Apple. The PyPI page for this package recommends using the 'CoreServices' package instead for modern development. While the PyObjC bindings are still maintained, users should be aware of the upstream deprecation.
- breaking PyObjC frequently drops support for older Python versions with major releases. For instance, PyObjC 12.0 dropped support for Python 3.9, and PyObjC 11.0 dropped support for Python 3.8.
- gotcha Changes in the handling of `__init__` when a class implements `__new__` were introduced in PyObjC 10.3, leading to breaks in some projects. While partially reverted in 10.3.1 to allow `__init__` usage in certain scenarios, this remains a potential issue for custom Objective-C classes in Python.
- breaking PyObjC 11.1 aligned its behavior for initializer methods (those in the 'init' family) with Clang's Automatic Reference Counting (ARC) documentation. This means PyObjC now correctly models that 'init' methods steal a reference to `self` and return a new reference, which can change reference counting behavior.
- gotcha Using the DictionaryServices framework on macOS 10.12 with a `python.org` binary has been known to cause interpreter crashes.
- gotcha Prior to PyObjC 10.1, `os.fspath()` would not work correctly with Cocoa URLs (`NSURL`, `CFURLRef`) that refer to local filesystem paths, raising a `TypeError` for other URLs. This was fixed to enable regular Python filesystem APIs with such URLs.
- gotcha Instances of `bytearray` can now be used as arguments for functions or selectors expecting a null-terminated C `char` array. This is a behavioral change that might affect existing code relying on previous type handling.
Install
-
pip install pyobjc-framework-dictionaryservices
Imports
- DCSCopyTextDefinition
from CoreServices import DCSCopyTextDefinition
- CFRange
from CoreServices import CFRange
Quickstart
import Foundation
import CoreServices
import objc
word = "recursive"
# Create a CFStringRef from a Python string
string_ref = Foundation.CFString.stringWithString_(word)
# Define a range covering the entire string
full_range = CoreServices.CFRange(0, len(word))
# Look up the definition using DCSCopyTextDefinition
definition_ref = CoreServices.DCSCopyTextDefinition(string_ref, full_range)
if definition_ref:
# Convert CFStringRef to Python string and print
definition = str(definition_ref)
print(f"Definition of '{word}':\n{definition}")
# Release the CFStringRef returned by DCSCopyTextDefinition (retained by 'Copy')
Foundation.CFRelease(definition_ref)
else:
print(f"No definition found for '{word}'.")
# Release the input CFStringRef
Foundation.CFRelease(string_ref)