Sandbox is off but my app cant use certain features

  • Sandbox is set to no in the entitlements file.
  • Settings → security & privacy → privacy → accessibility is enabled for the app.
  • Can detect global mouse events.
  • Can't use accessibility features:
let systemWideElement: AXUIElement = AXUIElementCreateSystemWide()

var focusedElement : AnyObject?
let error = AXUIElementCopyAttributeValue(
        systemWideElement,
        kAXFocusedUIElementAttribute as CFString,
        &focusedElement
)
print(focusedElement)  // this prints `nil`
  • Can't execute applescripts ( NSAppleScript() )
  • Can't send keypress events ( CGEvent().post() )

Also, if i compile the executable with swiftc from terminal and then run from terminal, the app is able to access these features.

Are there other xcode settings I need to change or are they always blocked when building from xcode?

Accepted Reply

it seems deleting everything in the xcode Derived Data folder, and deleting my app from system preferences → security & privacy → privacy → accessibility, and then re-adding has fixed it

(i had two apps with the same name so perhaps that caused it to bug-out)


EDIT: with further testing it seems now occasionally i need to remove the app from system preferences → security & privacy → privacy → accessibility to get it to work

Replies

Even setting the pasteboard fails:

class AppDelegate: NSObject, NSApplicationDelegate {

    func applicationDidFinishLaunching(_ aNotification: Notification) {

        let prevClipboard: String = NSPasteboard.general.string(forType: .string) ?? ""
        print("prev: \(prevClipboard)")  // returns "string 1"
        
        // modify clipboard from "string 1" to "string 2"
        NSPasteboard.general.setString("string 2", forType: .string)
        
        let newClipboard: String = NSPasteboard.general.string(forType: .string) ?? ""
        print("prev: \(newClipboard)") // returns "string 1" (not "string 2" as it should, thus it shows i am unable to modify the clipboard)
    }
}

it seems deleting everything in the xcode Derived Data folder, and deleting my app from system preferences → security & privacy → privacy → accessibility, and then re-adding has fixed it

(i had two apps with the same name so perhaps that caused it to bug-out)


EDIT: with further testing it seems now occasionally i need to remove the app from system preferences → security & privacy → privacy → accessibility to get it to work