2011-05-23 38 views
3

我想执行某些系统事件,Mac OS X的

  • 关机
  • 重启
  • 注销
  • 睡眠

我的系统通过我提出申请,我似乎无法找到任何本地的Objective C方式来做到这一点,这非常困难。

任何人都可以指导我做到这一点的最好办法:

我曾尝试:

NSString *scriptAction = @"restart"; // @"restart"/@"shut down"/@"sleep"/@"log out" 
NSString *scriptSource = [NSString stringWithFormat:@"tell application \"Finder\" to %@", scriptAction]; 
NSAppleScript *appleScript = [[[NSAppleScript alloc] initWithSource:scriptSource] autorelease]; 
NSDictionary *errDict = nil; 
if (![appleScript executeAndReturnError:&errDict]) { 
    // 
} 

那没有运气可言,也试过:

NSAppleScript* theScript = [[NSAppleScript alloc] initWithSource: 
          @"Tell application \"Finder\" to restart"]; 
if (theScript != NULL) 
{ 
    NSDictionary* errDict = NULL; 
    // execution of the following line ends with EXC 
    if (YES == [theScript compileAndReturnError: &errDict]) 
    { 
     [theScript executeAndReturnError: &errDict]; 
    } 
    [theScript release]; 
} 

由于没有运气

+1

可能重复[Shutdown Mac Objective C](http://stackoverflow.com/questions/4505632/shutdown-mac-objective-c) – 2011-05-23 23:03:56

+2

技术问答1134应该会有帮助:http://developer.apple.com /library/mac/#qa/qa1134/_index.html – 2011-05-23 23:08:44

+0

嗨乔希。我实际上发布了第一个问题。完全忘了它。我已经尝试了所有列出的方法,包括没有运气的q&a – 2011-05-23 23:19:11

回答

9

我已经使用超过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的一部分)。

+0

完美的作品!谢谢 – 2011-05-24 11:11:12

+0

它对我来说不起作用:(对任何操作使用此功能将在调用AESendMessage时为-600'[“procNotFound”](“没有指定描述符的合格进程”)提供状态想法为什么? – Alex 2011-09-27 19:34:55

+0

这个工作完美无沙箱。但是一旦SandBoxing打开它不起作用。谁能告诉我哪些权利被启用以使其工作。 – 2017-06-08 11:16:42

0

这绝对应该工作如果您从GUI会话登录

如果您仅在没有GUI的情况下从ssh会话等登录,它将不起作用。

请更全面地描述你的情况,你得到了什么样的错误等,否则我们不能帮你。

+0

我根本没有收到任何错误。我已经在界面构建器中正确连线了所有东西。分配方法和一切。但是涉及到代码执行的部分。什么都没发生。是的,应用程序将运行在基于GUI的应用程序上。 – 2011-05-24 00:17:54

相关问题