PyObjC ImageCaptureCore Framework Bindings
This library provides Python bindings for Apple's ImageCaptureCore.framework, enabling Python applications to interact with scanners, cameras, and other image capture devices on macOS. It is part of the larger PyObjC project, which wraps various macOS frameworks. The current version is 12.1, with releases tightly coupled to macOS SDK updates and Python version support.
Warnings
- breaking PyObjC versions are tightly coupled with supported Python versions. PyObjC 12.0 dropped support for Python 3.9, requiring Python 3.10 or newer. PyObjC 11.0 dropped support for Python 3.8.
- gotcha PyObjC framework bindings are macOS-specific. Attempting to import or use `pyobjc-framework-imagecapturecore` on non-macOS operating systems will result in import errors or runtime failures.
- gotcha Many ImageCaptureCore operations are asynchronous and rely on delegate callbacks. To receive these callbacks and keep your application responsive, you must run a Cocoa event loop (e.g., using `PyObjCTools.AppHelper.runConsoleEventLoop()` for console apps or within a GUI application's main loop).
- gotcha When subclassing Objective-C classes in Python with PyObjC, the interaction between `__new__` and `__init__` can be nuanced. If your Python subclass (or one of its superclasses) explicitly defines `__new__`, then `__init__` will be called. However, if `__new__` is *not* explicitly overridden by the user, `__init__` will *not* be called. This can lead to objects not being initialized as expected.
- gotcha PyObjC 11.1 introduced a change in how it models initializer methods to align with Clang's Automatic Reference Counting (ARC). Methods in the 'init' family (e.g., `init` methods) now correctly 'steal' the reference to `self` and return a new reference. This change can affect complex object graphs or manual reference counting scenarios in Python subclasses.
Install
-
pip install pyobjc-framework-imagecapturecore -
pip install pyobjc
Imports
- ImageCaptureCore
import ImageCaptureCore
- ICDeviceBrowser
from ImageCaptureCore import ICDeviceBrowser
Quickstart
import ImageCaptureCore
import objc
from PyObjCTools import AppHelper
class MyDeviceBrowserDelegate(objc.NSObject):
def deviceBrowser_didAddDevice_moreComing_(self, browser, device, moreComing):
print(f"[*] Found device: {device.name()} (UUID: {device.uuid()})")
# You can access device properties here, e.g., device.type(), device.icon().
def deviceBrowser_didRemoveDevice_moreComing_(self, browser, device, moreComing):
print(f"[-] Removed device: {device.name()} (UUID: {device.uuid()})")
def main():
browser = ImageCaptureCore.ICDeviceBrowser.alloc().init()
delegate = MyDeviceBrowserDelegate.alloc().init()
browser.setDelegate_(delegate)
print("Starting ImageCaptureCore device browser. Press Ctrl+C to exit.")
print("Detected devices will be printed as they are found or removed.")
browser.start()
# Run the Cocoa event loop indefinitely to receive delegate callbacks.
# This keeps the script alive and responsive to system events.
AppHelper.runConsoleEventLoop(installInterrupt=True)
if __name__ == "__main__":
main()