Programmatically put a Mac into sleep

后端 未结 6 1004
清歌不尽
清歌不尽 2021-02-03 13:21

I can\'t find any instructions how to put a Mac programmatically into sleep mode (in Objective-C). I\'m sure it should be only one line, but could you give me a hint?

6条回答
  •  说谎
    说谎 (楼主)
    2021-02-03 13:56

    You can use AppleScript

    NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"tell application \"System Events\" to sleep"];
    NSDictionary *errorInfo;
    [script executeAndReturnError:&errorInfo];
    [script release];
    

提交回复
热议问题