PyObjC CoreSpotlight Framework Bindings
PyObjC is a bridge between Python and Objective-C, enabling Python scripts to use and extend existing Objective-C class libraries, most importantly the Cocoa frameworks by Apple. The `pyobjc-framework-corespotlight` package provides Python bindings for the CoreSpotlight framework, allowing Python applications to interact with macOS Spotlight indexing capabilities. The current version is 12.1, with a regular release cadence often aligning with new macOS SDKs and Python versions.
Warnings
- breaking PyObjC 12.0 dropped support for Python 3.9. Users on Python 3.9 must use PyObjC versions prior to 12.0.
- breaking PyObjC 11.0 dropped support for Python 3.8. Users on Python 3.8 must use PyObjC versions prior to 11.0.
- breaking PyObjC 11.1 aligned with `clang`'s Automatic Reference Counting (ARC) documentation for initializer methods. This changes how PyObjC models `init` family methods, which now correctly steal a reference to `self` and return a new one. Code that made assumptions about reference counting with `alloc`/`init` chains might break.
- gotcha PyObjC 10.3 initially removed support for `__init__` when a user-implemented `__new__` was present, which broke some projects. Version 10.3.1 partially reverted this, allowing `__init__` if a user class or its superclass implements `__new__`, but `__init__` is still not supported when relying on PyObjC's provided `__new__`.
- gotcha Experimental free-threading support (PEP 703) was introduced in Python 3.13 and integrated into PyObjC 11.0. While present, this was an experimental feature in Python and PyObjC, and might have limitations or stability concerns. PyObjC itself did not fully support the experimental free-threading in Python 3.13 at the time of 10.3 release.
- gotcha Prior to PyObjC 10.1, using `os.fspath()` with Cocoa URLs (NSURL, CFURLRef) that did not refer to local filesystem paths would raise a `TypeError`. This was fixed to enable Python filesystem APIs with local filesystem URLs.
Install
-
pip install pyobjc-framework-corespotlight -
pip install pyobjc
Imports
- CoreSpotlight
import CoreSpotlight
- CSSearchableItem
from CoreSpotlight import CSSearchableItem
- CSSearchableItemAttributeSet
from CoreSpotlight import CSSearchableItemAttributeSet
Quickstart
import CoreSpotlight
from Foundation import NSDate
def index_item(item_id, title, description):
# Create an attribute set for the item
attribute_set = CoreSpotlight.CSSearchableItemAttributeSet.alloc().initWithItemContentType_('public.text')
attribute_set.setTitle_(title)
attribute_set.setContentDescription_(description)
attribute_set.setKeywords_(['pyobjc', 'spotlight', 'example'])
attribute_set.setCreationDate_(NSDate.date())
# Create the searchable item
searchable_item = CoreSpotlight.CSSearchableItem.alloc().initWithUniqueIdentifier_domainIdentifier_attributeSet_(
item_id, 'com.example.app.domain', attribute_set
)
# Index the item
CoreSpotlight.CSSearchableIndex.defaultSearchableIndex().indexSearchableItems_completionHandler_(
[searchable_item], None
)
print(f"Indexed item: {title} (ID: {item_id})")
if __name__ == '__main__':
# Example usage
index_item("my-unique-id-1", "My First PyObjC Spotlight Item", "This is a test item indexed via PyObjC CoreSpotlight.")
index_item("my-unique-id-2", "Another Spotlight Entry", "This showcases indexing another piece of content.")