gpt4 book ai didi

objective-c - 以编程方式让 Mac 进入休眠状态

转载 作者:太空狗 更新时间:2023-10-30 03:30:07 26 4
gpt4 key购买 nike

我找不到任何说明如何以编程方式将 Mac 置于 sleep 模式(在 Objective-C 中)。我确定它应该只有一行,但你能给我一个提示吗?

最佳答案

#include <stdio.h> 
#include <CoreServices/CoreServices.h>
#include <Carbon/Carbon.h>

SendAppleEventToSystemProcess(kAESleep);

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);

AEDisposeDesc(&appleEventToSend);
if (error != noErr)
{
return(error);
}

AEDisposeDesc(&eventReply);

return(error);
}

有关 https://developer.apple.com/library/content/qa/qa1134/_index.html 的更多详细信息

关于objective-c - 以编程方式让 Mac 进入休眠状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6243140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com