我尝试使用应用程序脚本桥来让我的Mac进入睡眠状态。代码如下所示:
#import "Finder.h"
FinderApplication *Finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.finder"];
[Finder sleep];但它不起作用。你知道为什么它不起作用吗?没有编译错误或警告,但它不工作…
发布于 2011-06-09 02:49:25
正如我在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中的代码,但我的代码被重新设计为使用AESendMessage()而不是AESend()。AESend()使用的是HIToolbox.framework,而Carbon.framework使用的是64位应用程序。(AESendMessage()是CoreServices中AE.framework的一部分)。
发布于 2011-06-09 02:24:28
如果Scripting Bridge不足以做一些非特定于应用程序的事情,比如关闭Mac,那么你可以转移到Applescript (扩展为Scripting Bridge)不能直接访问的其他框架。有关关闭Mac的信息,请参阅核心服务:Technical Q&A QA1134: Programmatically causing restart, shutdown and/or logout
https://stackoverflow.com/questions/6271300
复制相似问题