Skip to content

Commit a783fd1

Browse files
committed
Fixes requesting accessibility permissions on first run
1 parent 5464ec1 commit a783fd1

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

ax/Sources/AXHelper/main.swift

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,35 @@ func debug(_ message: String) {
2929
// Check accessibility permissions
3030
func checkAccessibilityPermissions() {
3131
debug("Checking accessibility permissions...")
32-
33-
// SKIP THE CHECK TEMPORARILY to debug SIGTRAP issues
34-
debug("⚠️ ACCESSIBILITY CHECK DISABLED FOR DEBUGGING")
35-
return
36-
37-
// Original code below
38-
/*
39-
// Use the constant directly as a String to avoid concurrency issues
40-
let checkOptPrompt = "AXTrustedCheckOptionPrompt" as CFString
41-
let options = [checkOptPrompt: true] as CFDictionary
42-
let accessEnabled = AXIsProcessTrustedWithOptions(options)
43-
32+
33+
// Check without prompting. The prompt can cause issues for command-line tools.
34+
let accessEnabled = AXIsProcessTrusted()
35+
4436
if !accessEnabled {
45-
print("Error: This application requires accessibility permissions.")
46-
print("Please enable them in System Preferences > Privacy & Security > Accessibility")
37+
// Output to stderr so it can be captured by the calling process
38+
fputs("ERROR: Accessibility permissions are not granted for the application running this tool.\n", stderr)
39+
fputs("Please ensure the application that executes 'ax' (e.g., Terminal, your IDE, or the Node.js process) has 'Accessibility' permissions enabled in:\n", stderr)
40+
fputs("System Settings > Privacy & Security > Accessibility.\n", stderr)
41+
fputs("After granting permissions, you may need to restart the application that runs this tool.\n", stderr)
42+
43+
// Also print a more specific hint if we can identify the parent process name
44+
if let parentName = getParentProcessName() {
45+
fputs("Hint: Grant accessibility permissions to '\(parentName)'.\n", stderr)
46+
}
47+
48+
// Attempt a benign accessibility call to encourage the OS to show the permission prompt
49+
// for the parent application. The ax tool will still exit with an error for this run.
50+
fputs("Info: Attempting a minimal accessibility interaction to help trigger the system permission prompt if needed...\n", stderr)
51+
let systemWideElement = AXUIElementCreateSystemWide()
52+
var focusedElement: AnyObject?
53+
_ = AXUIElementCopyAttributeValue(systemWideElement, kAXFocusedUIElementAttribute as CFString, &focusedElement)
54+
// We don't use the result of the call above when permissions are missing;
55+
// its purpose is to signal macOS to check/prompt for the parent app's permissions.
56+
4757
exit(1)
58+
} else {
59+
debug("Accessibility permissions are granted.")
4860
}
49-
*/
5061
}
5162

5263
// MARK: - Codable command envelopes -------------------------------------------------

0 commit comments

Comments
 (0)