Thanks for your response!
I tried using AEDeterminePermissionToAutomateTarget
but got no response on Xcode Version 15.2 (15C500b) and MacOS 13.5.
I think my app has a similar issue to this thread https://developer.apple.com/forums/thread/666528.
I would like to check permission before running Apple Script from my app so I use my Bundle.main.bundleIdentifier
in determinePermission()
function.
Here is my code, check()
function and request()
function does not return anything :
protocol AppAccess {
typealias Status = AppAccessStatus
func check() -> Status
func request() -> Status
}
enum AppAccessStatus {
case granted, denied, requiresConsent, notRunning, unknown(Int)
}
class AppAccessImpl: AppAccess {
func check() -> Status {
guard #available(OSX 10.14, *) else {
return .granted
}
return determinePermission(ask: false)
}
func request() -> Status {
guard #available(OSX 10.14, *) else {
return .granted
}
return determinePermission(ask: true)
}
@available(OSX 10.14, *)
private func determinePermission(ask: Bool) -> Status {
let errAEEventWouldRequireUserConsent = OSStatus(-1744)
if var addressDesc = NSAppleEventDescriptor(bundleIdentifier: Bundle.main.bundleIdentifier ?? "").aeDesc?.pointee {
let appleScriptPermission = AEDeterminePermissionToAutomateTarget(&addressDesc, typeWildCard, typeWildCard, ask)
AEDisposeDesc(&addressDesc)
switch appleScriptPermission {
case noErr: return .granted
case OSStatus(errAEEventNotPermitted): return .denied
case errAEEventWouldRequireUserConsent: return .requiresConsent
case OSStatus(procNotFound):
return .notRunning
default: return .unknown(Int(appleScriptPermission))
}
}
return .unknown(-999)
}
}
Can you help to take a look? Thanks a lot!