Swift Programming Tools for macOS Accessibility Control
Swift offers several specialized libraries and frameworks that enable developers to interact with macOS applications through the accessibility APIs. These tools provide programmatic control over the macOS environment, making it possible to create assistive technologies, automation tools, and productivity applications.
Core Libraries and Frameworks
AXSwift
AXSwift stands out as a dedicated Swift wrapper for macOS's C-based accessibility client APIs. This library makes working with accessibility features significantly easier by providing:
A modern API written entirely in Swift
Explicit error handling mechanisms
Complete coverage of the underlying C API
Better documentation than Apple's official resources
Straightforward installation via SPM, Carthage, or CocoaPods[1]
Example implementation with SPM:
swift
// In Package.swift
.package(url: "https://github.com/tmandry/AXSwift", from: "0.3.0")
AXSwift is particularly valuable because the native accessibility APIs are notoriously error-prone and difficult to work with directly[1].
WatchEye
WatchEye is a specialized Swift library that sits on top of the macOS Accessibility API, making it easy to:
React to changes in the active application
Detect when window titles change
Work with browsers to extract additional data (like current URLs)
Monitor user activity across applications[3]
To use WatchEye, you need to implement a delegate pattern:
swift
import AppKit
import WatchEye
import Foundation
class ExampleWatchEyeDelegate {
let watchEye: WatchEye
init() {
watchEye = WatchEye()
watchEye.delegate = self
}
}
extension ExampleWatchEyeDelegate: WatchEyeDelegate {
func watchEyeDidReceiveAccessibilityPermissions(_: WatchEye) {
print("Accessibility permissions granted!")
}
func watchEye(_: WatchEye, didFocusApplication app: NSRunningApplication) {
print("\(app.bundleIdentifier!) is now in focus")
}
func watchEye(_: WatchEye, didChangeTitleOf app: NSRunningApplication, newTitle title: String) {
// Handle title changes
}
}
WatchEye requires disabling the App Sandbox and may need additional entitlements for full functionality[3].
MacOS Accessibility Framework
Native Accessibility API
Apple introduced a method-based accessibility API in macOS 10.10, replacing the previous key-based approach. This API provides protocols that define how accessibility clients interact with applications[2].
Key features of the native API include:
Protocols that guide developers through creating accessible controls
Similarity to iOS accessibility API
Backward compatibility with older key-based API[2]
Implementation Example for Permissions
To request accessibility permissions in a macOS app:
swift
let options: NSDictionary = [kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String : true]
let accessEnabled = AXIsProcessTrustedWithOptions(options)
if !accessEnabled {
print("Access Not Enabled")
} else {
print("Access Granted")
}
This code displays a dialog requesting accessibility access for your application[8].
Global Event Monitoring
With proper accessibility permissions, you can monitor global events like keystrokes:
swift
NSEvent.addGlobalMonitorForEvents(matching: NSEvent.EventTypeMask.keyDown) { (event) in
print(event.keyCode)
}
This capability enables applications to respond to user input across the entire system[8].
Platform Considerations
AppKit Integration
For deep macOS integration, many developers combine accessibility features with AppKit:
AppKit provides greater control over macOS-specific capabilities
Can be combined with SwiftUI for modern interfaces
Essential for advanced accessibility implementations[6]
Sandbox Limitations
Applications using accessibility APIs typically need special entitlements:
Accessibility control requires disabling the App Sandbox
Additional permissions like com.apple.security.automation.apple-events may be needed
Users must explicitly grant permission through System Preferences[3][5]
Cross-Platform Considerations
When developing accessibility tools:
Apple platforms include the Objective-C runtime, which is required to access frameworks like AppKit
Swift core libraries provide portable implementations of fundamental Apple frameworks for cross-platform compatibility[10]
Related Projects
Swindler, a framework for building macOS window managers in Swift, is built on top of AXSwift and demonstrates the practical application of accessibility APIs for window management[1].
For more complex applications involving accessibility, the combination of Swift, AppKit, and accessibility APIs provides a powerful toolset for controlling and monitoring the macOS environment.