【问题标题】:Finder Scripting Bridge to ShutdownFinder 脚本桥接至关机
【发布时间】:2011-06-07 20:35:47
【问题描述】:

我尝试使用 Application Scripting Bridge 让我的 Mac 进入睡眠状态。 代码如下所示:

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

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

【问题讨论】:

    标签: objective-c cocoa macos scripting-bridge


    【解决方案1】:

    正如我在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()CoreServicesAE.framework 的一部分)。

    【讨论】:

    • 太棒了。也发现了 Apple 页面,但看到它使用的是 Carbon……
    • 它不适用于 10.13。你有什么建议吗?我收到以下错误:AppleEvents: received mach msg which is not complex type as expected in getMemoryReference.
    【解决方案2】:

    如果 Scripting Bridge 不足以执行非应用程序特定的操作,例如关闭 Mac,那么您可以迁移到 Applescript(以及扩展的 Scripting Bridge)无法直接访问的其他框架.如需关闭 Mac,请参阅核心服务:Technical Q&A QA1134: Programmatically causing restart, shutdown and/or logout

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多