1

I'm working to add accessibility to a custom UI element in an existing macOS codebase. We've got a custom error window that appears when an error occurs, and I'm trying to get VoiceOver to announce the error when it occurs.

Currently, I'm trying to use the NSAccessibility.post(element: Any, notification: NSAccessibility.Notification, userInfo: [NSAccessibility.NotificationUserInfoKey : Any]?) method, but while the code is run, the notification is not spoken.

Here's what I've currently got in my NSViewController:

NSAccessibility.post(element: self, notification: .announcementRequested, userInfo: [
    .announcement: NSLocalizedString("CANT_CONNECT_ERROR", comment: "Error string for connection failure"),
    .priority: NSAccessibilityPriorityLevel.high
])

My expectation was that, using the .announcementRequested option, VoiceOver should automatically pick this up and speak the announcement, but no luck. I've tried using different notification types, tried putting in a hardcoded string value in the .announcement argument, and tried using .rawValue and 90 for the .priority argument.

1
  • Does it help if you post the notificcation against NSApp instead of self? The documentation indicates it should be posted against NSApp but some archived accessibility-dev conversation suggests maybe it needs to be posted against a window or something instead. Maybe try a few different things. Jul 15, 2019 at 21:55

3 Answers 3

6

So I finally figured out how to get this to work after much trial and error (and danielpunkass's helpful comments). YMMV, but here's how I got it to work:

  1. The element argument has to be set to NSApp.mainWindow (I've added as Any as well to silence a compiler warning)
  2. The priority level in the userInfo dictionary has to be set using the .rawValue, not the actual enum value. Apple's documentation does not mention this of course.

Here's my working code:

NSAccessibility.post(
    element: NSApp.mainWindow as Any,
    notification: .announcementRequested,
    userInfo: [
        .announcement: "This is a custom accessibility notification",
        .priority: NSAccessibilityPriorityLevel.high.rawValue
    ]
)
1

An old conversation on the Accessibility-Dev mailing list suggests that this notification may need to be directed at NSApp's mainWindow:

https://lists.apple.com/archives/accessibility-dev/2013/Jul/msg00027.html

Maybe this will do the trick for you as well?

2
  • Thanks for the tip Daniel. I always forget to check the old mailing list for things like this. I tried the suggestions, but unfortunately wasn't able to get it to work by referencing NSApp or the main window, or any permutation.
    – hisaac
    Jul 16, 2019 at 14:08
  • Bah, sorry to hear that! I wish it were easier to debug VoiceOver's behavior when it doesn't work as expected. Jul 17, 2019 at 15:14
1

I was looking for a way to announce text on macOS and hisaac's answer solved my issue. Since I have multi-platform app, I created some helper extensions which I can use across iOS, macOS and tvOS. They can be improved for sure, especially in terms of naming, but since there is anyway not so much around when it comes to accessibility, I thought I share anything I have:

import SwiftUI

#if os(macOS)
public typealias PlatformAccessibility = NSAccessibility
#else
public typealias PlatformAccessibility = UIAccessibility
#endif

public enum AccessibilityNotification {
    case platformAnnouncement
    
    fileprivate var platformNotification: PlatformAccessibility.Notification {
        switch self {
#if os(macOS)
        case .platformAnnouncement:
            return .announcementRequested
#else
        case .platformAnnouncement:
            return .announcement
#endif
        }
    }
}

public extension PlatformAccessibility {
    
    static var isVoiceOverEnabled: Bool {
#if os(macOS)
        NSWorkspace.shared.isVoiceOverEnabled
#else
        UIAccessibility.isVoiceOverRunning
#endif
    }
    
    static func post(notification: AccessibilityNotification, argument: Any?, delay: Double = 0, queued: Bool = false) {
#if os(macOS)
        DispatchQueue.main.asyncAfter(deadline: .now() + delay + (queued ? 1 : 0)) {
            NSAccessibility.post(
                element: NSApp.mainWindow as Any,
                notification: notification.platformNotification,
                userInfo: [
                    .announcement: argument as Any,
                    .priority: NSAccessibilityPriorityLevel.high.rawValue
                ]
            )
        }
#else
        DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
            var message = argument
            if let argument = argument as? String, queued {
                message = NSAttributedString(string: argument, attributes: [.accessibilitySpeechQueueAnnouncement: true])
            }
            PlatformAccessibility.post(notification: notification.platformNotification, argument: message)
        }
#endif
    }
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.