PyObjC LinkPresentation Framework

12.1 · active · verified Tue Apr 14

PyObjC is a bridge between Python and Objective-C, enabling Python scripts to interact with macOS Cocoa frameworks. This specific package, `pyobjc-framework-linkpresentation`, provides Python wrappers for Apple's LinkPresentation framework on macOS, allowing developers to fetch and present rich link previews from URLs. It is actively maintained with regular releases, currently at version 12.1.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to fetch rich link metadata from a URL using `LPMetadataProvider` and how to create custom `LPLinkMetadata` objects. It includes a mechanism to handle the asynchronous nature of `startFetchingMetadataForURL_completionHandler_` using a `threading.Event` to wait for the Objective-C callback.

import LinkPresentation
import Foundation
import objc
import threading

# Use a threading.Event to signal when the metadata fetching is complete
done_fetching = threading.Event()
metadata_result = None
error_result = None

def completion_handler(metadata, error):
    global metadata_result, error_result
    metadata_result = metadata
    error_result = error
    done_fetching.set() # Signal that fetching is done

@objc.python_block
def py_completion_handler(metadata, error):
    completion_handler(metadata, error)

def fetch_link_metadata(url_string):
    global metadata_result, error_result
    metadata_result = None
    error_result = None
    done_fetching.clear()

    url = Foundation.NSURL.URLWithString_(url_string)
    if not url:
        print(f"Invalid URL: {url_string}")
        return

    provider = LinkPresentation.LPMetadataProvider.alloc().init()
    provider.startFetchingMetadataForURL_completionHandler_(url, py_completion_handler)

    # Wait for the completion handler to be called
    print(f"Fetching metadata for {url_string}...")
    if not done_fetching.wait(timeout=10): # Wait up to 10 seconds
        print("Fetching timed out.")
        return

    if error_result:
        print(f"Error fetching metadata: {error_result}")
    elif metadata_result:
        print("Metadata fetched successfully:")
        print(f"  Title: {metadata_result.title()}")
        print(f"  URL: {metadata_result.URL()}")
    else:
        print("No metadata or error received.")

if __name__ == "__main__":
    # Fetch metadata for a remote URL
    test_url = os.environ.get('TEST_URL', 'https://www.apple.com')
    fetch_link_metadata(test_url)
    
    # Create and inspect custom metadata
    print("\nCreating custom metadata:")
    custom_metadata = LinkPresentation.LPLinkMetadata.alloc().init()
    custom_metadata.setOriginalURL_(Foundation.NSURL.URLWithString_("https://example.com/custom"))
    custom_metadata.setURL_(Foundation.NSURL.URLWithString_("https://example.com/custom"))
    custom_metadata.setTitle_("My Custom Link Title")
    print(f"  Title: {custom_metadata.title()}")
    print(f"  URL: {custom_metadata.URL()}")

view raw JSON →