PyObjC Accounts Framework

12.1 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates how to import the `Accounts` framework and initialize the core `ACAccountStore` object, which is the entry point for interacting with user accounts. It also shows how to retrieve a specific account type. Note that actual access to user accounts and handling asynchronous operations (like requesting access) typically requires a more complete PyObjC/Cocoa application setup with a running event loop, and will often involve user prompts for permission.

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}")
# )

view raw JSON →