Could you please help me to solve the problem or give the advice? I am trying to send an apple event and get a response asynchronously.
Here is the example code for sending the apple event:
let target = NSAppleEventDescriptor(applicationURL: URL(string: “eppc://userName:password@ip/appName”)!) let appleEvent = NSAppleEventDescriptor(eventClass: AEEventClass(stringLiteral: “test”), eventID: AEEventID(stringLiteral: “evnt”), targetDescriptor: target, returnID: AEReturnID(kAutoGenerateReturnID), transactionID: AETransactionID(kAnyTransactionID)) let resultEvent = try appleEvent.sendEvent(options: [.queueReply], timeout: TimeInterval(kNoTimeOut))
Here is what the event handler looks like in the target application:
@objc func handler(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) { try replyEvent!.sendEvent(options: [.noReply], timeout: TimeInterval(kNoTimeOut)) }
I encountered two difficulties using the ‘queueReply’ sending mode:
- I see the input window for credentials in the case when the event handler app tries to send the response to the sender app.
(This is a problem for me since there is no one to enter the password on the side of the handler app. This window for credentials entry is not displayed if the sending is in the “waitForReply” mode)
- After entering the correct credentials, the sender app receives the error AppleEvents/sandbox: Returning errAEPrivilegeError/-10004
Sender app logs:
{test,evnt target=aprl(38/$657070633a2f2f3139322e3136382e302e3131352f6170706c654576656e7448...) {} returnID=27646} 2023-02-20 15:42:24.045320+0200 testevents[41720:2641106] [main] ERROR: AEImpl::~AEImpl fRetainCount not zero (1) {aevt,ansr target=aprl(38/$657070633a2f2f3139322e3136382e302e3131352f6170706c654576656e7448...) {} returnID=27646} 2023-02-20 15:42:29.593044+0200 testevents[41720:2641063] Suite NSCoreSuite, apple event code 0x1061109567x 2023-02-20 15:42:29.595201+0200 testevents[41720:2641063] Suite NSTextSuite, apple event code 0x1061109567x 2023-02-20 15:42:29.840410+0200 testevents[41720:2641063] [main] AppleEvents/sandbox: Returning errAEPrivilegeError/-10004 and denying dispatch of event aevt/ansr from process ‘’/0x0-0x0, pid=0, because it is not entitled to send the AppleEvent to this process.
Handler app logs:
{test,evnt target=aprl(27/$657070633a2f2f3139322e3136382e302e3131353a35393037362f) {}} {aevt,ansr target=aprl(27/$657070633a2f2f3139322e3136382e302e3131353a35393037362f) {}} 2023-02-20 15:42:29.569897+0200 appleEventHandler[41454:2635163] [main] ERROR: AEImpl::~AEImpl fRetainCount not zero (1) 2023-02-20 15:42:29.576049+0200 appleEventHandler[41454:2640788] [connection] nw_socket_handle_socket_event [C4:1] Socket SO_ERROR [61: Connection refused] 2023-02-20 15:42:29.577080+0200 appleEventHandler[41454:2638578] [connection] nw_connection_get_connected_socket [C4] Client called nw_connection_get_connected_socket on unconnected nw_connection ...
In this case, the sender app receives the response but is not handled. Handler code:
NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(self.handler), forEventClass: AEEventClass(stringLiteral: “aevt”), andEventID: AEEventID(stringLiteral: “ansr”)) @objc func handler(_ event: NSAppleEventDescriptor?, withReplyEvent: NSAppleEventDescriptor?) throws { print(event?.description) }
But this code works correctly with:
let target = NSAppleEventDescriptor(bundleIdentifier: “test.com.appleEventHandler”)
Logs:
{test,evnt target=bund(26/$746573742e636f6d2e6170706c654576656e7448616e646c6572) {} returnID=3183} {aevt,ansr target=appleEventHandler {} returnID=3183} Optional(“<NSAppleEventDescriptor: \‘aevt\’\\‘ansr\’{ }>“) {aevt,ansr target=appleEventHandler {} returnID=3183} Optional(“<NSAppleEventDescriptor: \‘aevt\’\\‘ansr\’{ }>“)
Please help me to understand how can I get an async response using the send option “queueReply” by eppc? (“waitForReply” option works correctly with eppc in this code)