我最近注意到我的应用程序无法再发送kAESleep
,kAEReallyLogOut
和kAEShutDown
等系统事件,而且我不能为我的生活找出原因。
我只能说,它在6个月前我上次测试这些特定选项时起作用了。从那以后,我可能已经升级了Xcode和操作系统本身,所以也许在引擎盖下发生了一些变化。
(非投掷)错误:
2018-04-22 10:24:36.548501+1000 <APP-NAME>[15698:628777] AppleEvents: received mach msg which wasn't complex type as expected in getMemoryReference.
我的功能看起来像这样(借用https://forums.developer.apple.com/thread/90702):
private func sendSystemCommand(command: AEEventID) throws {
var psn = ProcessSerialNumber(highLongOfPSN: UInt32(0), lowLongOfPSN: UInt32(kSystemProcess))
let target = NSAppleEventDescriptor(descriptorType: typeProcessSerialNumber, bytes: &psn, length: MemoryLayout.size(ofValue: psn))
let event = NSAppleEventDescriptor(
eventClass: kCoreEventClass,
eventID: command,
targetDescriptor: target,
returnID: AEReturnID(kAutoGenerateReturnID),
transactionID: AETransactionID(kAnyTransactionID)
)
_ = try event.sendEvent(options: [.defaultOptions], timeout: TimeInterval(kAEDefaultTimeout))
}
实施如下:
do {
try self.sendSystemCommand(command: kAEShutDown)
print("Shutting down")
} catch {
print("Error sending `shut down` command to system")
}
有什么想法吗?
编辑:经过多次来回,我相信这种行为是由应用沙盒系统引起的。我可以通过发送像osascript -e 'tell application "System Events" to shut down'
这样的系统命令来观察同样的问题。该命令从终端shell按预期工作,但从我的应用程序发送时返回错误。
我一直认为应用程序可以请求发送系统事件命令的权限,但我无法弄清楚 - 它似乎没有得到很好的记录。
答案 0 :(得分:0)
尝试为topics.invert.invert
添加com.apple.security.temporary-exception.apple-events
的权利:
com.apple.systemevents
答案 1 :(得分:-1)
我有这个问题,我用Objective C这样做
OSStatus SendAppleEventToSystemProcess(AEEventID EventToSend) {
AEAddressDesc targetDesc;
static const ProcessSerialNumber kPSNOfSystemProcess = { 0, kSystemProcess };
AppleEvent eventReply = { typeNull, NULL };
AppleEvent appleEventToSend = { typeNull, NULL };
OSStatus error = noErr;
error = AECreateDesc(typeProcessSerialNumber, &kPSNOfSystemProcess, sizeof(kPSNOfSystemProcess), &targetDesc);
if (error != noErr) {
return error;
}
error = AECreateAppleEvent(kCoreEventClass, EventToSend, &targetDesc, kAutoGenerateReturnID, kAnyTransactionID, &appleEventToSend);
AEDisposeDesc(&targetDesc);
if (error != noErr) {
return error;
}
error = AESend(&appleEventToSend, &eventReply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
// error = AESendMessage(&amp; appleEventToSend,&amp; eventReply,kAENormalPriority,kAEDefaultTimeout);
AEDisposeDesc(&appleEventToSend);
if (error != noErr) {
return error;
}
AEDisposeDesc(&eventReply);
return error;
}