Finder脚本桥关闭 [英] Finder Scripting Bridge to Shutdown

查看:223
本文介绍了Finder脚本桥关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用应用程序脚本桥来发送我的Mac睡觉。
代码如下:

I tried to use Application Scripting Bridge to send my Mac to sleep. The code look like the following:

#import "Finder.h"
 FinderApplication *Finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.finder"];
        [Finder sleep];

但它不工作。任何想法为什么它不工作?没有编译错误或警告,但它不工作...

But it doesn't work. Any ideas why it doesn't work? No compiling errors or warnings, but it doesn't work…

推荐答案

正如我在这个答案,我一直使用以下代码超过8年没有问题:

As I posted in this answer, I've been using the following code for over 8 years without issues:

MDRestartShutdownLogout.h:

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:

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

请注意,上述代码基于技术问答QA1134 ,但我们重新使用 AESendMessage(),而不是 AESend() AESend()位于 HIToolbox.framework 中,位于 Carbon.framework ,因此64位应用程序不可用。 ( AESendMessage() AE.framework 的一部分,位于 CoreServices )。

Note that the above code is based on the code from Technical Q&A QA1134, but mine is re-worked to use AESendMessage() rather than AESend(). AESend() is in HIToolbox.framework, which is in Carbon.framework and is therefore unavailable to 64-bit apps. (AESendMessage() is part of the AE.framework in CoreServices).

这篇关于Finder脚本桥关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆