sindresorhus / get-windows

Get metadata about the active window and open windows (title, id, bounds, owner, etc)
MIT License
798 stars 166 forks source link

chore: fix accessibility permission check logic 🔧 #180

Closed kentbetita closed 8 months ago

kentbetita commented 8 months ago

Description

This pull request addresses an issue where the package was incorrectly prompting for accessibility permissions even when the disableAccessibilityPermission option was set to false.

Changes

Problem

The original code used a comma-separated list of conditions in an if statement, which in Swift, functions as a logical AND operator. This meant that both conditions had to be evaluated, and as long as the first condition (!disableAccessibilityPermission) was true (i.e., disableAccessibilityPermission was false), the second condition (!AXIsProcessTrustedWithOptions(["AXTrustedCheckOptionPrompt": true] as CFDictionary)) would also be evaluated.

This led to a situation where, even when disableAccessibilityPermission was set to false, the code would still trigger the accessibility permission check because the second condition was always checked, thus resulting in an unintended prompt for accessibility permission.

Solution

The solution involved restructuring the conditional statement to ensure that the AXIsProcessTrustedWithOptions check is only performed when disableAccessibilityPermission is explicitly set to false. Now, the second condition is entirely dependent on the first condition being true. This change respects the user's preference for the disableAccessibilityPermission flag and prevents the accessibility permission prompt from appearing unless it is necessary.

Testing

Disclaimer

Please note that while I am not a Swift developer by profession, I have taken care to ensure that the changes proposed in this pull request adhere to best practices to the best of my knowledge and have been thoroughly tested.

kentbetita commented 8 months ago

We could also update the condition from getWindowInformation for consistency:

// Run the AppleScript to get the URL if the active window is a compatible browser and accessibility permissions are enabled.
if !disableAccessibilityPermission {
    if let bundleIdentifier = app.bundleIdentifier,
       let script = getActiveBrowserTabURLAppleScriptCommand(bundleIdentifier),
       let url = runAppleScript(source: script) {
        output["url"] = url
    }
}

but from my testing it has been working fine for both cases. This is optional.