This project provides C# bindings for the macOS Accessibility API, specifically from the
Application Services framework and the
AXUIElement.h header file. It includes elements such as the AXUIElement struct and the
AXUIElementCreateApplication(pid) function.
These bindings enable developers to interact with the macOS Accessibility API in a simplified and managed way, avoiding the need to work directly with low-level native APIs.
Currently, there is no NuGet package available. To use the bindings, follow these steps:
- Download the source code.
- Add the
AccessibilityBindings.csprojproject to your solution. - Reference the
AccessibilityBindings.csprojproject in your consuming project by adding the following reference:<ProjectReference Include="..\AccessibilityBindings\AccessibilityBindings.csproj" />
- Use the API in your code (see examples below).
To interact with accessibility UI elements, follow these steps:
-
Check and Request Permissions
Ensure your application has the required accessibility permissions. If not, request the user to grant them:if (!AccessibilityBindings.Accessibility.IsProcessTrusted(true)) { // Permissions not granted. Request the user to grant them and wait for confirmation. }
-
Obtain the Process ID (
pid)
Identify the process ID of the application you want to interact with. For example, locate the running application using its bundle identifier:const string safariAppBundleId = "com.apple.Safari"; AxUiElement safariPid = NSRunningApplication.GetRunningApplications(safariAppBundleId) .FirstOrDefault()?.ProcessIdentifier;
-
Create the Root
AXUIElementInstance
Use theAXUIElementCreateApplication()function to create an instance representing the application's root object:AxUiElement axApp = AxUiElement.CreateApplication(safariPid);
-
Traverse and Manipulate the UI Element Tree
Once you have the root element, you can access and manipulate the application's UI elements:AxUiElement mainWindow = axApp.GetMainWindow(); // Get the main window NSArray<AxUiElement> windows = axApp.GetWindows(); // Get all application windows NSArray<AxUiElement> childElements = mainWindow.GetChildren(); // Get child elements (e.g., labels, buttons) NSString title = mainWindow.CopyAttributeValue<NSString>(AXAttribute.Title); // Get the title attribute value NSString title = mainWindow.GetTitle(); // Shortcut to get the title attribute value
-
SafariSample
Launches Safari, scans the UI element tree to locate the address bar, and navigates Safari to a specified URL. Requests accessibility permissions if required. -
SafariWithUiSample
Similar toSafariSample, but includes a full-featured UI using View, ViewController, etc. This demonstrates handling permission checks in applications with complex user interfaces. -
PhotoshopSample
Detects specific modal dialogs (e.g., error or warning dialogs) in Adobe Photoshop and automatically clicks specified buttons within these dialogs by sending theAXPressaction to the button elements.
- If you are debugging with Rider, grant accessibility permissions to Rider, not to the application being debugged.
- When running the application outside of Rider, ensure that the application itself has the required permissions.
There is an issue about adding such a functionality to the standard library of the Xamarin.Mac (net-macos).