开发者

Shutdown Mac Objective C

I need to be able to implement methods of:

  • Shutdown
  • Restart
  • Logoff
  • Sleep

On a Mac, I am using X开发者_运维知识库Code but can't seem to figure out the code to perform these actions.

Can anyone help me out here?

Thanks


A trivial/lazy way do this is via some simple inline Applescript:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜