2011-06-07 51 views
0

我試圖使用Application Scripting Bridge來發送我的Mac進入睡眠狀態。 代碼如下所示:Finder腳本橋關閉

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

但它不起作用。任何想法,爲什麼它不工作?沒有編譯錯誤或警告,但它不工作...

回答

2

正如我張貼在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,但分鐘e重新使用AESendMessage()而不是AESend()AESend()位於HIToolbox.framework,位於Carbon.framework,因此無法用於64位應用程序。 (AESendMessage()CoreServices中的AE.framework的一部分)。

+0

太好了。發現蘋果頁面,但看到它使用碳... – Chris 2011-06-08 20:21:09

+0

它不適用於10.13。你有什麼建議嗎?我得到以下錯誤:AppleEvents:收到了msg,它不像getMemoryReference中預期的那樣是複雜的類型。 – Tibidabo 2017-10-05 23:14:51