PyObjC CoreSpotlight Framework Bindings

12.1 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates how to index content using the CoreSpotlight framework through PyObjC. It creates two `CSSearchableItem` instances with basic attributes and adds them to the default Spotlight index.

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.")

view raw JSON →