Shutdown Mac Objective C

前端 未结 1 1519
鱼传尺愫
鱼传尺愫 2021-01-14 01:58

I need to be able to implement methods of:

  • Shutdown
  • Restart
  • Logoff
  • Sleep

On a Mac, I am using XCode but can\'t seem t

相关标签:
1条回答
  • 2021-01-14 02:13

    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 讨论(0)
提交回复
热议问题