0
  • I have some app
  • This app must to have accessibility access (to use global hotkeys)
  • There is no problem to open Accessibility preferences window (System Settings -> Privacy and Security -> Accessibility)

But user must to click "+" button manually, to search my app on HDD and to add my app manually to the list:

enter image description here

I'm trying to ADD my app to this list.

Solution was found, but this solution is the reason of app crashes.

After adding the following keys to FileBoDebug.entitlements & FileBo.entitlements:

enter image description here

App is constantly crashed on each app run (Debug/Release builds both):

enter image description here

Details:


Could not launch “FileBo”
Domain: IDELaunchErrorDomain
Code: 20
Recovery Suggestion: The LaunchServices launcher has returned an error. Please check the system logs for the underlying cause of the error.
User Info: {
    DVTErrorCreationDateKey = "2023-07-31 02:12:39 +0000";
    DVTRadarComponentKey = 968756;
    IDERunOperationFailingWorker = IDELaunchServicesLauncher;
}
--
The operation couldn’t be completed. Launch failed.
Domain: RBSRequestErrorDomain
Code: 5
Failure Reason: Launch failed.
--
Launchd job spawn failed
Domain: NSPOSIXErrorDomain
Code: 153
--

Analytics Event: com.apple.dt.IDERunOperationWorkerFinished : {
    "device_model" = "MacBookAir10,1";
    "device_osBuild" = "13.3.1 (22E261)";
    "device_platform" = "com.apple.platform.macosx";
    "launchSession_schemeCommand" = Run;
    "launchSession_state" = 1;
    "launchSession_targetArch" = arm64;
    "operation_duration_ms" = 159;
    "operation_errorCode" = 20;
    "operation_errorDomain" = IDELaunchErrorDomain;
    "operation_errorWorker" = IDELaunchServicesLauncher;
    "operation_name" = IDERunOperationWorkerGroup;
    "param_consoleMode" = 0;
    "param_debugger_attachToExtensions" = 0;
    "param_debugger_attachToXPC" = 1;
    "param_debugger_type" = 3;
    "param_destination_isProxy" = 0;
    "param_destination_platform" = "com.apple.platform.macosx";
    "param_diag_MainThreadChecker_stopOnIssue" = 0;
    "param_diag_MallocStackLogging_enableDuringAttach" = 0;
    "param_diag_MallocStackLogging_enableForXPC" = 1;
    "param_diag_allowLocationSimulation" = 1;
    "param_diag_checker_tpc_enable" = 1;
    "param_diag_gpu_frameCapture_enable" = 0;
    "param_diag_gpu_shaderValidation_enable" = 0;
    "param_diag_gpu_validation_enable" = 0;
    "param_diag_memoryGraphOnResourceException" = 0;
    "param_diag_queueDebugging_enable" = 1;
    "param_diag_runtimeProfile_generate" = 0;
    "param_diag_sanitizer_asan_enable" = 0;
    "param_diag_sanitizer_tsan_enable" = 0;
    "param_diag_sanitizer_tsan_stopOnIssue" = 0;
    "param_diag_sanitizer_ubsan_stopOnIssue" = 0;
    "param_diag_showNonLocalizedStrings" = 0;
    "param_diag_viewDebugging_enabled" = 1;
    "param_diag_viewDebugging_insertDylibOnLaunch" = 1;
    "param_install_style" = 0;
    "param_launcher_UID" = 2;
    "param_launcher_allowDeviceSensorReplayData" = 0;
    "param_launcher_kind" = 0;
    "param_launcher_style" = 99;
    "param_launcher_substyle" = 8192;
    "param_runnable_appExtensionHostRunMode" = 0;
    "param_runnable_productType" = "com.apple.product-type.application";
    "param_testing_launchedForTesting" = 0;
    "param_testing_suppressSimulatorApp" = 0;
    "param_testing_usingCLI" = 0;
    "sdk_canonicalName" = "macosx13.3";
    "sdk_osVersion" = "13.3";
    "sdk_variant" = macos;
}
--


System Information

macOS Version 13.3.1 (Build 22E261)
Xcode 14.3 (21812) (Build 14E222b)
Timestamp: 2023-07-31T05:12:39+03:00

also there are no new logs in Console.


Also I'm tried to use AxIsProcessTrustedWithOptions - but with no results, you can see it on the video

Andrew_STOP_RU_WAR_IN_UA
  • 9,318
  • 5
  • 65
  • 101
  • I never heard of this key so you should take that other answer with a grain of salt. That said, this looks like something that belongs in Info.plist instead of entitlements. Did you try that? – TheNextman Aug 01 '23 at 16:13
  • @TheNextman yep, but nothing happened. App just run and there are no FileBo added into the Accessibility list – Andrew_STOP_RU_WAR_IN_UA Aug 01 '23 at 17:04
  • I think that `AxIsProcessTrustedWithOptions` is supposed to add your app to the list, it's strange that it doesn't in your case – TheNextman Aug 02 '23 at 13:28
  • @TheNextman but it is fact that it does not in my case: https://drive.google.com/file/d/1WH0UwQJotx1qO4SKhCXto3gapIEtVJ1c/view – Andrew_STOP_RU_WAR_IN_UA Aug 02 '23 at 16:00
  • Like @TheNextman, I doubt that `NSUserInterfaceAccessibility` exists. I haven't found any trace of that key in documentation or Google. – soundflix Aug 09 '23 at 10:16
  • @soundflix absolutely right. The same. But my previous question on SO was closed because of duplicate to question with such solution. World is unfair :) – Andrew_STOP_RU_WAR_IN_UA Aug 09 '23 at 10:23

1 Answers1

3

I have do research according your question, and found this answer in which if you run the app it will show app on Accessibility and found this solution and this solution is not crashing. The original method (which I've left at the bottom of this post) still prompted for accessibility on each new build of the app unfortunately.

I had to get this working in my macOS app and had another idea and way to approach it, given the user needs to action it anyway, I just have the app present the required dialogue on each run and during the app build, I run a script to clear the existing permissions. (github link -> https://github.com/othyn/macos-auto-clicker#gear-preferences-storage) run this app and check accordionly.

//
//  PermissionsService.swift
//  auto-clicker
//
//  Created by Ben on 10/04/2022.
//

import Cocoa

final class PermissionsService: ObservableObject {
    // Store the active trust state of the app.
    @Published var isTrusted: Bool = AXIsProcessTrusted()

    // Poll the accessibility state every 1 second to check
    //  and update the trust status.
    func pollAccessibilityPrivileges() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            self.isTrusted = AXIsProcessTrusted()

            if !self.isTrusted {
                self.pollAccessibilityPrivileges()
            }
        }
    }

    // Request accessibility permissions, this should prompt
    //  macOS to open and present the required dialogue open
    //  to the correct page for the user to just hit the add 
    //  button.
    static func acquireAccessibilityPrivileges() {
        let options: NSDictionary = [kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: true]
        let enabled = AXIsProcessTrustedWithOptions(options)
    }
}

add this code in App Delegate

//
//  AppDelegate.swift
//  auto-clicker
//
//  Created by Ben on 30/03/2022.
//

import Foundation
import Cocoa

final class AppDelegate: NSObject, NSApplicationDelegate {
    // When the application finishes launching, request the
    //  accessibility permissions from the service class we
    //  made earlier.
    func applicationDidFinishLaunching(_ notification: Notification) {
        PermissionsService.acquireAccessibilityPrivileges()
    }
}

implement this code in your view to get feedback to the user that we are waiting for permissions and listen to that isTrusted update value

//
//  AutoClickerApp.swift
//  auto-clicker
//
//  Created by Ben on 12/05/2021.
//

import Foundation
import SwiftUI
import Defaults

@main
struct AutoClickerApp: App {
    // Create an instance of the permissions service class that we
    //  can observe for the trusted state change.
    @StateObject private var permissionsService = PermissionsService()

    var body: some Scene {
        // Wait for trust permissions being granted from the user,
        //  displaying a blocking permissions view telling the user
        //  what to do and then presenting the main application view
        //  automatically when the required trust permissions are granted.
        WindowGroup {
            if self.permissionsService.isTrusted {
                MainView()
            } else {
                PermissionsView()
            }
            .onAppear(perform: self.permissionsService.pollAccessibilityPrivileges)
        }
    }
}

Added Images for reference. I hope it will solve your problem.

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Then in order to automatically clear the permissions during the app build, I added a new build script to the project that runs the following against /bin/sh:

tccutil reset Accessibility $PRODUCT_BUNDLE_IDENTIFIER

As seen in auto-clicker.xcodeproj/project.pbxproj (line 435).

check this link -> Accessibility Permissions for Development in macOS and XCode

enter image description here

Updated: fix the issue with your code please find attach images for reference.

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Chandaboy
  • 1,302
  • 5
  • 10
  • This code does not work. Even will not build because of WindowGroup code :) Also you in your answer never called `PermissionsService.acquireAccessibilityPrivileges()`. I did fixes in the code - but it still your solution does not work. =( Did you check this code in empty project before posting the answer? – Andrew_STOP_RU_WAR_IN_UA Aug 09 '23 at 01:43
  • You can check your solution here: https://github.com/ukushu/PermissionsTest by your own hands – Andrew_STOP_RU_WAR_IN_UA Aug 09 '23 at 01:43
  • but when I compile and run then it will giving me permission then on accebility, user can able to On and Off and its not crashing ... i guess this is what you are looking for – Chandaboy Aug 09 '23 at 04:54
  • @Andrew_STOP_RU_WAR_IN_UA I have checked, you need to add build script to the project and then runs it then it will show it on the accesbitlity section, and please check my updated answer – Chandaboy Aug 09 '23 at 06:11
  • Possibly we see different behavior. Please, look at this: https://drive.google.com/file/d/1vJR5PATZw_0Rei2bZjL-9o-cJ5sVb4LR/view?usp=drive_link – Andrew_STOP_RU_WAR_IN_UA Aug 09 '23 at 10:27
  • @Andrew_STOP_RU_WAR_IN_UA I have fix your github project, can u share me your email address so that I will share u the code – Chandaboy Aug 09 '23 at 10:29
  • yes send u please check, also i have updated answer with latest fixes in your project – Chandaboy Aug 09 '23 at 10:43