简体   繁体   English

Java的Sleep OS X

[英]Sleep OS X from Java

Really simple little function, but does anyone know how to sleep OS X from Java? 真正简单的小功能,但是有人知道如何从Java休眠OS X吗?

Cheers 干杯

System.exec("osascript -e 'tell application \"System Events\" to sleep'");

See: Making Mac OS X sleep from the command line 请参阅: 使Mac OS X从命令行进入睡眠状态

Create a script with the following: 使用以下命令创建脚本:

#!/bin/bash
osascript << EOT
tell application "System Events"
     sleep
end
EOT

And use system to exec it. 并使用系统执行它。

    
public void gotoSleep(){
    try{
        logger.finer("Zzz...");

        if (preferences.getOS().equals("OSX") == true ){
        Process p = Runtime.getRuntime().exec
            ("/bin/bash");
        String command = "osascript -e 'tell application \"System Events\"' " 
            + " -e \"sleep\" -e 'end tell'";

        OutputStream stdin = p.getOutputStream();
        stdin.write( command.getBytes() );
        stdin.flush();
        stdin.close();
        }

    }catch( Exception e ) { 
        logger.warning( e.toString() );
    }
}

For Some reason while i was doing it it did not work without executing it through bash. 由于某些原因,当我这样做时,如果不通过bash执行它,它将无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM