PyObjC Accounts Framework
pyobjc-framework-accounts provides Python bindings for Apple's Accounts framework on macOS. It allows Python applications to access and manage user accounts stored in System Preferences, such as social media, mail, and other internet accounts. The library is part of the larger PyObjC project, which acts as a bridge between Python and Objective-C, and is regularly updated to align with the latest macOS SDKs. The current version is 12.1.
Warnings
- breaking PyObjC versions frequently drop support for older Python versions to align with active Python releases and macOS SDKs. For example, PyObjC 12.0 dropped support for Python 3.9, and 11.0 dropped Python 3.8. Ensure your Python version is compatible with the PyObjC version you are using.
- breaking PyObjC 10.3 introduced a change where `__init__` could no longer be used when `__new__` was provided by PyObjC. While 10.3.1 partially restored this for classes with user-implemented `__new__`, code relying on `__new__` provided by PyObjC still cannot use `__init__`.
- breaking PyObjC 11.1 aligned its behavior with `clang`'s Automatic Reference Counting (ARC) documentation for initializer methods. Methods in the 'init' family now correctly steal a reference to `self` and return a new one, which might change memory management expectations for custom Objective-C classes bridged to Python.
- gotcha Starting with PyObjC 12.1, Key-Value Observing (KVO) usage is automatically disabled for subclasses of `NSProxy` defined in Python to prevent `SystemError`.
- gotcha In PyObjC 10.1, the `os.fspath()` function was updated to work with Cocoa URLs (`NSURL`, `CFURLRef`) that refer to local filesystem paths. It will now raise a `TypeError` for other types of URLs, enabling more robust use of Python's filesystem APIs with Cocoa URLs.
- breaking The `IMServicePlugIn` framework bindings were removed in PyObjC 10.0 because the underlying macOS framework was deprecated in macOS 10.13 and completely removed in macOS 14.
Install
-
pip install pyobjc-framework-accounts
Imports
- Accounts
import Accounts
Quickstart
import Accounts
import objc
# Initialize the ACAccountStore, the primary entry point for the Accounts framework.
# Note: Actual account access (e.g., requesting permissions, fetching accounts)
# often requires a running NSApplication event loop and user interaction on macOS.
# This example demonstrates basic object creation and type identification.
account_store = Accounts.ACAccountStore.alloc().init()
print(f"Initialized ACAccountStore object: {account_store}")
# Get an account type identifier, e.g., for Twitter
twitter_type_identifier = Accounts.ACAccountTypeIdentifierTwitter
print(f"Twitter Account Type Identifier: {twitter_type_identifier}")
# Retrieve the ACAccountType object for Twitter
twitter_account_type = account_store.accountTypeWithIdentifier_(twitter_type_identifier)
if twitter_account_type:
print(f"Retrieved ACAccountType for Twitter: {twitter_account_type.identifier()}")
else:
print(f"Could not retrieve ACAccountType for identifier: {twitter_type_identifier}")
# To request access or fetch accounts, you would typically use a completion handler
# and potentially an active NSApplication runloop in a full Cocoa application.
# For example (illustrative, not fully runnable without more setup):
# import Foundation
# account_store.requestAccessToAccountsWithType_options_completion_(
# twitter_account_type,
# None, # A Python dict for options, or None
# lambda granted, error: Foundation.NSLog(f"Access granted: {granted}, Error: {error}")
# )