Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shutdown Mac Objective C

I need to be able to implement methods of:

  • Shutdown
  • Restart
  • Logoff
  • Sleep

On a Mac, I am using XCode but can't seem to figure out the code to perform these actions.

Can anyone help me out here?

Thanks

like image 659
Sandeep Bansal Avatar asked Dec 29 '22 03:12

Sandeep Bansal


1 Answers

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); 
}
like image 67
Regexident Avatar answered Dec 31 '22 13:12

Regexident