PyObjC Framework FinderSync

12.1 · active · verified Tue Apr 14

pyobjc-framework-findersync provides Python wrappers for the macOS FinderSync.framework, allowing Python applications to extend Finder functionality. It is part of the larger PyObjC project, which provides a bridge between Python and Objective-C, enabling Python code to interact with macOS Cocoa frameworks. The PyObjC project, including its framework wrappers, typically releases new versions in alignment with macOS SDK updates and Python version support changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a Python class as a delegate for the `FIFinderSync` protocol and how to configure the `FIFinderSyncController`. Note that a Finder Sync Extension must be packaged within a macOS application bundle and configured via its `Info.plist` to be loaded by the Finder. This script is for illustrative purposes and will not register a functional Finder Sync extension by itself.

import objc
from FinderSync import FIFinderSync, FIFinderSyncController
from Foundation import NSURL
import os

class MyFinderSyncDelegate(FIFinderSync):
    """
    A minimal Finder Sync delegate class.
    In a real Finder Sync Extension, this class would implement methods
    like `requestBadgeIdentifierForURL_` or `toolbarItemImageForURL_`.
    """
    def init(self):
        self = objc.super().init()
        print("MyFinderSyncDelegate initialized.")
        return self

    # Example: You would implement FinderSync methods here.
    # def requestBadgeIdentifierForURL_(self, url):
    #     print(f"Requesting badge for: {url}")
    #     return "MyBadgeIdentifier" # Define this in Info.plist

def setup_finder_sync():
    """
    Illustrates how to configure the Finder Sync Controller.
    Note: This script itself cannot become a Finder Sync Extension.
    A Finder Sync Extension must be embedded within a host application
    bundle and configured via its Info.plist to be loaded by Finder.
    This code demonstrates the PyObjC API usage within such an extension.
    """
    controller = FIFinderSyncController.sharedExtensionController()

    if controller:
        # Create an instance of our delegate
        delegate_instance = MyFinderSyncDelegate.alloc().init()
        controller.setDelegate_(delegate_instance)

        # Specify directories to monitor
        # For demonstration, let's try to monitor the user's Downloads folder
        downloads_path = os.path.expanduser("~/Downloads")
        downloads_url = NSURL.fileURLWithPath_(downloads_path)

        controller.setDirectoryURLs_([downloads_url])

        print(f"FinderSyncController delegate set: {controller.delegate()}")
        print(f"Watching directories: {controller.directoryURLs()}")
        print("\nNote: For this to take effect, this code must be run from within")
        print("a properly configured macOS Finder Sync Extension bundle.")
        print("Simply running this Python script will not register the extension.")
    else:
        print("FIFinderSyncController.sharedExtensionController() returned None.")
        print("This usually means the code is not running within a Finder Sync Extension process.")
        print("A Finder Sync Extension requires specific macOS bundle setup to function.")

if __name__ == "__main__":
    setup_finder_sync()

view raw JSON →