Objective c 查找器脚本桥关闭
我尝试使用应用程序脚本桥将我的Mac送至睡眠状态。 代码如下所示:Objective c 查找器脚本桥关闭,objective-c,cocoa,macos,scripting-bridge,Objective C,Cocoa,Macos,Scripting Bridge,我尝试使用应用程序脚本桥将我的Mac送至睡眠状态。 代码如下所示: #import "Finder.h" FinderApplication *Finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.finder"]; [Finder sleep]; 但它不起作用。你知道为什么它不起作用吗?没有编译错误或警告,但它不起作用…如果脚本桥不足以执行非特定于应用程序的操作,例如关闭Mac,那么您就可以转
#import "Finder.h"
FinderApplication *Finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.finder"];
[Finder sleep];
但它不起作用。你知道为什么它不起作用吗?没有编译错误或警告,但它不起作用…如果脚本桥不足以执行非特定于应用程序的操作,例如关闭Mac,那么您就可以转移到Applescript(扩展为脚本桥)无法直接访问的其他框架。要关闭Mac,请参阅核心服务:正如我在中发布的,我已经使用以下代码超过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);
请注意,上面的代码基于中的代码,但我的代码被重新处理为使用
AESendMessage()
而不是AESend()
AESend()
位于HIToolbox.framework
中,它位于Carbon.framework
中,因此64位应用程序无法使用。(AESendMessage()
是CoreServices
中aes.framework
的一部分)。太好了。发现苹果的页面也有,但看到它使用了碳……它在10.13上不起作用。你有什么建议吗?我得到以下错误:AppleEvents:received mach msg不是getMemoryReference中预期的复杂类型。
#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;
}