正如我張貼在this answer,我一直在使用下面的代碼超過8年沒有問題:
MDRestartShutdownLogout.h:
#import <CoreServices/CoreServices.h>
/*
* kAERestart will cause system to restart
* kAEShutDown will cause system to shutdown
* kAEReallyLogout will cause system to logout
* kAESleep will cause system to sleep
*/
extern OSStatus MDSendAppleEventToSystemProcess(AEEventID eventToSend);
MDRestartShutdownLogout.m:
#import "MDRestartShutdownLogout.h"
OSStatus MDSendAppleEventToSystemProcess(AEEventID eventToSendID) {
AEAddressDesc targetDesc;
static const ProcessSerialNumber kPSNOfSystemProcess = {0, kSystemProcess };
AppleEvent eventReply = {typeNull, NULL};
AppleEvent eventToSend = {typeNull, NULL};
OSStatus status = AECreateDesc(typeProcessSerialNumber,
&kPSNOfSystemProcess, sizeof(kPSNOfSystemProcess), &targetDesc);
if (status != noErr) return status;
status = AECreateAppleEvent(kCoreEventClass, eventToSendID,
&targetDesc, kAutoGenerateReturnID, kAnyTransactionID, &eventToSend);
AEDisposeDesc(&targetDesc);
if (status != noErr) return status;
status = AESendMessage(&eventToSend, &eventReply,
kAENormalPriority, kAEDefaultTimeout);
AEDisposeDesc(&eventToSend);
if (status != noErr) return status;
AEDisposeDesc(&eventReply);
return status;
}
注意,上面的代碼是基於所述代碼從Technical Q&A QA1134,但分鐘e重新使用AESendMessage()
而不是AESend()
。 AESend()
位於HIToolbox.framework
,位於Carbon.framework
,因此無法用於64位應用程序。 (AESendMessage()
是CoreServices
中的AE.framework
的一部分)。
太好了。發現蘋果頁面,但看到它使用碳... – Chris 2011-06-08 20:21:09
它不適用於10.13。你有什麼建議嗎?我得到以下錯誤:AppleEvents:收到了msg,它不像getMemoryReference中預期的那樣是複雜的類型。 – Tibidabo 2017-10-05 23:14:51