PyObjC Framework: BackgroundAssets

12.1 · active · verified Tue Apr 14

Python wrappers for the macOS BackgroundAssets framework, enabling applications to manage large asset downloads in the background. It is part of the comprehensive PyObjC project, which provides Python bindings for the entire macOS and iOS Cocoa frameworks. Version 12.1 is the latest stable release, with updates typically aligned with macOS SDK releases and Python version support.

Warnings

Install

Imports

Quickstart

This example demonstrates how to create a `BAURLDownloadConfiguration` and a `BAURLDownload` object using the `BackgroundAssets` framework. Note that actually initiating and managing background downloads requires specific macOS application entitlements and a properly configured application delegate, which are outside the scope of a simple Python script.

import Foundation
from BackgroundAssets import BAManager, BAURLDownload, BAURLDownloadConfiguration, BAURLDownloadNetworkActivityCellular

# Get the shared manager instance
manager = BAManager.sharedManager()

# Create a configuration for the download
config = BAURLDownloadConfiguration.new()
config.setIdentifier_("com.example.mybackgrounddownload") # Unique identifier
config.setDiscretionary_(True) # Allow system to decide when to run
config.setRequiresPower_(False) # Does not require external power
config.setRequiresNetworkActivity_(BAURLDownloadNetworkActivityCellular) # Or BAURLDownloadNetworkActivityAny, BAURLDownloadNetworkActivityWiFi

# Create a URL for the download (replace with a real URL for testing)
download_url_str = "https://example.com/some/large/file.zip"
download_url = Foundation.NSURL.URLWithString_(download_url_str)

# Create the download object
# The last argument is an NSError** pointer; use None for Python in non-error cases
download, error = BAURLDownload.downloadWithURL_configuration_error_(download_url, config, None)

if download:
    print(f"Successfully created BAURLDownload: {download.identifier()}")
    print(f"Download URL: {download.URL().absoluteString()}")
    print(f"Configuration identifier: {download.configuration().identifier()}")
    
    # To actually schedule it, you'd add it to the manager. This often requires
    # specific macOS entitlements and an application lifecycle.
    # For a simple runnable snippet, just creating the objects is sufficient.
    # success, error_add = manager.addDownload_error_(download, None)
    # if success: print(f"Added download to manager.")
    # else: print(f"Failed to add download: {error_add}")
else:
    print(f"Failed to create BAURLDownload: {error}")

view raw JSON →